implement proccesFile method

This commit is contained in:
2026-05-16 17:08:04 +03:30
parent b692d0cbc3
commit 64bfeb594d
+46 -38
View File
@@ -1,4 +1,6 @@
import java.io.*;
import java.util.List; import java.util.List;
import java.util.Scanner;
public class ReportGenerator { public class ReportGenerator {
@@ -17,44 +19,49 @@ public class ReportGenerator {
public void processFile() public void processFile()
{ {
// TODO: // use try-catch to check is file available
// 1. Open the order details CSV file try {
// 2. Read the file line by line File file = new File(ordersFilePath);
// 3. Split each line using comma delimiter Scanner input = new Scanner(file);
// (each line is guaranteed to contain exactly two commas) // counter for better reporting invalid lines
// 4. Each line contains exactly 3 values in the format: int lineNumber = 0;
// [productId],[quantity],[discountPercent] while (input.hasNextLine()) {
// 5. Attempt to convert values: lineNumber++;
// - productId -> int // use try-catch to validate the lines
// - quantity -> int try {
// - discountPercent -> int String line = input.nextLine();
// NOTE: String[] fields = line.split(",");
// - quantity and discountPercent may contain invalid characters int productID = Integer.parseInt(fields[0]);
// (e.g., letters instead of numbers). int quantity = Integer.parseInt(fields[1]);
// - If parsing any value fails (NumberFormatException), int discountPercent = Integer.parseInt(fields[2]);
// the entire line must be considered invalid. // check if any field has wrong amounts
// - Use try-catch blocks and exception handling to safely if ((discountPercent < 0 || discountPercent > 100) || (quantity <= 0) || (!productList.contains(productID)))
// handle parsing and validation errors without stopping { throw new IllegalArgumentException(); }
// the processing of the remaining lines.
// 6. Validate parsed values: double subtotal = quantity * discountPercent / 100;
// - productId must be greater than 0 double discountValue = subtotal * discountPercent / 100;
// - quantity must be greater than 0 double finalCost = subtotal + discountValue;
// - discountPercent must be between 0 and 99 inclusive
// 7. Look for a Product in productList that matches productId //updating report attributes
// 8. If no Product with that ID exists in the catalog, totalQuantity += quantity;
// the entire line is considered invalid totalFinalCost += finalCost;
// 9. If all values are valid and the product exists, calculate: totalDiscountValue += discountValue;
// subtotal = quantity * product price }
// discountValue = subtotal * discountPercent / 100 catch (Exception e) {
// finalCost = subtotal - discountValue totalInvalidLines++;
// 10. Update report totals: System.out.println("Inavalid line: " + lineNumber);
// - totalQuantity += quantity continue;
// - totalFinalCost += finalCost }
// - totalDiscountValue += discountValue finally {
// 11. If any parsing error, validation failure, or missing product occurs: input.close();
// - increment totalInvalidLines }
// - skip the line and continue processing the next line }
// 12. Close all file resources
}
catch (IOException e) {
System.out.println("Orders file not found");
}
} }
public void saveReport() public void saveReport()
@@ -79,5 +86,6 @@ public class ReportGenerator {
Invalid Rows: 3 Invalid Rows: 3
================================ ================================
*/ */
} }
} }