Files
HW-06-exceptions-and-file-h…/src/main/java/Main.java
T
2026-05-20 12:36:46 +03:30

58 lines
1.9 KiB
Java

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
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);
while (scanner.hasNextLine())
{
String line = scanner.nextLine();
String[] parts = line.split(",");
if(parts.length == 3)
{
try
{
int id = Integer.parseInt(parts[0]);
String name = parts[1];
double price = Double.parseDouble(parts[2]);
productCatalog.add(new Product(id, name, price));
}
catch (NumberFormatException e)
{
System.err.println("Invalid format in products.csv: " + line);
}
}
}
scanner.close();
}
public static void main(String[] args)
{
try {
loadProducts();
} catch (IOException e) {
System.err.println("Error reading products");
System.out.println("Terminating the program...");
return;
}
System.out.println("=== Products loaded");
ReportGenerator reportGenerator = new ReportGenerator("2025_order_details.csv", Collections.unmodifiableList(productCatalog));
reportGenerator.processFile();
System.out.println("=== Order file processed");
reportGenerator.saveReport();
System.out.println("=== Report saved as report.txt");
}
}