From d207386d1de2d7fb6f0aed81a5885f235f319433 Mon Sep 17 00:00:00 2001 From: Mathis Kral Date: Sun, 26 Nov 2023 12:10:28 +0100 Subject: [PATCH] Add staff functionality to CustomCart and CustomOrder This works on #73 This also contains unit-tests for the CustomOrder. --- src/main/java/catering/order/CustomCart.java | 42 ++++++++- src/main/java/catering/order/CustomOrder.java | 61 ++++++++++--- .../java/catering/order/OrderUnitTests.java | 85 +++++++++++++++++++ 3 files changed, 175 insertions(+), 13 deletions(-) create mode 100644 src/test/java/catering/order/OrderUnitTests.java diff --git a/src/main/java/catering/order/CustomCart.java b/src/main/java/catering/order/CustomCart.java index 1872538..8457371 100644 --- a/src/main/java/catering/order/CustomCart.java +++ b/src/main/java/catering/order/CustomCart.java @@ -1,11 +1,15 @@ package catering.order; +import catering.staff.Staff; import org.salespointframework.order.Cart; +import java.time.Duration; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; +import java.util.HashSet; +import java.util.Set; public class CustomCart extends Cart { + private Set staff; private OrderType orderType; private LocalDateTime start; private LocalDateTime finish; @@ -14,10 +18,37 @@ public class CustomCart extends Cart { // Constructor public CustomCart(OrderType orderType, LocalDateTime start, LocalDateTime finish) { super(); + this.staff = new HashSet<>(); this.orderType = orderType; this.start = start; this.finish = finish; - this.formatterPattern = "dd.MM.yyy, HH:mm 'Uhr'"; + this.formatterPattern = "dd.MM.yyyy, HH:mm 'Uhr'"; + } + + /** + * Adds an employee to the cart + */ + public boolean addStaff(Staff staff) { + return this.staff.add(staff); + } + + public Set getStaff() { + return staff; + } + + public boolean removeStaff(Staff staff) { + return this.staff.remove(staff); + } + + /** + * Add staff to order (analogous to cart.addItemsTo(order)) + */ + public CustomOrder addStaffTo(CustomOrder order) { + for (Staff staff : this.staff) { + order.addStaff(staff); + } + + return order; } public OrderType getOrderType() { @@ -36,6 +67,13 @@ public class CustomCart extends Cart { this.start = start; } + /** + * @return hours between start and finish + */ + public long getDurationInHours(LocalDateTime start, LocalDateTime finish) { + return Duration.between(start, finish).getSeconds() / 3600; + } + public LocalDateTime getFinish() { return finish; } diff --git a/src/main/java/catering/order/CustomOrder.java b/src/main/java/catering/order/CustomOrder.java index f7e0fc3..8e49d28 100644 --- a/src/main/java/catering/order/CustomOrder.java +++ b/src/main/java/catering/order/CustomOrder.java @@ -1,32 +1,73 @@ package catering.order; +import catering.staff.Staff; import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.OneToMany; +import org.javamoney.moneta.Money; import org.salespointframework.order.Order; import org.salespointframework.payment.Cash; import org.salespointframework.useraccount.UserAccount; -import org.springframework.stereotype.Component; +import org.springframework.data.annotation.Id; +import javax.money.MonetaryAmount; +import java.time.Duration; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.HashSet; +import java.util.Set; @Entity public class CustomOrder extends Order { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + @OneToMany + private Set staff; private OrderType orderType = OrderType.SOMETHING_ELSE; private LocalDateTime start; private LocalDateTime finish; private boolean invoiceAvailable = false; - private String formatterPattern = "dd.MM.yyy, HH:mm 'Uhr'"; + private String formatterPattern = "dd.MM.yyyy, HH:mm 'Uhr'"; // Constructor public CustomOrder(UserAccount.UserAccountIdentifier identifier, CustomCart cart) { super(identifier, Cash.CASH); + this.staff = new HashSet<>(); this.orderType = cart.getOrderType(); this.start = cart.getStart(); this.finish = cart.getFinish(); this.formatterPattern = cart.getFormatterPattern(); } - public CustomOrder() {} // TODO: find out, why this is mandatory + public CustomOrder() {} + + /** + * Helper function to get the amount of hours in between start and finish (analogous to CustomCart) + * @return hours between start and finish + */ + private long getDurationInHours(LocalDateTime start, LocalDateTime finish) { + return Duration.between(start, finish).getSeconds() / 3600; + } + + /** + * Adds an employee to the order and adds a new {@Link ChangeLine} containing the costs + */ + public boolean addStaff(Staff staff) { + MonetaryAmount cost = Money.of(12.0, "EUR") + .multiply(getDurationInHours(this.start, this.finish)); // TODO: Get salary from staff + + if (this.staff.add(staff)) { + super.addChargeLine(cost, staff.getId().toString()); + return true; + } + return false; + } + + public Set getStaff() { + return staff; + } public OrderType getOrderType() { return orderType; @@ -36,26 +77,24 @@ public class CustomOrder extends Order { return start; } + /** + * @return start DateTime in a formatted manner + */ public String getFormattedStart() { return start.format(DateTimeFormatter.ofPattern(formatterPattern)); } - public void setStart(LocalDateTime start) { - this.start = start; - } - public LocalDateTime getFinish() { return finish; } + /** + * @return finish DateTime in a formatted manner + */ public String getFormattedFinish() { return finish.format(DateTimeFormatter.ofPattern(formatterPattern)); } - public void setFinish(LocalDateTime finish) { - this.finish = finish; - } - public boolean isInvoiceAvailable() { return invoiceAvailable; } diff --git a/src/test/java/catering/order/OrderUnitTests.java b/src/test/java/catering/order/OrderUnitTests.java new file mode 100644 index 0000000..359b271 --- /dev/null +++ b/src/test/java/catering/order/OrderUnitTests.java @@ -0,0 +1,85 @@ +package catering.order; + +import catering.staff.JobType; +import catering.staff.Staff; +import catering.staff.StaffManagement; +import org.javamoney.moneta.Money; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.salespointframework.catalog.Product; +import org.salespointframework.quantity.Quantity; +import org.salespointframework.useraccount.UserAccount; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.*; + +@SpringBootTest +public class OrderUnitTests { + @Autowired + StaffManagement staffManagement; + Staff staff; + CustomOrder order; + + + @BeforeEach + void setUp() { + staff = new Staff("Peter Muffin", JobType.COOK); + staffManagement.addStaff(staff); + + order = new CustomOrder(UserAccount.UserAccountIdentifier.of("12345"), + new CustomCart(OrderType.RENT_A_COOK, + LocalDateTime.of(2023, 11, 10, 22, 0), + LocalDateTime.of(2023, 11, 11, 9, 0))); + } + + @Test + void dateTimeFormatter() { + assertThat(order.getFormattedStart()).isEqualTo("10.11.2023, 22:00 Uhr"); + assertThat(order.getFormattedFinish()).isEqualTo("11.11.2023, 09:00 Uhr"); + } + + @Test + void addStaffToOrder() { + order.addStaff(staff); + + // test if staff was added + assertThat(order.getStaff()).hasSize(1); + + // test if ChargeLine is correct + assertThat(order.getChargeLines().stream().count()).isEqualTo(1); + assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*12, "EUR")); + } + + @Test + void orderWithStaffAndProducts() { + order.addOrderLine(new Product("Brötchen (vegan)", + Money.of(0.6, "EUR")), + Quantity.of(10)); + + order.addStaff(staff); + + // test if staff was added + assertThat(order.getStaff()).hasSize(1); + assertThat(order.getOrderLines().stream().count()).isEqualTo(1); + + // test if ChargeLine is correct + assertThat(order.getChargeLines().stream().count()).isEqualTo(1); + + // test if costs are correct + assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*12, "EUR")); + assertThat(order.getOrderLines().getTotal()).isEqualTo(Money.of(10.0*0.6, "EUR")); + assertThat(order.getTotal()).isEqualTo(Money.of(11.0 * 12.0 + 10.0 * 0.6, "EUR")); + + // test for duplication + order.addStaff(staff); + assertThat(order.getStaff()).hasSize(1); + assertThat(order.getOrderLines().stream().count()).isEqualTo(1); + assertThat(order.getChargeLines().stream().count()).isEqualTo(1); + assertThat(order.getChargeLines().getTotal()).isEqualTo(Money.of(11*12, "EUR")); + assertThat(order.getOrderLines().getTotal()).isEqualTo(Money.of(10.0*0.6, "EUR")); + assertThat(order.getTotal()).isEqualTo(Money.of(11.0 * 12.0 + 10.0 * 0.6, "EUR")); + } +}