implement Main.java
This commit is contained in:
+22
-9
@@ -7,23 +7,32 @@ import java.util.Scanner;
|
|||||||
public class Main {
|
public class Main {
|
||||||
static ArrayList<Product> productCatalog = new ArrayList<>();
|
static ArrayList<Product> productCatalog = new ArrayList<>();
|
||||||
public static void loadProducts() throws IOException{
|
public static void loadProducts() throws IOException{
|
||||||
|
|
||||||
InputStream input = Main.class
|
InputStream input = Main.class
|
||||||
.getClassLoader()
|
.getClassLoader()
|
||||||
.getResourceAsStream("products.csv");
|
.getResourceAsStream("products.csv");
|
||||||
if (input == null) {
|
if (input == null) {
|
||||||
throw new IOException("products.csv not found");
|
throw new IOException("products.csv not found");
|
||||||
}
|
}
|
||||||
Scanner scanner = new Scanner(input);
|
|
||||||
|
|
||||||
// TODO:
|
Scanner in = new Scanner(input);
|
||||||
// - 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
|
|
||||||
|
|
||||||
// NOTE
|
int productId;
|
||||||
// - The data in products.csv is guaranteed to be valid.
|
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)
|
public static void main(String[] args)
|
||||||
@@ -34,7 +43,11 @@ public class Main {
|
|||||||
System.err.println("Error reading products");
|
System.err.println("Error reading products");
|
||||||
System.out.println("Terminating the program...");
|
System.out.println("Terminating the program...");
|
||||||
return;
|
return;
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
System.out.println("=== Products loaded");
|
System.out.println("=== Products loaded");
|
||||||
|
|
||||||
ReportGenerator reportGenerator = new ReportGenerator("2025_order_details.csv", Collections.unmodifiableList(productCatalog));
|
ReportGenerator reportGenerator = new ReportGenerator("2025_order_details.csv", Collections.unmodifiableList(productCatalog));
|
||||||
|
|||||||
Reference in New Issue
Block a user