diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..ac451d2 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,3 +1,5 @@ +import java.io.File; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; @@ -14,7 +16,15 @@ public class Main { throw new IOException("products.csv not found"); } Scanner scanner = new Scanner(input); - + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] parts = line.split(","); + int productID = Integer.parseInt(parts[0].trim()); + String productName = parts[1].trim(); + double price = Double.parseDouble(parts[2].trim()); + Product product = new Product(productID, productName, price); + productCatalog.add(product); + } // 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..2e27a6d 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,4 +1,9 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.PrintWriter; import java.util.List; +import java.util.Scanner; public class ReportGenerator { @@ -17,6 +22,64 @@ public class ReportGenerator { public void processFile() { + //opening orders and doing the math + try { + File source = new File(ordersFilePath); + Scanner scanner = new Scanner(source); + + //reading line by line + while (scanner.hasNextLine()) { + int productId; + int count; + int discountPercent; + String line = scanner.nextLine(); + String[] parts = line.split(","); + try { + productId = Integer.parseInt(parts[0].trim()); + if (productId < 1 || productId > productList.size()) { + totalInvalidLines++; + continue; + } + + } catch (NumberFormatException e) { + totalInvalidLines++; + continue; + } + try { + count = Integer.parseInt(parts[1].trim()); + if(count < 1) { + totalInvalidLines++; + continue; + } + } catch (NumberFormatException e) { + totalInvalidLines++; + continue; + } + try { + discountPercent = Integer.parseInt(parts[2].trim()); + if (discountPercent < 0 || discountPercent > 99) { + totalInvalidLines++; + continue; + } + } catch (NumberFormatException e) { + totalInvalidLines++; + continue; + } + + //computing the results + double cost = count * productList.get(productId - 1).getPrice(); + double discount = count * productList.get(productId - 1).getPrice() * discountPercent / 100; + totalFinalCost += cost - discount; + totalDiscountValue += discount; + totalQuantity += count; + + } + scanner.close(); + } catch (FileNotFoundException e) { + System.out.println("File not found."); + } catch (IOException e) { + System.out.println(e.getMessage()); + } // TODO: // 1. Open the order details CSV file // 2. Read the file line by line @@ -59,6 +122,22 @@ public class ReportGenerator { public void saveReport() { + try { + File file = new File("report.txt"); + PrintWriter printWriter = new PrintWriter(file); + printWriter.println("========= SALES REPORT ========="); + printWriter.println("Total Quantity Sold: " + totalQuantity); + printWriter.printf("Total Final Cost: %.2f\n", totalFinalCost); + printWriter.printf("Total Discount Value: %.2f\n", totalDiscountValue); + printWriter.println("Invalid Lines: " + totalInvalidLines); + printWriter.println("================================"); + printWriter.close(); + } catch (FileNotFoundException e) { + System.out.println("File not found."); + } catch (IOException e) { + System.out.println(e.getMessage()); + } + // TODO: // 1. Build a formatted report string // 2. Include: