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 0000000..2f67637 Binary files /dev/null and b/structure.txt differ