From d8c191e367cb9f932f693b894ded489680204372 Mon Sep 17 00:00:00 2001 From: Saeed Jamali Date: Sat, 16 May 2026 18:56:36 +0330 Subject: [PATCH] Implement open file fucntionality --- src/main/java/ReportGenerator.java | 43 ++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..424a87a 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -1,4 +1,8 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; import java.util.List; +import java.util.Scanner; public class ReportGenerator { @@ -17,6 +21,30 @@ public class ReportGenerator { public void processFile() { + try { + Scanner scanner = new Scanner(new File("products.csv")); + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + String[] parts = line.split(","); + int productID = Integer.parseInt(parts[0]); + String productName = parts[1]; + double price = Double.parseDouble(parts[2]); + Product product = new Product(productID, productName, price); + productList.add(product); + } + } catch (FileNotFoundException e) { + System.out.println("File not found."); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + + try { + File source = new File(ordersFilePath); + Scanner scanner = new Scanner(source); + + } catch (FileNotFoundException e) { + System.out.println("File not found."); + } // TODO: // 1. Open the order details CSV file // 2. Read the file line by line @@ -59,6 +87,21 @@ public class ReportGenerator { public void saveReport() { + try { + File file = new File("report.txt"); + PrintWriter printWriter = new PrintWriter(file); + printWriter.write("========= SALES REPORT =========\n"); + printWriter.write("Total Quantity Sold: "+ totalQuantity + "\n"); + printWriter.write("Total Final Cost: "+ totalFinalCost + "\n"); + printWriter.write("Total Discount Value: "+ totalDiscountValue + "\n"); + printWriter.write("================================"); + printWriter.close(); + } catch (FileNotFoundException e) { + System.out.println("File not found."); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + // TODO: // 1. Build a formatted report string // 2. Include: