From d8c191e367cb9f932f693b894ded489680204372 Mon Sep 17 00:00:00 2001 From: Saeed Jamali Date: Sat, 16 May 2026 18:56:36 +0330 Subject: [PATCH 1/2] Implement open file fucntionality --- src/main/java/ReportGenerator.java | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..424a87a 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,4 +1,8 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; import java.util.List; +import java.util.Scanner; public class ReportGenerator { @@ -17,6 +21,30 @@ public class ReportGenerator { public void processFile() { + try { + Scanner scanner = new Scanner(new File("products.csv")); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] parts = line.split(","); + int productID = Integer.parseInt(parts[0]); + String productName = parts[1]; + double price = Double.parseDouble(parts[2]); + Product product = new Product(productID, productName, price); + productList.add(product); + } + } catch (FileNotFoundException e) { + System.out.println("File not found."); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + try { + File source = new File(ordersFilePath); + Scanner scanner = new Scanner(source); + + } catch (FileNotFoundException e) { + System.out.println("File not found."); + } // TODO: // 1. Open the order details CSV file // 2. Read the file line by line @@ -59,6 +87,21 @@ public class ReportGenerator { public void saveReport() { + try { + File file = new File("report.txt"); + PrintWriter printWriter = new PrintWriter(file); + printWriter.write("========= SALES REPORT =========\n"); + printWriter.write("Total Quantity Sold: "+ totalQuantity + "\n"); + printWriter.write("Total Final Cost: "+ totalFinalCost + "\n"); + printWriter.write("Total Discount Value: "+ totalDiscountValue + "\n"); + printWriter.write("================================"); + printWriter.close(); + } catch (FileNotFoundException e) { + System.out.println("File not found."); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + // TODO: // 1. Build a formatted report string // 2. Include: From 5aaddf01467d0a556d92112a8be5d6a09c8b88c2 Mon Sep 17 00:00:00 2001 From: Saeed Jamali Date: Fri, 22 May 2026 17:09:03 +0330 Subject: [PATCH 2/2] Implement Report Generator --- src/main/java/Main.java | 12 ++++- src/main/java/ReportGenerator.java | 86 +++++++++++++++++++++--------- 2 files changed, 72 insertions(+), 26 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..ac451d2 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,3 +1,5 @@ +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -14,7 +16,15 @@ public class Main { throw new IOException("products.csv not found"); } Scanner scanner = new Scanner(input); - + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] parts = line.split(","); + int productID = Integer.parseInt(parts[0].trim()); + String productName = parts[1].trim(); + double price = Double.parseDouble(parts[2].trim()); + Product product = new Product(productID, productName, price); + productCatalog.add(product); + } // 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 424a87a..2e27a6d 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,5 +1,6 @@ import java.io.File; import java.io.FileNotFoundException; +import java.io.IOException; import java.io.PrintWriter; import java.util.List; import java.util.Scanner; @@ -21,30 +22,64 @@ public class ReportGenerator { public void processFile() { - try { - Scanner scanner = new Scanner(new File("products.csv")); - while (scanner.hasNextLine()) { - String line = scanner.nextLine(); - String[] parts = line.split(","); - int productID = Integer.parseInt(parts[0]); - String productName = parts[1]; - double price = Double.parseDouble(parts[2]); - Product product = new Product(productID, productName, price); - productList.add(product); - } - } catch (FileNotFoundException e) { - System.out.println("File not found."); - } catch (Exception e) { - System.out.println(e.getMessage()); - } - + //opening orders and doing the math try { File source = new File(ordersFilePath); Scanner scanner = new Scanner(source); - + + //reading line by line + while (scanner.hasNextLine()) { + int productId; + int count; + int discountPercent; + String line = scanner.nextLine(); + String[] parts = line.split(","); + try { + productId = Integer.parseInt(parts[0].trim()); + if (productId < 1 || productId > productList.size()) { + totalInvalidLines++; + continue; + } + + } catch (NumberFormatException e) { + totalInvalidLines++; + continue; + } + try { + count = Integer.parseInt(parts[1].trim()); + if(count < 1) { + totalInvalidLines++; + continue; + } + } catch (NumberFormatException e) { + totalInvalidLines++; + continue; + } + try { + discountPercent = Integer.parseInt(parts[2].trim()); + if (discountPercent < 0 || discountPercent > 99) { + totalInvalidLines++; + continue; + } + } catch (NumberFormatException e) { + totalInvalidLines++; + continue; + } + + //computing the results + double cost = count * productList.get(productId - 1).getPrice(); + double discount = count * productList.get(productId - 1).getPrice() * discountPercent / 100; + totalFinalCost += cost - discount; + totalDiscountValue += discount; + totalQuantity += count; + + } + scanner.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); - } + } catch (IOException e) { + System.out.println(e.getMessage()); + } // TODO: // 1. Open the order details CSV file // 2. Read the file line by line @@ -90,15 +125,16 @@ public class ReportGenerator { try { File file = new File("report.txt"); PrintWriter printWriter = new PrintWriter(file); - printWriter.write("========= SALES REPORT =========\n"); - printWriter.write("Total Quantity Sold: "+ totalQuantity + "\n"); - printWriter.write("Total Final Cost: "+ totalFinalCost + "\n"); - printWriter.write("Total Discount Value: "+ totalDiscountValue + "\n"); - printWriter.write("================================"); + printWriter.println("========= SALES REPORT ========="); + printWriter.println("Total Quantity Sold: " + totalQuantity); + printWriter.printf("Total Final Cost: %.2f\n", totalFinalCost); + printWriter.printf("Total Discount Value: %.2f\n", totalDiscountValue); + printWriter.println("Invalid Lines: " + totalInvalidLines); + printWriter.println("================================"); printWriter.close(); } catch (FileNotFoundException e) { System.out.println("File not found."); - } catch (Exception e) { + } catch (IOException e) { System.out.println(e.getMessage()); }