import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.List; import java.util.Scanner; public class ReportGenerator { private final String ordersFilePath; private final List productList; private double totalFinalCost; private int totalQuantity; private double totalDiscountValue; private int totalInvalidLines; public ReportGenerator(String ordersFilePath, List productList) { this.ordersFilePath = ordersFilePath; this.productList = productList; totalFinalCost = 0 ; totalQuantity = 0; totalDiscountValue = 0; totalInvalidLines = 0 ; } public void processFile() throws FileNotFoundException { File file = new File(ordersFilePath); Scanner in = new Scanner(file); while (in.hasNextLine()){ String line = in.nextLine(); String[] lineArray = line.split(","); int productId; int quantity; int discountPercent; try { productId = Integer.parseInt(lineArray[0]); }catch (NumberFormatException e){ totalInvalidLines += 1; continue; } if (productId <= 0 ) { totalInvalidLines +=1; continue; } try { quantity = Integer.parseInt(lineArray[1]); }catch (NumberFormatException e){ totalInvalidLines += 1; continue; } if (quantity <= 0 ) { totalInvalidLines +=1; continue; } try { discountPercent = Integer.parseInt(lineArray[2]); }catch (NumberFormatException e){ totalInvalidLines +=1 ; continue; } if (discountPercent < 0 || discountPercent> 99 ) { totalInvalidLines +=1; continue; } boolean productIdExist = false; Product pro = null ; for (Product product : productList) { if (product.getProductID() == productId) { pro = product; productIdExist = true; break; } } if (!productIdExist){ totalInvalidLines += 1 ; continue; } double subtotal = quantity * pro.getPrice(); double discountValue = subtotal * ((double) discountPercent / 100); double finalCost = subtotal - discountValue; totalQuantity += quantity; totalFinalCost += finalCost; totalDiscountValue += discountValue; } in.close(); } public void saveReport() throws IOException { String report = ""; report += ("========= SALES REPORT ========="+"\n"); report += ("Total Quantity Sold: "+totalQuantity+"\n"); report += ("Total Final Cost: $"+totalFinalCost+"\n"); report += ("Total Discount Value: $"+totalDiscountValue+"\n"); report += ("Invalid Rows: "+totalInvalidLines+"\n"); report += ("================================"+"\n"); File file = new File("report.txt"); FileWriter fileWriter = new FileWriter(file); fileWriter.write(report); fileWriter.close(); } }