diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..2706dbc 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -14,6 +14,17 @@ public class Main { throw new IOException("products.csv not found"); } Scanner scanner = new Scanner(input); + while (scanner.hasNextLine()) + { + String ln = scanner.nextLine() ; + String[] part = ln.split(","); + String product_id = part[0] ; + String product_name = part[1] ; + String product_price = part[2] ; + + productCatalog.add(new Product(Integer.parseInt(product_id), product_name , Float.parseFloat(product_price))) ; + + } // TODO: // - Read each line from products.csv @@ -26,8 +37,7 @@ public class Main { // - The data in products.csv is guaranteed to be valid. } - 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/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..9399c3a 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,22 +1,96 @@ +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; import java.util.List; +import java.util.Scanner; 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; this.productList = productList; } + public static boolean isNumber(String str) { + try { + Integer.parseInt(str); + return true; + } catch (NumberFormatException e) { + return false; + } + } + + public void processFile() throws IOException { + InputStream input = getClass().getClassLoader().getResourceAsStream (ordersFilePath); + if (input == null) { + throw new IOException("not found"); + } + Scanner scanner = new Scanner(input); + while (scanner.hasNextLine()) + { + String ln = scanner.nextLine() ; + String[] part = ln.split(",") ; + String product_id = part[0] ; + String product_number = part[1] ; + String product_discount = part[2] ; + + + + if (isNumber(product_id) && isNumber(product_number ) && isNumber(product_discount)) + { + int id = Integer.parseInt(product_id) ; + int number = Integer.parseInt(product_number) ; + int discount = Integer.parseInt(product_discount) ; + + if (id < 1 || id > 9 ) + { + totalInvalidLines ++ ; + continue; + + } + if (number < 1) + { + totalInvalidLines ++ ; + continue; + } + if (discount >= 100 || discount < 0 ) + { + totalInvalidLines ++ ; + continue; + } + for (Product p : productList) + { + if (p.getProductID() == id) + { + totalFinalCost = totalFinalCost + ((p.getPrice()*(100-discount)/100)*number) ; + totalDiscountValue = totalDiscountValue + ((p.getPrice()*discount/100)*number); + } + + } + totalQuantity ++ ; + + + + } + else { + + totalInvalidLines ++ ; + continue; + } + + + + + } - public void processFile() - { // TODO: // 1. Open the order details CSV file // 2. Read the file line by line @@ -57,8 +131,24 @@ public class ReportGenerator { // 12. Close all file resources } - public void saveReport() - { + public void saveReport() throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter("report.txt")) ; + writer.write("========= SALES REPORT ========="); + writer.newLine(); + writer.write("Total Quantity Sold:" + totalQuantity); + writer.newLine(); + writer.write("Total Final Cost: $" + totalFinalCost); + writer.newLine(); + writer.write("Total Discount Value: $" + totalDiscountValue); + writer.newLine(); + writer.write("Invalid Rows: " + totalInvalidLines); + writer.newLine(); + writer.write("================================"); + writer.newLine(); + writer.close(); + + + // TODO: // 1. Build a formatted report string // 2. Include: @@ -80,4 +170,5 @@ public class ReportGenerator { ================================ */ } -} \ 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