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 efb3e0361b544a547bcb51eba8c0bb1632d464e6..a139cfcefc28198377bd89840206532a1cb7e004 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 @@ -79,4 +79,17 @@ public class WasteController { return wasteService.getCakeDiagram(groupId).map(ResponseEntity::ok).orElseGet(()->ResponseEntity.notFound().build()); } + + /** + * Get the information of the last months of a specific group. + * + * @param groupId the id of the group to get the information for + * @return a ResponseEntity object containing an array of doubles representing the waste for each category + * in the last four months, or a not found response if the group does not exist or has no waste data + */ + @GetMapping("/statistic/lastMonths/{groupId}") + public ResponseEntity<double[]> getInformationOfLastMoths(@PathVariable("groupId") long groupId){ + return wasteService.getLastMonth(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 8f43267c5e6805f2d2924544274c110db0510b25..fab7d6071c995c666bacba4ad54cd04c4062d8d7 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 @@ -1,5 +1,6 @@ package ntnu.idatt2016.v233.SmartMat.service.group; +import java.nio.channels.FileChannel; import java.sql.Timestamp; import java.util.List; import lombok.AllArgsConstructor; @@ -77,4 +78,15 @@ public class WasteService { return group.map(value -> StatisticUtil.getNumberOfWasteByCategoryName(wasteRepository.findByGroupId(value).get())); } + /** + * Retrieve an optional array of doubles representing the amount of waste produced in each of the last 4 months for a given group. + * + * @param groupId a long representing the id of the group whose waste production statistics are to be retrieved + * @return an optional array of doubles representing the amount of waste produced in each of the last 4 months for the given group, + * or an empty optional if the group could not be found or no waste was produced in the last 4 months + */ + public Optional<double[]> getLastMonth(long groupId) { + Optional<Group> group = groupRepository.findByGroupId(groupId); + return group.map(value -> StatisticUtil.getNumberOfWasteByLastMonth(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 4ec0de83f551862e720a27aa4185cf3d83a92d6d..47a389707c90e7980d6bd55554de6b05fb3e72a6 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java @@ -3,6 +3,9 @@ package ntnu.idatt2016.v233.SmartMat.util; import ntnu.idatt2016.v233.SmartMat.entity.Waste; import java.sql.Date; +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; @@ -47,4 +50,71 @@ public class StatisticUtil { } return numberOfWastes; } + + /** + * Calculates the sum of waste for each of the four previous months. + * The amount of waste is converted to kilograms if the unit is in liters, grams, milliliters, centiliters, or deciliters. + * + * @param wastes the list of wastes to calculate the sum from + * @return an array of four doubles representing the sum of waste for each of the last four months, + * in the same order as the months are counted backwards from the current month + */ + public static double[] getNumberOfWasteByLastMonth(List<Waste> wastes){ + double[] result = new double[4]; + HashMap<Integer,List<Waste>> hashMap = new HashMap<>(); + + LocalDate localDate = LocalDate.now(); + int currentMonth = localDate.getMonthValue(); + int currentYear = localDate.getYear(); + for(Waste waste : wastes){ + LocalDate localDate1 = waste.getTimestamp().toLocalDateTime().toLocalDate(); + if(currentMonth == localDate1.getMonthValue() && currentYear == localDate1.getYear()){ + hashMap.computeIfAbsent(0, k -> new ArrayList<>()); + hashMap.get(0).add(waste); + } + if(Math.abs((currentMonth-1) % 12) == localDate1.getMonthValue() && currentYear == localDate1.getYear()){ + hashMap.computeIfAbsent(1, k -> new ArrayList<>()); + hashMap.get(1).add(waste); + } + if(Math.abs((currentMonth-2) % 12) == localDate1.getMonthValue() && currentYear == localDate1.getYear()){ + hashMap.computeIfAbsent(2, k -> new ArrayList<>()); + hashMap.get(2).add(waste); + } + if(Math.abs((currentMonth-3) % 12) == localDate1.getMonthValue() && currentYear == localDate1.getYear()){ + hashMap.computeIfAbsent(3, k -> new ArrayList<>()); + hashMap.get(3).add(waste); + } + } + for(int i = 0; i < 4; i++){ + result[i] = getSumOfWaste(hashMap.get(i)); + } + return result; + } + + /** + * Calculates the sum of waste for a list of wastes, and converts the amounts to kilograms if the unit is in liters, + * grams, milliliters, centiliters, or deciliters. + * + * @param wastes the list of wastes to calculate the sum from + * @return the sum of waste in kilograms + */ + private static double getSumOfWaste(List<Waste> wastes){ + double sum = 0.0; + if(wastes == null){ + wastes = new ArrayList<>(); + } + for(Waste waste: wastes){ + switch (waste.getEan().getUnit()){ + case "l" -> sum += waste.getAmount() * 0.998; + case "kg" -> sum += waste.getAmount(); + case "g" -> sum += waste.getAmount() * 0.001; + case "ml" -> sum += waste.getAmount() * 0.000998; + case "cl" -> sum += waste.getAmount() * 0.00998; + case "dl" -> sum += waste.getAmount() * 0.0998; + default -> sum += 0.1; + } + } + return sum; + } + } \ No newline at end of file