From b3918fd47ff2f7d1a984121b69cdfd0891fc2a38 Mon Sep 17 00:00:00 2001 From: Matin Date: Thu, 21 May 2026 02:26:19 +0430 Subject: [PATCH 1/3] I fixed the main part and part of the report. --- src/main/java/Main.java | 50 ++++++++++++- src/main/java/ReportGenerator.java | 111 +++++++++++++++++++++++++++-- 2 files changed, 152 insertions(+), 9 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..bb3c24e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -14,6 +14,53 @@ public class Main { throw new IOException("products.csv 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_name = "" ; + String product_price = "" ; + 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_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: // - Read each line from products.csv @@ -26,8 +73,7 @@ public class Main { // - The data in products.csv is guaranteed to be valid. } - public static void main(String[] args) - { + public static void main(String[] args) throws IOException { try { loadProducts(); } catch (IOException e) { diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..04f9df5 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,22 +1,116 @@ +import java.io.IOException; +import java.io.InputStream; 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; + private double totalFinalCost = 0; + private int totalQuantity = 0 ; + private double totalDiscountValue = 0 ; + private int totalInvalidLines = 0 ; public ReportGenerator(String ordersFilePath, List productList) { this.ordersFilePath = ordersFilePath; this.productList = productList; } + public static boolean isNumber(String str) { + try { + Integer.parseInt(str); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + public void processFile() throws IOException { + InputStream input = ReportGenerator.class + .getClassLoader() + .getResourceAsStream("products.csv"); + if (input == null) { + throw new IOException("products.csv 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; + + + } + } + + if (isNumber(product_id) && isNumber(product_number ) && isNumber(product_discount)) + { + int id = Integer.parseInt(product_id) ; + int number = Integer.parseInt(product_number) ; + int discount = Integer.parseInt(product_discount) ; + + if (id < 1 || id > 9 ) + { + totalInvalidLines ++ ; + continue; + + } + if (number < 1) + { + totalInvalidLines ++ ; + continue; + } + if (discount >= 100 || discount < 0 ) + { + totalInvalidLines ++ ; + continue; + } + + + } + else { + + totalInvalidLines ++ ; + continue; + } + + + + + } - public void processFile() - { // TODO: // 1. Open the order details CSV file // 2. Read the file line by line @@ -80,4 +174,7 @@ public class ReportGenerator { ================================ */ } -} \ No newline at end of file +} + + public void saveReport() { + } \ No newline at end of file From 54c43e95eef72174d479cad0e0317e42e75cff77 Mon Sep 17 00:00:00 2001 From: Matin Date: Thu, 21 May 2026 03:39:19 +0430 Subject: [PATCH 2/3] 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 From 871218f406accc81fdf016944a0ccf97dc512d2a Mon Sep 17 00:00:00 2001 From: Matin Date: Thu, 21 May 2026 03:59:03 +0430 Subject: [PATCH 3/3] the issues were solved . the problem was in file location --- src/main/java/ReportGenerator.java | 2 ++ .../main/resources/2025_order_details.csv | 0 2 files changed, 2 insertions(+) rename 2025_order_details.csv => src/main/resources/2025_order_details.csv (100%) diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8467383..9399c3a 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -143,6 +143,8 @@ public class ReportGenerator { writer.newLine(); writer.write("Invalid Rows: " + totalInvalidLines); writer.newLine(); + writer.write("================================"); + writer.newLine(); writer.close(); diff --git a/2025_order_details.csv b/src/main/resources/2025_order_details.csv similarity index 100% rename from 2025_order_details.csv rename to src/main/resources/2025_order_details.csv