implement ReportGenerator

This commit is contained in:
2026-05-21 01:32:31 +03:30
parent 9eb0b77c3f
commit ada4c1a976
+76 -65
View File
@@ -1,83 +1,94 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List; import java.util.List;
import java.util.Scanner;
public class ReportGenerator { public class ReportGenerator {
private final String ordersFilePath; private final String ordersFilePath;
private final List<Product> productList; private final List<Product> productList;
private double totalFinalCost; private double totalFinalCost = 0;
private int totalQuantity; private int totalQuantity = 0;
private double totalDiscountValue; private double totalDiscountValue = 0;
private int totalInvalidLines; private int totalInvalidLines = 0;
public ReportGenerator(String ordersFilePath, List<Product> productList) { public ReportGenerator(String ordersFilePath, List<Product> productList) {
this.ordersFilePath = ordersFilePath; this.ordersFilePath = ordersFilePath;
this.productList = productList; this.productList = productList;
} }
public void processFile() public void processFile() throws IOException {
{
// TODO: File input = new File(ordersFilePath);
// 1. Open the order details CSV file
// 2. Read the file line by line Scanner in = new Scanner(input);
// 3. Split each line using comma delimiter
// (each line is guaranteed to contain exactly two commas) int productId, quantity, discountPercent;
// 4. Each line contains exactly 3 values in the format:
// [productId],[quantity],[discountPercent] while (in.hasNextLine()) {
// 5. Attempt to convert values:
// - productId -> int Scanner line = new Scanner(in.nextLine());
// - quantity -> int line.useDelimiter(",");
// - discountPercent -> int
// NOTE: try {
// - quantity and discountPercent may contain invalid characters productId = line.nextInt();
// (e.g., letters instead of numbers). quantity = line.nextInt();
// - If parsing any value fails (NumberFormatException), discountPercent = line.nextInt();
// the entire line must be considered invalid. } catch (Exception e) {
// - Use try-catch blocks and exception handling to safely line.close();
// handle parsing and validation errors without stopping totalInvalidLines++;
// the processing of the remaining lines. continue;
// 6. Validate parsed values: }
// - productId must be greater than 0
// - quantity must be greater than 0 if(productId<=0 || quantity<=0 || discountPercent<0 || discountPercent>99) {
// - discountPercent must be between 0 and 99 inclusive totalInvalidLines++;
// 7. Look for a Product in productList that matches productId line.close();
// 8. If no Product with that ID exists in the catalog, continue;
// the entire line is considered invalid }
// 9. If all values are valid and the product exists, calculate:
// subtotal = quantity * product price boolean productFound = false;
// discountValue = subtotal * discountPercent / 100 for(Product product : productList) {
// finalCost = subtotal - discountValue if(product.getProductID() == productId) {
// 10. Update report totals:
// - totalQuantity += quantity double subtotal = quantity * product.getPrice();
// - totalFinalCost += finalCost double discountValue = subtotal * discountPercent / 100;
// - totalDiscountValue += discountValue double finalCost = subtotal - discountValue;
// 11. If any parsing error, validation failure, or missing product occurs:
// - increment totalInvalidLines totalQuantity += quantity;
// - skip the line and continue processing the next line totalFinalCost += finalCost;
// 12. Close all file resources totalDiscountValue += discountValue;
productFound = true;
break;
}
}
if(!productFound) totalInvalidLines++;
line.close();
}
in.close();
} }
public void saveReport() public void saveReport() {
{
// TODO:
// 1. Build a formatted report string
// 2. Include:
// - total quantity
// - total final cost
// - total discount value
// - 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 File file = new File("report.txt");
========= SALES REPORT ========= String report = String.format("========== SALES REPORT ==========\n" +
Total Quantity Sold: 42 "Total Quantity Sold: %d\n" +
Total Final Cost: $1520.75 "Total Final Cost: $%.2f\n" +
Total Discount Value: $230.50 "Total Discount Value: $%.2f\n" +
Invalid Rows: 3 "Invalid Rows: %d",
================================ totalQuantity, totalFinalCost, totalDiscountValue, totalInvalidLines);
*/
try {
PrintWriter out = new PrintWriter(file);
out.print(report);
out.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
} }
} }