From 9eb0b77c3fd6cd03dd2dc3a6105759cb0b9d50b4 Mon Sep 17 00:00:00 2001 From: Fateme Azizi Date: Wed, 20 May 2026 16:54:18 +0330 Subject: [PATCH] implement Main.java --- src/main/java/Main.java | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..fcc6e81 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -7,23 +7,32 @@ import java.util.Scanner; public class Main { static ArrayList productCatalog = new ArrayList<>(); public static void loadProducts() throws IOException{ + InputStream input = Main.class .getClassLoader() .getResourceAsStream("products.csv"); if (input == null) { throw new IOException("products.csv not found"); } - Scanner scanner = new Scanner(input); - // TODO: - // - Read each line from products.csv - // - For each line, parse productId, name, and price - // - The format of the file is like this: - // [productId],[name],[price] - // - Store Product objects in the productCatalog ArrayList + Scanner in = new Scanner(input); - // NOTE - // - The data in products.csv is guaranteed to be valid. + int productId; + String name; + double price; + + + while (in.hasNextLine()) { + + Scanner line = new Scanner(in.nextLine()); + line.useDelimiter(","); + productId = line.nextInt(); + name = line.next(); + price = line.nextDouble(); + + productCatalog.add(new Product(productId, name, price)); + } + in.close(); } public static void main(String[] args) @@ -34,7 +43,11 @@ public class Main { System.err.println("Error reading products"); System.out.println("Terminating the program..."); return; + } catch (Exception e) { + System.out.println(e.getMessage()); } + + System.out.println("=== Products loaded"); ReportGenerator reportGenerator = new ReportGenerator("2025_order_details.csv", Collections.unmodifiableList(productCatalog));