Skip to content
Snippets Groups Projects
Commit 958c012a authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Endpoint to add lost money was added

parent f70d3360
No related branches found
No related tags found
No related merge requests found
......@@ -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());
}
}
......@@ -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()));
}
}
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment