2 Commits
Author SHA1 Message Date
HoseinA05 1d09041f1d Merge pull request 'complete' (#1) from develop into main 2026-07-02 20:03:17 +00:00
Reza 0c38bbdaf2 complete 2026-05-21 19:13:36 +03:30
2 changed files with 72 additions and 71 deletions
+10 -8
View File
@@ -15,15 +15,17 @@ public class Main {
} }
Scanner scanner = new Scanner(input); Scanner scanner = new Scanner(input);
// TODO: while (scanner.hasNextLine()) {
// - Read each line from products.csv String line = scanner.nextLine();
// - For each line, parse productId, name, and price String[] parts = line.split(",");
// - The format of the file is like this:
// [productId],[name],[price]
// - Store Product objects in the productCatalog ArrayList
// NOTE int id = Integer.parseInt(parts[0]);
// - The data in products.csv is guaranteed to be valid. String name = parts[1];
double price = Double.parseDouble(parts[2]);
productCatalog.add(new Product(id, name, price));
}
scanner.close();
} }
public static void main(String[] args) public static void main(String[] args)
+60 -61
View File
@@ -1,4 +1,8 @@
import java.io.File;
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 {
@@ -15,69 +19,64 @@ public class ReportGenerator {
this.productList = productList; this.productList = productList;
} }
public void processFile() public void processFile() {
{ try {
// TODO: Scanner scanner = new Scanner(new File(ordersFilePath));
// 1. Open the order details CSV file while (scanner.hasNextLine()) {
// 2. Read the file line by line String line = scanner.nextLine();
// 3. Split each line using comma delimiter String[] parts = line.split(",");
// (each line is guaranteed to contain exactly two commas)
// 4. Each line contains exactly 3 values in the format: try {
// [productId],[quantity],[discountPercent] int productId = Integer.parseInt(parts[0]);
// 5. Attempt to convert values: int quantity = Integer.parseInt(parts[1]);
// - productId -> int int discount = Integer.parseInt(parts[2]);
// - quantity -> int
// - discountPercent -> int if (productId <= 0 || quantity <= 0 || discount < 0 || discount > 99) {
// NOTE: totalInvalidLines++;
// - quantity and discountPercent may contain invalid characters continue;
// (e.g., letters instead of numbers). }
// - If parsing any value fails (NumberFormatException), Product found = null;
// the entire line must be considered invalid. for (Product p : productList) {
// - Use try-catch blocks and exception handling to safely if (p.getProductID() == productId) {
// handle parsing and validation errors without stopping found = p;
// the processing of the remaining lines. break;
// 6. Validate parsed values: }
// - productId must be greater than 0 }
// - quantity must be greater than 0 if (found == null) {
// - discountPercent must be between 0 and 99 inclusive totalInvalidLines++;
// 7. Look for a Product in productList that matches productId continue;
// 8. If no Product with that ID exists in the catalog,
// the entire line is considered invalid
// 9. If all values are valid and the product exists, calculate:
// subtotal = quantity * product price
// discountValue = subtotal * discountPercent / 100
// finalCost = subtotal - discountValue
// 10. Update report totals:
// - totalQuantity += quantity
// - totalFinalCost += finalCost
// - totalDiscountValue += discountValue
// 11. If any parsing error, validation failure, or missing product occurs:
// - increment totalInvalidLines
// - skip the line and continue processing the next line
// 12. Close all file resources
} }
public void saveReport() double subtotal = quantity * found.getPrice();
{ double discountValue = subtotal * discount / 100.0;
// TODO: double finalCost = subtotal - discountValue;
// 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 totalQuantity += quantity;
========= SALES REPORT ========= totalFinalCost += finalCost;
Total Quantity Sold: 42 totalDiscountValue += discountValue;
Total Final Cost: $1520.75
Total Discount Value: $230.50 } catch (NumberFormatException e) {
Invalid Rows: 3 totalInvalidLines++;
================================ }
*/ }
scanner.close();
} catch (IOException e) {
System.out.println("Error reading order file: " + e.getMessage());
}
}
public void saveReport() {
try {
FileWriter writer = new FileWriter("report.txt");
writer.write("========= SALES REPORT =========\n");
writer.write("Total Quantity Sold: " + totalQuantity + "\n");
writer.write(String.format("Total Final Cost: $%.2f%n", totalFinalCost));
writer.write(String.format("Total Discount Value: $%.2f%n", totalDiscountValue));
writer.write("Invalid Rows: " + totalInvalidLines + "\n");
writer.write("================================\n");
writer.close();
} catch (IOException e) {
System.out.println("Error writing report: " + e.getMessage());
}
} }
} }