completing all files

This commit is contained in:
2026-05-20 15:18:04 +03:30
parent 8e6d1d1760
commit 4df6e61314
2 changed files with 106 additions and 12 deletions
+21 -7
View File
@@ -1,3 +1,5 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
@@ -6,6 +8,7 @@ import java.util.Scanner;
public class Main {
static ArrayList<Product> productCatalog = new ArrayList<>();
public static void loadProducts() throws IOException{
InputStream input = Main.class
.getClassLoader()
@@ -13,15 +16,25 @@ 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();
String[] items = line.split(",");
int productId = Integer.parseInt(items[0].trim());
String name = items[1].trim();
double price = Double.parseDouble(items[2].trim());
Product product = new Product(productId, name, price);
productCatalog.add(product);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
// NOTE
// - The data in products.csv is guaranteed to be valid.
}
@@ -42,5 +55,6 @@ public class Main {
System.out.println("=== Order file processed");
reportGenerator.saveReport();
System.out.println("=== Report saved as report.txt");
}
}
+85 -5
View File
@@ -1,14 +1,18 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
public class ReportGenerator {
private final String ordersFilePath;
private final List<Product> productList;
private double totalFinalCost;
private int totalQuantity;
private double totalDiscountValue;
private int totalInvalidLines;
private double totalFinalCost = 0;
private int totalQuantity = 0;
private double totalDiscountValue = 0;
private int totalInvalidLines = 0;
public ReportGenerator(String ordersFilePath, List<Product> productList) {
this.ordersFilePath = ordersFilePath;
@@ -17,9 +21,70 @@ public class ReportGenerator {
public void processFile()
{
// TODO:
// 1. Open the order details CSV file
// 2. Read the file line by line
try {
FileReader reader = new FileReader(this.ordersFilePath);
BufferedReader bufferedReader = new BufferedReader(reader);
String line;
while ((line = bufferedReader.readLine()) != null) {
try {
String[] values = line.split(",");
// Convert to integers
int productId = Integer.parseInt(values[0].trim());
int quantity = Integer.parseInt(values[1].trim());
int discountPercent = Integer.parseInt(values[2].trim());
// Validate values
if (productId <= 0 || productId > 9 || quantity <= 0 || discountPercent < 0 || discountPercent > 99) {
totalInvalidLines++;
continue;
}
// Check if product exists in productList?
Product product = null;
for (Product p : productList) {
if (p.getProductID() == productId) {
product = p;
break;
}
}
if (product == null) {
totalInvalidLines++;
continue;
}
else {
double subtotal = quantity * product.getPrice();
double discountValue = subtotal * discountPercent / 100;
double finalCost = subtotal - discountValue;
totalQuantity += quantity;
totalDiscountValue += discountValue;
totalFinalCost += finalCost;
}
} catch (NumberFormatException e) {
totalInvalidLines++;
continue;
}
}
bufferedReader.close();
reader.close();
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
// 3. Split each line using comma delimiter
// (each line is guaranteed to contain exactly two commas)
// 4. Each line contains exactly 3 values in the format:
@@ -71,13 +136,28 @@ public class ReportGenerator {
// 5. Handle file writing exceptions
// 6. Close writer resources
try {
FileWriter writer = new FileWriter("report.txt");
writer.write("========= SALES REPORT =========\n");
writer.write("Total Quantity Sold: " + totalQuantity);
writer.write(String.format("\nTotal Final Cost: $%.2f", totalFinalCost));
writer.write(String.format("\nTotal Discount Value: $%.2f", totalDiscountValue));
writer.write("\nInvalid Rows: " + totalInvalidLines);
writer.write("\n================================");
writer.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
/* Example Structure
========= SALES REPORT =========
Total Quantity Sold: 42
Total Final Cost: $1520.75
Total Discount Value: $230.50
Invalid Rows: 3
================================
=================================
*/
}
}