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 a1547da3db65850ab0c45a0a8422bb64ea1a092b..efb3e0361b544a547bcb51eba8c0bb1632d464e6 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 @@ -67,4 +67,16 @@ public class WasteController { return wasteService.getWasteOfCategoryByGroupId(groupId, CategoryUtil.getCategoryName(categoryNumber)).map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build()); } + /** + * Retrieve information about the cake diagram for a given group ID using a GET request. + * + * @param groupId The ID of the group for which to retrieve the cake diagram information. + * @return A ResponseEntity containing an array of doubles representing the cake diagram information if found, + * or a 404 Not Found response if not found. + */ + @GetMapping("/statistic/cakeGraph/{groupId}") + public ResponseEntity<double[]> getInformationOfCakeGraph(@PathVariable("groupId") long groupId){ + return wasteService.getCakeDiagram(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 a2b089c7391a1bb4826516ca053e3a0041887c6f..8f43267c5e6805f2d2924544274c110db0510b25 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 @@ -10,6 +10,7 @@ import ntnu.idatt2016.v233.SmartMat.entity.product.Product; import ntnu.idatt2016.v233.SmartMat.repository.group.GroupRepository; import ntnu.idatt2016.v233.SmartMat.repository.group.WasteRepository; import ntnu.idatt2016.v233.SmartMat.repository.product.ProductRepository; +import ntnu.idatt2016.v233.SmartMat.util.StatisticUtil; import org.springframework.stereotype.Service; import java.util.Optional; @@ -64,4 +65,16 @@ public class WasteService { return wasteRepository.findAllWasteOfOneCategoryFromGroup(groupId,categoryName); } + /** + * Get the cake diagram for a group of waste. + * + * @param groupId The ID of the group for which to retrieve the cake diagram. + * @return An Optional containing an array of doubles representing the cake diagram for the group, + * or an empty Optional if the group is not found. + */ + public Optional<double[]> getCakeDiagram(long groupId){ + Optional<Group> group = groupRepository.findByGroupId(groupId); + return group.map(value -> StatisticUtil.getNumberOfWasteByCategoryName(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 new file mode 100644 index 0000000000000000000000000000000000000000..4ec0de83f551862e720a27aa4185cf3d83a92d6d --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/StatisticUtil.java @@ -0,0 +1,50 @@ +package ntnu.idatt2016.v233.SmartMat.util; + +import ntnu.idatt2016.v233.SmartMat.entity.Waste; + +import java.sql.Date; +import java.util.List; + + +/** + * A utility class for calculating statistics related to waste and CO2 emissions. + */ +public class StatisticUtil { + + /** + * Get the annual average amount of CO2 emissions per person. + * + * @param co2List The list of CO2 emissions for a group of people. + * @param numberOfPerson The number of people in the group. + * @param firstDate The date on which the group started tracking their CO2 emissions. + * @return The annual average amount of CO2 emissions per person. + */ + private static double getAnnualAveragePerPerson(List<Double> co2List, int numberOfPerson, Date firstDate) { + java.util.Date currentDate = new java.util.Date(); + long diffInMillis = currentDate.getTime() - firstDate.getTime(); + long diffInDays = diffInMillis / (24 * 60 * 60 * 1000); + double co2Sum = 0.0; + for (double c02 : co2List) co2Sum += c02; + return co2Sum / (((double) diffInDays / 365.0) * (double) numberOfPerson); + } + + /** + * Get the number of waste items for each category name in a list of waste items. + * + * @param wastes The list of waste items for which to calculate the number of items by category name. + * @return An array of doubles representing the number of waste items for each category name. + */ + public static double[] getNumberOfWasteByCategoryName(List<Waste> wastes) { + double[] numberOfWastes = new double[5]; + for (Waste waste : wastes) { + switch (waste.getEan().getCategory().getCategoryName()) { + case "meat, fish and chicken" -> numberOfWastes[0] += waste.getAmount(); + case "baked goods and grains" -> numberOfWastes[1] += waste.getAmount(); + case "dairy and egg" -> numberOfWastes[2] += waste.getAmount(); + case "fruit and vegetables" -> numberOfWastes[3] += waste.getAmount(); + default -> numberOfWastes[4] += waste.getAmount(); + } + } + return numberOfWastes; + } +} \ No newline at end of file