From 54c43e95eef72174d479cad0e0317e42e75cff77 Mon Sep 17 00:00:00 2001 From: Matin Date: Thu, 21 May 2026 03:39:19 +0430 Subject: [PATCH] The code is complete, but it has some bugs that need fixing. --- src/main/java/Main.java | 46 ++---------------- src/main/java/ReportGenerator.java | 78 ++++++++++++++---------------- 2 files changed, 40 insertions(+), 84 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index bb3c24e..2706dbc 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -17,49 +17,13 @@ public class Main { while (scanner.hasNextLine()) { String ln = scanner.nextLine() ; - int part_number = 0 ; - String part = ""; - String product_id = "" ; - String product_name = "" ; - String product_price = "" ; - for (char x : ln.toCharArray() ) - { + String[] part = ln.split(","); + String product_id = part[0] ; + String product_name = part[1] ; + String product_price = part[2] ; - if (x != ',') - { - part = part + x ; - } - else if (x == ',') - { - switch (part_number) - { - case 0 : - product_id = part ; - part = "" ; - part_number ++ ; - break; + productCatalog.add(new Product(Integer.parseInt(product_id), product_name , Float.parseFloat(product_price))) ; - case 1 : - product_name = part ; - part = "" ; - part_number ++ ; - break; - - case 2 : - product_price = part ; - part = "" ; - part_number ++ ; - break; - - - } - } - - productCatalog.add(new Product(Integer.parseInt(product_id), product_name , Float.parseFloat(product_price))) ; - - - - } } // TODO: diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 04f9df5..8467383 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,3 +1,5 @@ +import java.io.BufferedWriter; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.List; @@ -27,53 +29,20 @@ public class ReportGenerator { } public void processFile() throws IOException { - InputStream input = ReportGenerator.class - .getClassLoader() - .getResourceAsStream("products.csv"); + InputStream input = getClass().getClassLoader().getResourceAsStream (ordersFilePath); if (input == null) { - throw new IOException("products.csv not found"); + throw new IOException("not found"); } Scanner scanner = new Scanner(input); while (scanner.hasNextLine()) { String ln = scanner.nextLine() ; - int part_number = 0 ; - String part = ""; - String product_id = "" ; - String product_number = "" ; - String product_discount = "" ; - for (char x : ln.toCharArray() ) - { - - if (x != ',') - { - part = part + x ; - } - else if (x == ',') - { - switch (part_number) - { - case 0 : - product_id = part ; - part = "" ; - part_number ++ ; - break; - - case 1 : - product_number = part ; - part = "" ; - part_number ++ ; - break; - - case 2 : - product_discount = part ; - part = "" ; - part_number ++ ; - break; + String[] part = ln.split(",") ; + String product_id = part[0] ; + String product_number = part[1] ; + String product_discount = part[2] ; - } - } if (isNumber(product_id) && isNumber(product_number ) && isNumber(product_discount)) { @@ -97,6 +66,17 @@ public class ReportGenerator { totalInvalidLines ++ ; continue; } + for (Product p : productList) + { + if (p.getProductID() == id) + { + totalFinalCost = totalFinalCost + ((p.getPrice()*(100-discount)/100)*number) ; + totalDiscountValue = totalDiscountValue + ((p.getPrice()*discount/100)*number); + } + + } + totalQuantity ++ ; + } @@ -151,8 +131,22 @@ public class ReportGenerator { // 12. Close all file resources } - public void saveReport() - { + public void saveReport() throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter("report.txt")) ; + writer.write("========= SALES REPORT ========="); + writer.newLine(); + writer.write("Total Quantity Sold:" + totalQuantity); + writer.newLine(); + writer.write("Total Final Cost: $" + totalFinalCost); + writer.newLine(); + writer.write("Total Discount Value: $" + totalDiscountValue); + writer.newLine(); + writer.write("Invalid Rows: " + totalInvalidLines); + writer.newLine(); + writer.close(); + + + // TODO: // 1. Build a formatted report string // 2. Include: @@ -176,5 +170,3 @@ public class ReportGenerator { } } - public void saveReport() { - } \ No newline at end of file