mirror of
https://github.com/st-tu-dresden-praktikum/swt23w23
synced 2024-07-19 21:04:36 +02:00
Implement per-month employee working hours
This is not yet finished, as the new/changed methods in CustomOrder are quite a mess.
This commit is contained in:
parent
ee7ab27a6d
commit
fd7d05a0f6
|
@ -11,6 +11,7 @@ import org.springframework.data.annotation.Id;
|
||||||
import javax.money.MonetaryAmount;
|
import javax.money.MonetaryAmount;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.YearMonth;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -38,14 +39,39 @@ public class CustomOrder extends Order {
|
||||||
this.formatterPattern = cart.getFormatterPattern();
|
this.formatterPattern = cart.getFormatterPattern();
|
||||||
}
|
}
|
||||||
|
|
||||||
public CustomOrder() {}
|
public CustomOrder() { }
|
||||||
|
|
||||||
|
private long getDurationInSeconds(LocalDateTime start, LocalDateTime finish) {
|
||||||
|
if (start.compareTo(finish) > 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return Duration.between(start, finish).getSeconds();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to get the amount of hours in between start and finish (analogous to CustomCart)
|
* Helper function to get the amount of hours in between start and finish (analogous to CustomCart)
|
||||||
* @return hours between start and finish
|
* @return hours between start and finish
|
||||||
*/
|
*/
|
||||||
private long getDurationInHours(LocalDateTime start, LocalDateTime finish) {
|
private long getDurationInHours(LocalDateTime start, LocalDateTime finish) {
|
||||||
return Duration.between(start, finish).getSeconds() / 3600;
|
return getDurationInSeconds(start, finish) / 3600;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDurationInHours() {
|
||||||
|
return getDurationInHours(start, finish);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static LocalDateTime min(LocalDateTime a, LocalDateTime b) {
|
||||||
|
return (a.compareTo(b)) < 0 ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static LocalDateTime max(LocalDateTime a, LocalDateTime b) {
|
||||||
|
return (a.compareTo(b)) > 0 ? a : b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDurationInSecondsDuringMonth(YearMonth month) {
|
||||||
|
LocalDateTime startInMonth = max(this.start, month.atDay(1).atStartOfDay());
|
||||||
|
LocalDateTime finishInMonth = min(this.finish, month.plusMonths(1).atDay(1).atStartOfDay());
|
||||||
|
return getDurationInSeconds(startInMonth, finishInMonth);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -53,7 +79,7 @@ public class CustomOrder extends Order {
|
||||||
*/
|
*/
|
||||||
public boolean addEmployee(Employee employee) {
|
public boolean addEmployee(Employee employee) {
|
||||||
MonetaryAmount cost = Money.of(12.0, "EUR")
|
MonetaryAmount cost = Money.of(12.0, "EUR")
|
||||||
.multiply(getDurationInHours(this.start, this.finish)); // TODO: Get salary from employee
|
.multiply(getDurationInHours()); // TODO: Get salary from employee
|
||||||
|
|
||||||
if (this.staff.add(employee)) {
|
if (this.staff.add(employee)) {
|
||||||
super.addChargeLine(cost, employee.getId().toString());
|
super.addChargeLine(cost, employee.getId().toString());
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package catering.staff;
|
package catering.staff;
|
||||||
|
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.YearMonth;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
|
@ -104,4 +105,10 @@ public class StaffManagement {
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getWorkingHoursByEmployee(Employee e, YearMonth month) {
|
||||||
|
return orderManagement.findAll(Pageable.unpaged()).stream()
|
||||||
|
.filter(order -> order.getStaff().contains(e))
|
||||||
|
.map(order -> order.getDurationInSecondsDuringMonth(month))
|
||||||
|
.collect(Collectors.summingDouble(seconds -> (double) seconds / 3600));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue