3 Commits
Author SHA1 Message Date
HadiSharifi eba86e7ca5 implement saveReport method 2026-05-16 18:17:22 +03:30
HadiSharifi 64bfeb594d implement proccesFile method 2026-05-16 17:08:04 +03:30
HadiSharifi b692d0cbc3 implement loadProducts method 2026-05-16 15:43:25 +03:30
2 changed files with 70 additions and 66 deletions
+9 -9
View File
@@ -15,15 +15,15 @@ public class Main {
} }
Scanner scanner = new Scanner(input); Scanner scanner = new Scanner(input);
// TODO: //reading file line by line and then parsing their values
// - Read each line from products.csv while (scanner.hasNextLine()) {
// - For each line, parse productId, name, and price String line = scanner.nextLine();
// - The format of the file is like this: String[] cells = line.split(",");
// [productId],[name],[price] int productID = Integer.parseInt(cells[0]);
// - Store Product objects in the productCatalog ArrayList String productName = cells[1];
double price = Double.parseDouble(cells[2]);
// NOTE productCatalog.add(new Product(productID, productName, price));
// - The data in products.csv is guaranteed to be valid. }
} }
public static void main(String[] args) public static void main(String[] args)
+61 -57
View File
@@ -1,4 +1,7 @@
import java.io.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner;
public class ReportGenerator { public class ReportGenerator {
@@ -17,67 +20,68 @@ 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 > 99) || (quantity <= 0) ||
// handle parsing and validation errors without stopping (productList.stream().noneMatch(product -> product.getProductID() == productID)))
// the processing of the remaining lines. { throw new IllegalArgumentException(); }
// 6. Validate parsed values:
// - productId must be greater than 0 Product p = productList.stream().filter(product -> product.getProductID() == productID).findFirst().get();
// - quantity must be greater than 0 double subtotal = quantity * p.getPrice();
// - discountPercent must be between 0 and 99 inclusive double discountValue = subtotal * discountPercent / 100;
// 7. Look for a Product in productList that matches productId double finalCost = subtotal - discountValue;
// 8. If no Product with that ID exists in the catalog,
// the entire line is considered invalid //updating report attributes
// 9. If all values are valid and the product exists, calculate: totalQuantity += quantity;
// subtotal = quantity * product price totalFinalCost += finalCost;
// discountValue = subtotal * discountPercent / 100 totalDiscountValue += discountValue;
// finalCost = subtotal - discountValue }
// 10. Update report totals: catch (IllegalArgumentException e) {
// - totalQuantity += quantity totalInvalidLines++;
// - totalFinalCost += finalCost System.out.println("Inavalid line: " + lineNumber);
// - totalDiscountValue += discountValue }
// 11. If any parsing error, validation failure, or missing product occurs: }
// - increment totalInvalidLines input.close();
// - 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()
{ {
// 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 String report = "========= SALES REPORT =========\n" +
========= SALES REPORT ========= "Total Quantity Sold: " + totalQuantity + "\n" +
Total Quantity Sold: 42 "Total Final Cost: " + totalFinalCost + "\n" +
Total Final Cost: $1520.75 "Total Discount Value: " + totalDiscountValue + "\n" +
Total Discount Value: $230.50 "Total Invalid Rows: " + totalInvalidLines + "\n" +
Invalid Rows: 3 "=======================================";
================================
*/ try {
PrintWriter output = new PrintWriter("report.txt");
output.println(report);
output.close();
}
catch (FileNotFoundException e) {
System.out.println("Error writing report file!");
}
} }
} }