diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..1c3c62e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -13,21 +13,23 @@ public class Main { 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 + try (Scanner scanner = new Scanner(input)) { + while (scanner.hasNextLine()) { + String line = scanner.nextLine(); + if (line.isBlank()) continue; - // NOTE - // - The data in products.csv is guaranteed to be valid. + String[] parts = line.split(","); + int id = Integer.parseInt(parts[0].trim()); + String name = parts[1].trim(); + double price = Double.parseDouble(parts[2].trim()); + + productCatalog.add(new Product(id, name, price)); + } + } } - public static void main(String[] args) - { + public static void main(String[] args) throws IOException { try { loadProducts(); } catch (IOException e) {