Last time, we learned how to handle business logic in a multi-layer application. Specifically, we created a method for selling lemonades by placing an order. In this tutorial we will cover the final part of our requirements: generating reports. To achieve this, we will introduce the concept of Data Transfer Objects (DTOs) and use them to implement our requirements.
Let’s revisit the problem:
…
At the end of the day, we need to do some reporting. First, we should be able to generate a daily sales report and secondly, we need to identify which products have low stock levels.
…
If we analyze the problem, we can see that we have all the data needed to generate these reports. We should add two more options to our menu: the first – to display all the orders created today and the second – to display the products with an empty stock.
To implement this, we need to introduce the concept of DTO.
A DTO (Data Transfer Object) is a simple object used to transfer data between different layers or components of a software application. It is an object created by combining attributes from different entities. In our case, we need to create two DTO classes to transfer data from the Service Layer to the UI Layer.
Let’s create the object for the first report which should contain the date and the total sale value. In a new package called dtos we add the class DailySalesDTO.
public class DailySalesDTO {
private Date day;
private int totalSales;
public DailySalesDTO(Date day, int totalSales) {
this.day = day;
this.totalSales = totalSales;
}
public Date getDay() {
return day;
}
public void setDay(Date day) {
this.day = day;
}
public int getTotalSales() {
return totalSales;
}
public void setTotalSales(int totalSales) {
this.totalSales = totalSales;
}
public String getDayString() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dateString = dateFormat.format(day);
return dateString;
}
}
In the Service Layer, we need to collect all the data for the report. To achieve this, we need to add a method in OrderService:
public List getDailyReport(){
List report = new ArrayList<>();
Iterable orders = orderRepository.findAll();
for(Order order: orders){
Date date = order.getDate();
boolean alreadyAddedDay = false;
for(DailySalesDTO day: report){
if(day.getDay().equals(date)){
alreadyAddedDay = true;
day.setTotalSales(day.getTotalSales() + order.getFinalPrice());
}
}
if(!alreadyAddedDay){
report.add(new DailySalesDTO(order.getDate(), order.getFinalPrice()));
}
}
return report;
}
Finally, we need to update the UI to include our new functionality:
public void runDailyReportOption() {
System.out.println("You want to create a daily report.");
List report = orderService.getDailyReport();
for(DailySalesDTO day: report){
String reportLine = "For the day %s the total value of the sells is %d.";
String reportLineFormatted = String.format(reportLine, day.getDayString(), day.getTotalSales());
System.out.println(reportLineFormatted);
}
}
Create the second report. If we revisit the problem: “we need to see if we have products with stocks that are empty.”
You should create a DTO containing the product name, supplier name and supplier email and populate the report with this data if the product is out of stock.
In this tutorial we have learned how to create Data Transfer Objects and generate reports using them. In the final step of our journey, we will summarize the entire chapter, discuss the next steps for a software developer, and provide some similar problems for you to practice on your own.
Learn advanced concepts, work on real-world projects, and fast-track your journey to becoming a proficient Java developer. Start now and unlock your full potential in the world of Java programming!
Start now and unlock your full potential in the world of Java programming!
The place where you can start your Java journey.
© All Rights Reserved.