Add staff functionality to CustomCart and CustomOrder

This works on #73
This also contains unit-tests for the CustomOrder.
This commit is contained in:
Mathis Kral 2023-11-26 12:10:28 +01:00 committed by Mathis
parent 53c40fec2f
commit d207386d1d
3 changed files with 175 additions and 13 deletions

View file

@ -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> 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<Staff> 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;
}

View file

@ -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> 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<Staff> 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;
}

View file

@ -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"));
}
}