diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/WasteController.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/WasteController.java index a139cfcefc28198377bd89840206532a1cb7e004..b2eb154fc6dc44bc3da0b96e2f105b2117e4d757 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/WasteController.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/controller/group/WasteController.java @@ -92,4 +92,15 @@ public class WasteController { return wasteService.getLastMonth(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build()); } + /** + * Retrieves the amount of money lost due to expired products in a specific group. + * The amount is calculated based on the total cost of the expired products. + * + * @param groupId the ID of the group to retrieve the lost money from + * @return a ResponseEntity with the lost money as a Double if found, or a ResponseEntity with status 404 if the group is not found + */ + @GetMapping("/statistic/lostMoney/{groupId}") + public ResponseEntity<Double> getLostMoney(@PathVariable("groupId") long groupId){ + return wasteService.getLostMoney(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build()); + } } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteService.java index fab7d6071c995c666bacba4ad54cd04c4062d8d7..3ab784ad7c1d996a79fb2e46c6cfdd01213e1870 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/group/WasteService.java @@ -89,4 +89,15 @@ public class WasteService { Optional<Group> group = groupRepository.findByGroupId(groupId); return group.map(value -> StatisticUtil.getNumberOfWasteByLastMonth(wasteRepository.findByGroupId(value).get())); } + + /** + * Retrieves the lost money in the last month for the group with the given ID. + * + * @param groupId the ID of the group to retrieve the lost money for + * @return an {@code Optional} containing the lost money if the group exists, or empty if it doesn't exist or there are no wastes in the last month + */ + public Optional<Double> getLostMoney(long groupId) { + Optional<Group> group = groupRepository.findByGroupId(groupId); + return group.map(value -> StatisticUtil.getLostMoneyInLastMonth(wasteRepository.findByGroupId(value).get())); + } } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java index 47a389707c90e7980d6bd55554de6b05fb3e72a6..a05b097bdbf468b08e4ff719eb7a40541a8a0345 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java @@ -3,7 +3,9 @@ package ntnu.idatt2016.v233.SmartMat.util; import ntnu.idatt2016.v233.SmartMat.entity.Waste; import java.sql.Date; +import java.sql.Timestamp; import java.time.LocalDate; +import java.time.LocalDateTime; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -117,4 +119,37 @@ public class StatisticUtil { return sum; } + /** + * Calculates the total amount of money lost due to expired or discarded items in the last 30 days, + * based on the list of wastes passed as a parameter. + * + * @param wastes the list of wastes to be analyzed + * @return the total amount of money lost due to expired or discarded items in the last 30 days + */ + public static double getLostMoneyInLastMonth(List<Waste> wastes){ + List<Waste> wasteList = new ArrayList<>(); + LocalDateTime time = LocalDateTime.now().minusDays(30); + for(Waste waste: wastes){ + if(waste.getTimestamp().after(Timestamp.valueOf(time))){ + wasteList.add(waste); + } + } + return lostMoney(wasteList); + } + + /** + * Calculates the total amount of money lost due to expired or discarded items, + * based on the list of wastes passed as a parameter. + * + * @param wastes the list of wastes to be analyzed + * @return the total amount of money lost due to expired or discarded items + */ + private static double lostMoney(List<Waste> wastes){ + double sum = 0.0; + for(Waste waste: wastes){ + sum += waste.getAmount()*waste.getBuyPrice(); + } + return sum; + } + } \ No newline at end of file