From 6d6606b1664acc1fbb1688685080fe81ddb557c2 Mon Sep 17 00:00:00 2001 From: emad Date: Fri, 19 Jun 2026 14:36:53 +0430 Subject: [PATCH] implement all TODOs --- src/main/java/Main.java | 10 +++- src/main/java/ReportGenerator.java | 80 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..a7fa5fb 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -14,7 +14,15 @@ public class Main { throw new IOException("products.csv not found"); } Scanner scanner = new Scanner(input); - + while(scanner.hasNextLine()) + { + String line = scanner.nextLine(); + String[] information = line.split(","); + int productId = Integer.parseInt(information[0].trim()); + String productName = information[1].trim(); + double price = Double.parseDouble(information[2].trim()); + productCatalog.add(new Product(productId, productName, price)); + } // 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..504821c 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,3 +1,7 @@ +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; public class ReportGenerator { @@ -55,6 +59,63 @@ public class ReportGenerator { // - increment totalInvalidLines // - skip the line and continue processing the next line // 12. Close all file resources + Path path = Path.of(ordersFilePath); + try + { + List lines = Files.readAllLines(path); + for(String line: lines) + { + String[] information = line.split(","); + int productId; + int quantity; + double discountPercent; + try + { + productId = Integer.parseInt((information[0].trim())); + quantity = Integer.parseInt(information[1].trim()); + discountPercent = Double.parseDouble(information[2].trim()); + } + catch (NumberFormatException e) + { + totalInvalidLines++; + continue; + } + if((productId <= 0) || (quantity <= 0)) + { + totalInvalidLines++; + continue; + } + else if((discountPercent <= 0) || (discountPercent > 99)) + { + totalInvalidLines++; + continue; + } + boolean haveValidId = false; + for (Product product: productList) + { + if(productId == product.getProductID()) + { + haveValidId = true; + double subtotal = quantity * product.getPrice(); + double discountValue = subtotal * discountPercent / 100; + double finalCost = subtotal - discountValue; + totalQuantity += quantity; + totalFinalCost += finalCost; + totalDiscountValue += discountValue; + break; + } + } + if(!haveValidId) + { + totalInvalidLines++; + } + } + } + catch (IOException e) + { + throw new RuntimeException(e); + } + } public void saveReport() @@ -79,5 +140,24 @@ public class ReportGenerator { Invalid Rows: 3 ================================ */ + String report = String.format( + "========= SALES REPORT =========\n" + + "Total Quantity Sold: %d\n" + + "Total Final Cost: $%.2f\n" + + "Total Discount Value: $%.2f\n" + + "Invalid Rows: %d\n" + + "================================", + totalQuantity, + totalFinalCost, + totalDiscountValue, + totalInvalidLines + ); + + Path path = Path.of("report.txt"); + try { + Files.write(path, List.of(report)); + } catch (IOException e) { + System.out.println("Making report file failed!"); + } } } \ No newline at end of file -- 2.54.0