implement ReportGenerator.java

This commit is contained in:
2026-05-19 14:43:49 -07:00
parent e4d0d6febb
commit 6776af3efd
+83 -57
View File
@@ -1,4 +1,9 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Scanner;
public class ReportGenerator { public class ReportGenerator {
@@ -17,67 +22,88 @@ public class ReportGenerator {
public void processFile() public void processFile()
{ {
// TODO: Scanner scanner = null;
// 1. Open the order details CSV file
// 2. Read the file line by line try {
// 3. Split each line using comma delimiter
// (each line is guaranteed to contain exactly two commas) File file = new File(ordersFilePath);
// 4. Each line contains exactly 3 values in the format: scanner = new Scanner(file);
// [productId],[quantity],[discountPercent]
// 5. Attempt to convert values: while (scanner.hasNextLine()) {
// - productId -> int
// - quantity -> int String line = scanner.nextLine();
// - discountPercent -> int
// NOTE: try {
// - quantity and discountPercent may contain invalid characters String[] parts = line.split(",");
// (e.g., letters instead of numbers).
// - If parsing any value fails (NumberFormatException), int productId = Integer.parseInt(parts[0].trim());
// the entire line must be considered invalid. int quantity = Integer.parseInt(parts[1].trim());
// - Use try-catch blocks and exception handling to safely int discountPercent = Integer.parseInt(parts[2].trim());
// handle parsing and validation errors without stopping
// the processing of the remaining lines. if (productId <= 0 || quantity <= 0 || discountPercent < 0 || discountPercent > 99) {
// 6. Validate parsed values: totalInvalidLines++;
// - productId must be greater than 0 continue;
// - quantity must be greater than 0 }
// - discountPercent must be between 0 and 99 inclusive
// 7. Look for a Product in productList that matches productId Product foundProduct = null;
// 8. If no Product with that ID exists in the catalog,
// the entire line is considered invalid for (Product currentProduct : productList) {
// 9. If all values are valid and the product exists, calculate: if (currentProduct.getProductID() == productId) {
// subtotal = quantity * product price foundProduct = currentProduct;
// discountValue = subtotal * discountPercent / 100 break;
// finalCost = subtotal - discountValue }
// 10. Update report totals: }
// - totalQuantity += quantity
// - totalFinalCost += finalCost if (foundProduct == null) {
// - totalDiscountValue += discountValue totalInvalidLines++;
// 11. If any parsing error, validation failure, or missing product occurs: continue;
// - increment totalInvalidLines }
// - skip the line and continue processing the next line
// 12. Close all file resources double subtotal = quantity * foundProduct.getPrice();
double discountValue = subtotal * discountPercent / 100.0;
double finalCost = subtotal - discountValue;
totalQuantity += quantity;
totalFinalCost += finalCost;
totalDiscountValue += discountValue;
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) {
totalInvalidLines++;
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + ordersFilePath);
}
if (scanner != null) {
scanner.close();
}
} }
public void saveReport() public void saveReport()
{ {
// TODO: String report =
// 1. Build a formatted report string "========= SALES REPORT =========\n" +
// 2. Include: "Total Quantity Sold: " + totalQuantity + "\n" +
// - total quantity "Total Final Cost: $" + String.format("%.2f", totalFinalCost) + "\n" +
// - total final cost "Total Discount Value: $" + String.format("%.2f", totalDiscountValue) + "\n" +
// - total discount value "Invalid Rows: " + totalInvalidLines + "\n" +
// - total invalid lines "================================";
// 3. Create/open output report file (name it report.txt)
// 4. Write report string into file
// 5. Handle file writing exceptions
// 6. Close writer resources
/* Example Structure try
========= SALES REPORT ========= {
Total Quantity Sold: 42 FileWriter fileWriter = new FileWriter("report.txt");
Total Final Cost: $1520.75
Total Discount Value: $230.50 fileWriter.write(report);
Invalid Rows: 3
================================ fileWriter.close();
*/
} }
catch (IOException e)
{
System.out.println("Error writing report: " + e.getMessage());
}
}
} }