From 151d9feb70efc8e9da232e0c5c7555299ae50b0e Mon Sep 17 00:00:00 2001 From: mahdiLenovaF Date: Wed, 20 May 2026 09:00:25 +0330 Subject: [PATCH 1/2] do loadproduct --- src/main/java/Main.java | 18 +++++++ src/main/java/ReportGenerator.java | 77 ++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..a502653 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -2,10 +2,14 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; +import java.util.InputMismatchException; import java.util.Scanner; public class Main { + static ArrayList productCatalog = new ArrayList<>(); + + public static void loadProducts() throws IOException{ InputStream input = Main.class .getClassLoader() @@ -15,6 +19,20 @@ public class Main { } Scanner scanner = new Scanner(input); + while (scanner.hasNextLine()){ + String line = scanner.nextLine(); + String[] lines = line.split(","); + + int id = Integer.parseInt(lines[0]); + String name = lines[1]; + double price = Double.parseDouble(lines[2]); + + Product product = new Product(id , name , 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 8eb458b..2eb0ac5 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,65 @@ public class ReportGenerator { public void processFile() { + File inputfile = new File(ordersFilePath); + Scanner input = null; + try { + input = new Scanner(inputfile); + } catch (FileNotFoundException e) { + System.out.println("there is no csv file to open it");; + } + + while (input.hasNextLine()){ + + input.useDelimiter(","); + + try{ + int id = input.nextInt(); + int quantity = input.nextInt(); + int discount = input.nextInt(); + if(quantity<0 || discount<0 || discount>= 100 || id <= 0 || id > 9){ + totalInvalidLines++; + continue; + } + + double priceOfproduct = 0; + for (Product p : productList){ + if(id == p.getProductID()){ + priceOfproduct = p.getPrice(); + } + } + + if(priceOfproduct == 0){ + totalInvalidLines++; + continue; + } + + double subtotal = quantity * priceOfproduct; + double discountValue = subtotal * discount / 100 ; + double finalCost = subtotal - discountValue; + + + totalQuantity += quantity; + totalFinalCost += finalCost; + totalDiscountValue += discountValue; + + + + + + + + + }catch (NumberFormatException e){ + totalInvalidLines++; + continue; + } + + + + } + + input.close(); // TODO: // 1. Open the order details CSV file // 2. Read the file line by line @@ -59,6 +122,20 @@ public class ReportGenerator { public void saveReport() { + + + try { + PrintWriter report = new PrintWriter("Report.txt"); + report.println("=======REPORT========"); + report.println("Totsl Quantity sold :" + totalQuantity); + report.println("Toal final cost: $" + totalFinalCost); + report.println("TOtal discount value : $" + totalDiscountValue); + report.println("Invalid rows: " + totalInvalidLines); + report.println("====================="); + + } catch (FileNotFoundException e) { + throw new RuntimeException(e); + } // TODO: // 1. Build a formatted report string // 2. Include: -- 2.54.0 From 9c940c1cfbb495a4c429944f80f245e3cf77e388 Mon Sep 17 00:00:00 2001 From: mahdiLenovaF Date: Wed, 20 May 2026 09:41:00 +0330 Subject: [PATCH 2/2] complet project --- src/main/java/Main.java | 2 +- src/main/java/ReportGenerator.java | 48 +++++++++++++++--------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a502653..49b8fd1 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -29,7 +29,7 @@ public class Main { Product product = new Product(id , name , price); productCatalog.add(product); - + } diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 2eb0ac5..500f0f7 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -9,10 +9,10 @@ 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; @@ -26,38 +26,42 @@ public class ReportGenerator { try { input = new Scanner(inputfile); } catch (FileNotFoundException e) { - System.out.println("there is no csv file to open it");; + System.out.println("We cannot open csv file");; } while (input.hasNextLine()){ - input.useDelimiter(","); + try{ - int id = input.nextInt(); - int quantity = input.nextInt(); - int discount = input.nextInt(); - if(quantity<0 || discount<0 || discount>= 100 || id <= 0 || id > 9){ + String line = input.nextLine(); + String[] data = line.split(","); + int id = Integer.parseInt(data[0]); + int quantity = Integer.parseInt(data[1]); + int discount = Integer.parseInt(data[2]); + + if(quantity <= 0 || discount<0 || discount>= 100 || id <= 0 || id > 9){ totalInvalidLines++; continue; } - double priceOfproduct = 0; - for (Product p : productList){ - if(id == p.getProductID()){ - priceOfproduct = p.getPrice(); + Product myProduct = null; + for (Product p : productList) { + if (p.getProductID() == id) { + myProduct = p; + break; } } - if(priceOfproduct == 0){ + if (myProduct == null) { totalInvalidLines++; continue; } - double subtotal = quantity * priceOfproduct; - double discountValue = subtotal * discount / 100 ; - double finalCost = subtotal - discountValue; + double subtotal = quantity * myProduct.getPrice(); + double discountValue = subtotal * (discount / 100.0); + double finalCost = subtotal - discountValue; totalQuantity += quantity; totalFinalCost += finalCost; @@ -65,11 +69,6 @@ public class ReportGenerator { - - - - - }catch (NumberFormatException e){ totalInvalidLines++; continue; @@ -129,9 +128,10 @@ public class ReportGenerator { report.println("=======REPORT========"); report.println("Totsl Quantity sold :" + totalQuantity); report.println("Toal final cost: $" + totalFinalCost); - report.println("TOtal discount value : $" + totalDiscountValue); + report.println("Total discount value : $" + totalDiscountValue); report.println("Invalid rows: " + totalInvalidLines); report.println("====================="); + report.close(); } catch (FileNotFoundException e) { throw new RuntimeException(e); -- 2.54.0