This commit is contained in:
2026-06-08 12:59:57 +03:30
parent 8e6d1d1760
commit 92adb3c784
3 changed files with 128 additions and 80 deletions
+36 -16
View File
@@ -4,43 +4,63 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Main {
public class Main
{
static ArrayList<Product> productCatalog = new ArrayList<>();
public static void loadProducts() throws IOException{
public static void loadProducts() throws IOException
{
InputStream input = Main.class
.getClassLoader()
.getResourceAsStream("products.csv");
if (input == null) {
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();
// NOTE
// - The data in products.csv is guaranteed to be valid.
if (line.trim().isEmpty()) continue;
String[] parts = line.split(",");
if (parts.length >= 3)
{
int productId = Integer.parseInt(parts[0].trim());
String name = parts[1].trim();
double price = Double.parseDouble(parts[2].trim());
productCatalog.add(new Product(productId, name, price));
}
}
}
}
public static void main(String[] args)
{
try {
try
{
loadProducts();
} catch (IOException e) {
System.err.println("Error reading products");
} catch (IOException e)
{
System.err.println("Error reading products: " + e.getMessage());
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");
}
}
}