From 5111e30762aed587de39c19bb9b7ebb3ed10e3b4 Mon Sep 17 00:00:00 2001 From: Mobina Date: Sat, 23 May 2026 03:10:30 +0330 Subject: [PATCH] HW-06 --- src/main/java/Main.java | 17 +++++++ src/main/java/ReportGenerator.java | 76 ++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..d16c1e7 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,7 +1,10 @@ import java.io.IOException; import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Scanner; public class Main { @@ -15,6 +18,20 @@ public class Main { } Scanner scanner = new Scanner(input); + int productId=0; + String name=" "; + double price=0; + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] parts = line.split(","); + productId = Integer.parseInt(parts[0]); + price= Double.parseDouble(parts[2]); + Product p= new Product(productId, parts[1],price); + productCatalog.add(p); + } + + // TODO: // - Read each line from products.csv // - For each line, parse productId, name, and price diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..f29ffb8 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,4 +1,7 @@ import java.util.List; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.io.IOException; public class ReportGenerator { @@ -17,6 +20,54 @@ public class ReportGenerator { public void processFile() { + try { + List lines = Files.readAllLines(Paths.get(ordersFilePath)); + + for(String line: lines) + { + String[] parts = line.split(","); + int id=0; + int quantity=0; + int discountPercent=0; + double subtotal=0; + double discountValue=0; + double finalCost=0; + + try { + id = Integer.parseInt(parts[0]); + quantity=Integer.parseInt(parts[1]); + discountPercent=Integer.parseInt(parts[2]); + + if(id<=0 || quantity<=0 || discountPercent<0 || discountPercent>99) { + totalDiscountValue++; + continue; + } + if(!exist(id, productList)) { + totalInvalidLines++; + continue; + } + + for(Product p:productList) + { + if(id== p.getProductID()) + { + subtotal = quantity * p.getPrice(); + discountValue = subtotal * discountPercent / 100; + finalCost = subtotal - discountValue; + } + } + + totalQuantity += quantity; + totalFinalCost += finalCost; + } + catch (NumberFormatException e) { + continue; + } + } + } + catch (IOException e) { + e.printStackTrace(); + } // TODO: // 1. Open the order details CSV file // 2. Read the file line by line @@ -57,8 +108,33 @@ public class ReportGenerator { // 12. Close all file resources } + public boolean exist(int id, List productList) + { + for(Product p:productList) + { + if(id== p.getProductID()) + return true; + } + return false; + } + public void saveReport() { + String report = "========= SALES REPORT =========\n"; + report += "Total Quantity Sold: " + totalQuantity + "\n"; + report += "Total Final Cost: $" + String.format("%.2f", totalFinalCost) + "\n"; + report += "Total Discount Value: $" + String.format("%.2f", totalDiscountValue) + "\n"; + report += "Invalid Rows: " + totalInvalidLines + "\n"; + report += "================================\n"; + + try { + Files.writeString(Paths.get("report.txt"), report); + System.out.println("Report saved successfully!"); + } catch (IOException e) { + System.err.println("Error saving report: " + e.getMessage()); + e.printStackTrace(); + + } // TODO: // 1. Build a formatted report string // 2. Include: -- 2.54.0