From c328e8d6b292b9b5f72fe7fe8fb57e65302ae4e7 Mon Sep 17 00:00:00 2001 From: Arshida_0 Date: Tue, 19 May 2026 00:32:40 +0430 Subject: [PATCH] HW 06 --- src/main/java/Main.java | 17 +++++++- src/main/java/ReportGenerator.java | 62 ++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..ef3ac8f 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,3 +1,5 @@ +import java.io.BufferedReader; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -6,7 +8,7 @@ import java.util.Scanner; public class Main { static ArrayList productCatalog = new ArrayList<>(); - public static void loadProducts() throws IOException{ + public static void loadProducts() throws IOException { InputStream input = Main.class .getClassLoader() .getResourceAsStream("products.csv"); @@ -15,7 +17,7 @@ public class Main { } Scanner scanner = new Scanner(input); - // TODO: + // TODO: . // - Read each line from products.csv // - For each line, parse productId, name, and price // - The format of the file is like this: @@ -24,6 +26,17 @@ public class Main { // NOTE // - The data in products.csv is guaranteed to be valid. + + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] fields = line.split(","); + int id = Integer.parseInt(fields[0]); + String name = fields[1]; + double price = Double.parseDouble(fields[2]); + Product product = new Product(id, name, price); + productCatalog.add(product); + } + scanner.close(); } public static void main(String[] args) diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..8708934 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,3 +1,4 @@ +import java.io.*; import java.util.List; public class ReportGenerator { @@ -55,6 +56,55 @@ public class ReportGenerator { // - increment totalInvalidLines // - skip the line and continue processing the next line // 12. Close all file resources + + + + try (FileReader fileReader = new FileReader(ordersFilePath)) { + BufferedReader bufferedReader = new BufferedReader(fileReader); + String line; + while ((line = bufferedReader.readLine()) != null) { + String[] fields = line.split(","); + try { + int productId_rd = Integer.parseInt(fields[0]); + int quantity_rd = Integer.parseInt(fields[1]); + int discountPercent_rd = Integer.parseInt(fields[2]); + + if ((productId_rd <= 0)||(quantity_rd <= 0)||(discountPercent_rd < 0)||(discountPercent_rd > 99)) { + totalInvalidLines++; + continue; + } + + boolean isTheId = false; + + for (Product p : productList) { + if (p.getProductID() == productId_rd) { + double subtotal = quantity_rd * p.getPrice(); + double discountValue = subtotal * discountPercent_rd / 100; + double finalCost = subtotal - discountValue; + totalFinalCost += finalCost; + totalQuantity += quantity_rd; + totalDiscountValue += discountValue; + isTheId = true; + break; + } + } + + if (!isTheId) { + totalInvalidLines++; + } + } + catch (NumberFormatException e) { + totalInvalidLines++; + } + } + bufferedReader.close(); + } + catch (IOException e) { + System.out.println("Can't read " + ordersFilePath); + System.out.println(e.getMessage()); + } + + } public void saveReport() @@ -79,5 +129,17 @@ public class ReportGenerator { Invalid Rows: 3 ================================ */ + + String report_format = "========= SALES REPORT =========\nTotal Quantity Sold: %d\nTotal Final Cost: %.4f\nTotal Discount Value: %.4f\nInvalid Rows: %d\n================================\n"; + String report_str = String.format(report_format, totalQuantity, totalFinalCost, totalDiscountValue, totalInvalidLines); + + try (FileWriter fw = new FileWriter("report.txt", true)) { + BufferedWriter bw = new BufferedWriter(fw); + bw.write(report_str); + bw.close(); + } + catch (IOException e) { + System.out.println(e.getMessage()); + } } } \ No newline at end of file