From 4746189bf25552ba91f0212e8be9b44145cdf098 Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Wed, 20 May 2026 01:54:10 +0330 Subject: [PATCH 1/4] Part 1 completed. --- src/main/java/Main.java | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..4e1ad1b 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collections; +import java.util.List; import java.util.Scanner; public class Main { @@ -15,15 +16,18 @@ public class Main { } Scanner scanner = new Scanner(input); - // TODO: - // - Read each line from products.csv - // - For each line, parse productId, name, and price - // - The format of the file is like this: - // [productId],[name],[price] - // - Store Product objects in the productCatalog ArrayList + List productCatalog = new ArrayList<>(); - // NOTE - // - The data in products.csv is guaranteed to be valid. + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] columns = line.split(","); + + int productId = Integer.parseInt(columns[0]); + String name = columns[1]; + double price = Double.parseDouble(columns[2]); + + productCatalog.add(new Product(productId, name, price)); + } } public static void main(String[] args) -- 2.54.0 From b1c19fde8dd81f2023f7f07db1b1f2974157ff33 Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Wed, 20 May 2026 02:42:02 +0330 Subject: [PATCH 2/4] Part 2 and part 3 completed. --- src/main/java/Main.java | 5 +- src/main/java/Product.java | 3 + src/main/java/ReportGenerator.java | 207 +++++++++++++----- .../main/resources/2025_order_details.csv | 0 4 files changed, 152 insertions(+), 63 deletions(-) rename 2025_order_details.csv => src/main/resources/2025_order_details.csv (100%) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 4e1ad1b..87a80f8 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -16,8 +16,6 @@ public class Main { } Scanner scanner = new Scanner(input); - List productCatalog = new ArrayList<>(); - while (scanner.hasNextLine()) { String line = scanner.nextLine(); String[] columns = line.split(","); @@ -30,8 +28,7 @@ public class Main { } } - 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/Product.java b/src/main/java/Product.java index dfc6aee..770fc46 100644 --- a/src/main/java/Product.java +++ b/src/main/java/Product.java @@ -9,6 +9,8 @@ public class Product { this.price = price; } + + public int getProductID() { return productID; } @@ -20,4 +22,5 @@ public class Product { public double getPrice() { return price; } + } diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..5528e8c 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,4 +1,9 @@ +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.PrintWriter; import java.util.List; +import java.util.Scanner; public class ReportGenerator { @@ -13,71 +18,155 @@ public class ReportGenerator { public ReportGenerator(String ordersFilePath, List productList) { this.ordersFilePath = ordersFilePath; this.productList = productList; + this.totalFinalCost = 0.0; + this.totalQuantity = 0; + this.totalDiscountValue = 0.0; + this.totalInvalidLines = 0; } - public void processFile() - { - // TODO: - // 1. Open the order details CSV file - // 2. Read the file line by line - // 3. Split each line using comma delimiter - // (each line is guaranteed to contain exactly two commas) - // 4. Each line contains exactly 3 values in the format: - // [productId],[quantity],[discountPercent] - // 5. Attempt to convert values: - // - productId -> int - // - quantity -> int - // - discountPercent -> int - // NOTE: - // - quantity and discountPercent may contain invalid characters - // (e.g., letters instead of numbers). - // - If parsing any value fails (NumberFormatException), - // the entire line must be considered invalid. - // - Use try-catch blocks and exception handling to safely - // handle parsing and validation errors without stopping - // the processing of the remaining lines. - // 6. Validate parsed values: - // - productId must be greater than 0 - // - quantity must be greater than 0 - // - discountPercent must be between 0 and 99 inclusive - // 7. Look for a Product in productList that matches productId - // 8. If no Product with that ID exists in the catalog, - // the entire line is considered invalid - // 9. If all values are valid and the product exists, calculate: - // subtotal = quantity * product price - // discountValue = subtotal * discountPercent / 100 - // finalCost = subtotal - discountValue - // 10. Update report totals: - // - totalQuantity += quantity - // - totalFinalCost += finalCost - // - totalDiscountValue += discountValue - // 11. If any parsing error, validation failure, or missing product occurs: - // - increment totalInvalidLines - // - skip the line and continue processing the next line - // 12. Close all file resources + public void processFile() throws IOException { + Scanner scanner = null; + InputStream input = null; + + try { + input = Main.class + .getClassLoader() + .getResourceAsStream("2025_order_details.csv"); + if (input == null) { + throw new IOException("2025_order_details.csv not found"); + } + scanner = new Scanner(input); + int lineNumber = 0; + + while (scanner.hasNextLine()) { + lineNumber++; + String line = scanner.nextLine(); + + String[] columns = line.split(","); + + int productId = 0; + int quantity = 0; + int discountPercent = 0; + boolean isValid = true; + String errorReason = ""; + + //Validation and parsing productId + try { + productId = Integer.parseInt(columns[0]); + if (productId <= 0) { + isValid = false; + errorReason = "productId must be > 0 (got " + productId + ")"; + } + } catch (NumberFormatException e) { + isValid = false; + errorReason = "productId is not a valid integer ('" + columns[0] + "')"; + } + + //Validation and parsing quantity (if productId was valid) + if (isValid) { + try { + quantity = Integer.parseInt(columns[1]); + if (quantity <= 0) { + isValid = false; + errorReason = "quantity must be > 0 (got " + quantity + ")"; + } + } catch (NumberFormatException e) { + isValid = false; + errorReason = "quantity is not a valid integer ('" + columns[1] + "')"; + } + } + + //Validation and parsing discountPercent (if productId and quantity were valid) + if (isValid) { + try { + discountPercent = Integer.parseInt(columns[2].trim()); + if (discountPercent < 0 || discountPercent > 99) { + isValid = false; + errorReason = "discountPercent must be between 0-99 (got " + discountPercent + ")"; + } + } catch (NumberFormatException e) { + isValid = false; + errorReason = "discountPercent is not a valid integer ('" + columns[2] + "')"; + } + } + + //Skipping the line if no Product with that ID exists in the catalog + Product product = null; + if (isValid) { + product = findProductById(productId); + if (product == null) { + isValid = false; + errorReason = "Product with ID " + productId + " not found in catalog"; + } + } + if (!isValid) { + System.out.println("Line " + lineNumber + " SKIPPED: " + errorReason); + totalInvalidLines++; + continue; + } + + double productPrice = product.getPrice(); + double subtotal = quantity * productPrice; + double discountValue = subtotal * discountPercent / 100; + double finalCost = subtotal - discountValue; + + totalQuantity += quantity; + totalDiscountValue += discountValue; + totalFinalCost += finalCost; + } + } catch (IOException e) { + System.err.println("Error reading file: " + e.getMessage()); + throw e; + } finally { + if (scanner != null) { + scanner.close(); + } + if (input != null) { + try { + input.close(); + } catch (IOException e) { + System.err.println("Error closing input stream: " + e.getMessage()); + } + } + } + } + + private Product findProductById(int productId) { + for (Product product : productList) { + if (product.getProductID() == productId) { + return product; + } + } + return null; } public void saveReport() { - // TODO: - // 1. Build a formatted report string - // 2. Include: - // - total quantity - // - total final cost - // - total discount value - // - total invalid lines - // 3. Create/open output report file (name it report.txt) - // 4. Write report string into file - // 5. Handle file writing exceptions - // 6. Close writer resources + PrintWriter writer = null; - /* Example Structure - ========= SALES REPORT ========= - Total Quantity Sold: 42 - Total Final Cost: $1520.75 - Total Discount Value: $230.50 - Invalid Rows: 3 - ================================ - */ + try { + StringBuilder report = new StringBuilder(); + report.append("========= SALES REPORT =========\n"); + report.append(String.format("Total Quantity Sold: %d\n", totalQuantity)); + report.append(String.format("Total Final Cost: $%.2f\n", totalFinalCost)); + report.append(String.format("Total Discount Value: $%.2f\n", totalDiscountValue)); + report.append(String.format("Invalid Rows: %d\n", totalInvalidLines)); + report.append("================================\n"); + + String outputFileName = "report.txt"; + writer = new PrintWriter(new FileWriter(outputFileName)); + + writer.print(report.toString()); + + System.out.println("Report saved successfully to: " + outputFileName); + System.out.println("\n" + report.toString()); + } catch (IOException e) { + System.err.println("Error writing report to file: " + e.getMessage()); + e.printStackTrace(); + } finally { + if (writer != null) { + writer.close(); + } + } } } \ No newline at end of file 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 -- 2.54.0 From becdc1f9038785a6bac6e6338fda42ad7b637fb4 Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Wed, 20 May 2026 02:42:57 +0330 Subject: [PATCH 3/4] The main class tested. --- src/main/java/Main.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 87a80f8..a307d9d 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -27,6 +27,7 @@ public class Main { productCatalog.add(new Product(productId, name, price)); } } + public static void main(String[] args) throws IOException { try { -- 2.54.0 From 29cd14a0505485dbfc53cfc0e7a0999758a3a5cc Mon Sep 17 00:00:00 2001 From: Zahra Gharagozloo Mazlaghan Date: Wed, 20 May 2026 02:45:39 +0330 Subject: [PATCH 4/4] The project completed. --- src/main/java/Main.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index a307d9d..6b4e0bc 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -27,7 +27,7 @@ public class Main { productCatalog.add(new Product(productId, name, price)); } } - + public static void main(String[] args) throws IOException { try { -- 2.54.0