From 958c012a8e6556868de67294fa24dad21e574854 Mon Sep 17 00:00:00 2001 From: Pedro Cardona <pedropca@stud.ntnu.no> Date: Tue, 2 May 2023 14:38:27 +0200 Subject: [PATCH] Endpoint to add lost money was added --- .../controller/group/WasteController.java | 11 ++++++ .../SmartMat/service/group/WasteService.java | 11 ++++++ .../v233/SmartMat/util/StatisticUtil.java | 35 +++++++++++++++++++ 3 files changed, 57 insertions(+) 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 a139cfce..b2eb154f 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 fab7d607..3ab784ad 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 47a38970..a05b097b 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 -- GitLab