implement Main.java

This commit is contained in:
2026-05-20 16:54:18 +03:30
parent 8e6d1d1760
commit 9eb0b77c3f
+22 -9
View File
@@ -7,23 +7,32 @@ import java.util.Scanner;
public class Main {
static ArrayList<Product> 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));