2 Commits
2 changed files with 97 additions and 46 deletions
+11 -8
View File
@@ -15,15 +15,18 @@ public class Main {
} }
Scanner scanner = new Scanner(input); Scanner scanner = new Scanner(input);
// TODO: while(scanner.hasNextLine()){
// - Read each line from products.csv String[] parts = scanner.nextLine().split(",");
// - For each line, parse productId, name, and price
// - The format of the file is like this:
// [productId],[name],[price]
// - Store Product objects in the productCatalog ArrayList
// NOTE int productID = Integer.parseInt(parts[0]);
// - The data in products.csv is guaranteed to be valid. String productName = parts[1];
double productPrice = Double.parseDouble(parts[2]);
Product product = new Product(productID, productName, productPrice);
productCatalog.add(product);
}
scanner.close();
} }
public static void main(String[] args) public static void main(String[] args)
+86 -38
View File
@@ -1,4 +1,10 @@
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.rmi.ServerError;
import java.util.List; import java.util.List;
import java.util.Scanner;
public class ReportGenerator { public class ReportGenerator {
@@ -17,48 +23,90 @@ public class ReportGenerator {
public void processFile() public void processFile()
{ {
// TODO:
// 1. Open the order details CSV file Path path = Paths.get(ordersFilePath);
// 2. Read the file line by line try {
// 3. Split each line using comma delimiter Scanner scanner = new Scanner(path);
// (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] String[] parts = scanner.nextLine().split(",");
// 5. Attempt to convert values: int productID, quantity, discount;
// - productId -> int
// - quantity -> int // product ID should be a valid integer
// - discountPercent -> int try{
// NOTE: productID = Integer.parseInt(parts[0]);
// - quantity and discountPercent may contain invalid characters if(productID <= 0){
// (e.g., letters instead of numbers). totalInvalidLines++;
// - If parsing any value fails (NumberFormatException), continue;
// the entire line must be considered invalid. }
// - Use try-catch blocks and exception handling to safely } catch(NumberFormatException e){
// 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 // quantity should be a valid integer and not less than 1
// - discountPercent must be between 0 and 99 inclusive try{
// 7. Look for a Product in productList that matches productId quantity = Integer.parseInt(parts[1]);
// 8. If no Product with that ID exists in the catalog, if(quantity <= 0){
// the entire line is considered invalid totalInvalidLines++;
// 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 totalInvalidLines++;
// 10. Update report totals: continue;
// - totalQuantity += quantity }
// - totalFinalCost += finalCost
// - totalDiscountValue += discountValue // discount should be a valid integer and between 0 and 99
// 11. If any parsing error, validation failure, or missing product occurs: try{
// - increment totalInvalidLines discount = Integer.parseInt(parts[2]);
// - skip the line and continue processing the next line if(discount < 0 || discount > 99){
// 12. Close all file resources totalInvalidLines++;
continue;
}
} catch(NumberFormatException e){
totalInvalidLines++;
continue;
}
try{
Product product = productList.get(productID - 1);
double subTotal = quantity * product.getPrice();
double discountValue = subTotal * discount / 100;
double finalCost = subTotal - discountValue;
totalQuantity += quantity;
totalFinalCost += finalCost;
totalDiscountValue += discountValue;
} catch(IndexOutOfBoundsException e){ // product id should be in product list ids
totalDiscountValue++;
continue;
}
}
scanner.close();
}
catch (IOException e){
System.out.println("Cannot read order details file.");
}
} }
public void saveReport() public void saveReport()
{ {
try{
FileWriter file = new FileWriter("report.txt");
String output = "========= SALES REPORT =========\n" +
"Total Quantity Sold: " + totalQuantity + "\n" +
"Total Final Cost: $" + totalFinalCost + "\n" +
"Total Discount Value: " + totalDiscountValue + "\n" +
"Invalid Rows: " + totalInvalidLines + "\n" +
"================================";
file.write(output);
file.close();
} catch(IOException e){
System.out.println(e.getMessage());
}
// TODO: // TODO:
// 1. Build a formatted report string // 1. Build a formatted report string
// 2. Include: // 2. Include: