2 Commits
2 changed files with 92 additions and 70 deletions
+11 -10
View File
@@ -1,3 +1,5 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@@ -14,16 +16,15 @@ public class Main {
throw new IOException("products.csv not found"); throw new IOException("products.csv not found");
} }
Scanner scanner = new Scanner(input); Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
// TODO: String line = scanner.nextLine();
// - Read each line from products.csv String[] parts = line.split(",");
// - For each line, parse productId, name, and price int productID = Integer.parseInt(parts[0].trim());
// - The format of the file is like this: String productName = parts[1].trim();
// [productId],[name],[price] double price = Double.parseDouble(parts[2].trim());
// - Store Product objects in the productCatalog ArrayList Product product = new Product(productID, productName, price);
productCatalog.add(product);
// NOTE }
// - The data in products.csv is guaranteed to be valid.
} }
public static void main(String[] args) public static void main(String[] args)
+80 -59
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 {
@@ -8,7 +13,7 @@ public class ReportGenerator {
private double totalFinalCost; private double totalFinalCost;
private int totalQuantity; private int totalQuantity;
private double totalDiscountValue; private double totalDiscountValue;
private int totalInvalidLines; private int InvalidLines;
public ReportGenerator(String ordersFilePath, List<Product> productList) { public ReportGenerator(String ordersFilePath, List<Product> productList) {
this.ordersFilePath = ordersFilePath; this.ordersFilePath = ordersFilePath;
@@ -17,67 +22,83 @@ public class ReportGenerator {
public void processFile() public void processFile()
{ {
// TODO: //opening and doing the math
// 1. Open the order details CSV file try {
// 2. Read the file line by line File source = new File(ordersFilePath);
// 3. Split each line using comma delimiter Scanner scanner = new Scanner(source);
// (each line is guaranteed to contain exactly two commas) //
// 4. Each line contains exactly 3 values in the format: while (scanner.hasNextLine()) {
// [productId],[quantity],[discountPercent] int productId;
// 5. Attempt to convert values: int count;
// - productId -> int int discountPercentage;
// - quantity -> int String line = scanner.nextLine();
// - discountPercent -> int String[] parts = line.split(",");
// NOTE: try {
// - quantity and discountPercent may contain invalid characters productId = Integer.parseInt(parts[0].trim());
// (e.g., letters instead of numbers). if (productId < 1 || productId > productList.size()) {
// - If parsing any value fails (NumberFormatException), InvalidLines++;
// the entire line must be considered invalid. continue;
// - Use try-catch blocks and exception handling to safely }
// handle parsing and validation errors without stopping
// the processing of the remaining lines. } catch (NumberFormatException e) {
// 6. Validate parsed values: InvalidLines++;
// - productId must be greater than 0 continue;
// - quantity must be greater than 0 }
// - discountPercent must be between 0 and 99 inclusive try {
// 7. Look for a Product in productList that matches productId count = Integer.parseInt(parts[1].trim());
// 8. If no Product with that ID exists in the catalog, if(count < 1) {
// the entire line is considered invalid InvalidLines++;
// 9. If all values are valid and the product exists, calculate: continue;
// subtotal = quantity * product price }
// discountValue = subtotal * discountPercent / 100 } catch (NumberFormatException e) {
// finalCost = subtotal - discountValue InvalidLines++;
// 10. Update report totals: continue;
// - totalQuantity += quantity }
// - totalFinalCost += finalCost try {
// - totalDiscountValue += discountValue discountPercentage = Integer.parseInt(parts[2].trim());
// 11. If any parsing error, validation failure, or missing product occurs: if (discountPercentage < 0 || discountPercentage > 99) {
// - increment totalInvalidLines InvalidLines++;
// - skip the line and continue processing the next line continue;
// 12. Close all file resources }
} catch (NumberFormatException e) {
InvalidLines++;
continue;
}
//computing the results
double cost = count * productList.get(productId - 1).getPrice();
double discount = count * productList.get(productId - 1).getPrice() * discountPercentage / 100;
totalFinalCost += cost - discount;
totalDiscountValue += discount;
totalQuantity += count;
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (IOException e) {
System.out.println(e.getMessage());
}
} }
public void saveReport() public void saveReport()
{ {
// TODO: String report =
// 1. Build a formatted report string "===== SALES REPORT =====\n" +
// 2. Include: "Total Quantity: " + totalQuantity + "\n" +
// - total quantity "Total Final Cost: $" + String.format("%.2f", totalFinalCost) + "\n" +
// - total final cost "Total Discount: $" + String.format("%.2f", totalDiscountValue) + "\n" +
// - total discount value "Invalid Lines: " + InvalidLines + "\n" +
// - total invalid lines "=========================\n";
// 3. Create/open output report file (name it report.txt) try
// 4. Write report string into file {
// 5. Handle file writing exceptions FileWriter fileWriter = new FileWriter("output.txt");
// 6. Close writer resources fileWriter.write(report);
fileWriter.close();
/* Example Structure }
========= SALES REPORT ========= catch (IOException e)
Total Quantity Sold: 42 {
Total Final Cost: $1520.75 System.out.println("Error in outputting report: " + e.getMessage());
Total Discount Value: $230.50 }
Invalid Rows: 3
================================
*/
} }
} }