From 54c46eea29f77e5d5b1ebbeb1afb31f85c8f61f3 Mon Sep 17 00:00:00 2001 From: Fatemesadat Mirabootalebi Date: Mon, 18 May 2026 04:16:38 -0700 Subject: [PATCH] implement exercises. Add optional product catalog export --- product_catalog.txt | 11 +++++ src/main/java/Main.java | 18 +++++++ src/main/java/ReportGenerator.java | 75 +++++++++++++++++++++++++++++ structure.txt | Bin 0 -> 1114 bytes 4 files changed, 104 insertions(+) create mode 100644 product_catalog.txt create mode 100644 structure.txt diff --git a/product_catalog.txt b/product_catalog.txt new file mode 100644 index 0000000..c3cdf59 --- /dev/null +++ b/product_catalog.txt @@ -0,0 +1,11 @@ +===== PRODUCT CATALOG ===== +1,Apple iPhone 16,999.99 +2,Samsung Galaxy S24,899.99 +3,Google Pixel 7,799.99 +4,OnePlus 11,699.99 +5,Sony WH-1000XM5 Headphones,349.99 +6,Apple MacBook Air,1199.99 +7,Dell XPS 13 Laptop,1099.99 +8,Amazon Echo Dot,49.99 +9,Fitbit Charge 5,129.99 +=========================== diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..5005d15 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -24,6 +24,21 @@ public class Main { // NOTE // - The data in products.csv is guaranteed to be valid. + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + + String[] elements = line.split(","); + + int productId = Integer.parseInt(elements[0]); + String name = elements[1]; + double price = Double.parseDouble(elements[2]); + + Product product = new Product(productId, name, price); + productCatalog.add(product); + } + + scanner.close(); + } public static void main(String[] args) @@ -42,5 +57,8 @@ public class Main { System.out.println("=== Order file processed"); reportGenerator.saveReport(); System.out.println("=== Report saved as report.txt"); + //additional + reportGenerator.saveProductCatalog(); + System.out.println("=== Product catalog saved"); } } diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..1a0cbbd 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -55,6 +55,50 @@ public class ReportGenerator { // - increment totalInvalidLines // - skip the line and continue processing the next line // 12. Close all file resources + + try(java.util.Scanner sc = new java.util.Scanner(new java.io.File(ordersFilePath))){ + while(sc.hasNextLine()) { + String line = sc.nextLine(); + String[] elements = line.split(","); + + try { + int productId = Integer.parseInt(elements[0]); + int quantity = Integer.parseInt(elements[1]); + int discountPercent = Integer.parseInt(elements[2]); + + if (productId <= 0 || quantity <= 0 || discountPercent < 0 || discountPercent > 99) { + totalInvalidLines++; + continue; + } + + Product foundProduct = null; + + for (Product pro : productList) { + if (pro.getProductID() == productId) { + foundProduct = pro; + break; + } + } + + if (foundProduct == null) { + totalInvalidLines++; + continue; + } + + double subtotal = quantity * foundProduct.getPrice(); + double discountValue = subtotal * discountPercent / 100.0; + double finalCost = subtotal - discountValue; + totalQuantity += quantity; + totalFinalCost += finalCost; + totalDiscountValue += discountValue; + } catch (NumberFormatException e) { + totalInvalidLines++; + } + } + } + catch(java.io.IOException e) { + System.err.println("Error reading order file: " + ordersFilePath); + } } public void saveReport() @@ -79,5 +123,36 @@ public class ReportGenerator { Invalid Rows: 3 ================================ */ + String report = "========= SALES REPORT =========\n" + "Total Quantity Sold: " + totalQuantity + "\n" + "Total Final Cost: $" + String.format("%.2f", totalFinalCost) + "\n" + "Total Discount Value: $" + String.format("%.2f", totalDiscountValue) + "\n" + "Invalid Rows: " + totalInvalidLines + "\n" + "================================"; + + try(java.io.PrintWriter writer = new java.io.PrintWriter(new java.io.FileWriter("report.txt"))) { + writer.println(report); + } + catch(java.io.IOException e) { + System.err.println("Error writing report file"); + } + } + //additional + public void saveProductCatalog() { + try (java.io.PrintWriter writer = + new java.io.PrintWriter(new java.io.FileWriter("product_catalog.txt"))) { + + writer.println("===== PRODUCT CATALOG ====="); + + for (Product product : productList) { + writer.println( + product.getProductID() + "," + + product.getProductName() + "," + + String.format("%.2f", product.getPrice()) + ); + } + + writer.println("==========================="); + + } catch (java.io.IOException e) { + System.err.println("Error writing product catalog file"); + } + } + } \ No newline at end of file diff --git a/structure.txt b/structure.txt new file mode 100644 index 0000000000000000000000000000000000000000..2f67637adaecc66490cbd1ba52ade339f10bd147 GIT binary patch literal 1114 zcmb7D!A^rf5PfG8|AD)aXrl+cX|;N3(lo}CNmGF|C{V&u?7?qW-we!QBc&l^ceb53 zv+uou-=7(_$Pgnzg#ZiO;RVk;Wk}&rb1$&LDNgXJ$Q^QG1LmY6#hS=~82SIb>mro_ zE-}RoW;n++?s0)D?J~tJLOp#GBTvZwsou0NQ=6_*^ITVG> EA6g5M-~a#s literal 0 HcmV?d00001