2 Commits
2 changed files with 88 additions and 67 deletions
+12 -8
View File
@@ -15,15 +15,19 @@ 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
// - The format of the file is like this:
// [productId],[name],[price]
// - Store Product objects in the productCatalog ArrayList
// NOTE String[] parts = line.split(",");
// - The data in products.csv is guaranteed to be valid.
int productId = Integer.parseInt(parts[0]);
String name = parts[1];
double price = Double.parseDouble(parts[2]);
productCatalog.add(new Product(productId, name, price));
}
scanner.close();
} }
public static void main(String[] args) public static void main(String[] args)
+76 -59
View File
@@ -1,4 +1,8 @@
import java.util.List; import java.util.List;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
public class ReportGenerator { public class ReportGenerator {
@@ -17,67 +21,80 @@ public class ReportGenerator {
public void processFile() public void processFile()
{ {
// TODO: try {
// 1. Open the order details CSV file Scanner scanner = new Scanner(new File(ordersFilePath));
// 2. Read the file line by line
// 3. Split each line using comma delimiter while (scanner.hasNextLine()) {
// (each line is guaranteed to contain exactly two commas)
// 4. Each line contains exactly 3 values in the format: String line = scanner.nextLine();
// [productId],[quantity],[discountPercent] String[] parts = line.split(",");
// 5. Attempt to convert values:
// - productId -> int try {
// - quantity -> int
// - discountPercent -> int int productId = Integer.parseInt(parts[0]);
// NOTE: int quantity = Integer.parseInt(parts[1]);
// - quantity and discountPercent may contain invalid characters int discountPercent = Integer.parseInt(parts[2]);
// (e.g., letters instead of numbers).
// - If parsing any value fails (NumberFormatException), if (productId <= 0 || quantity <= 0 || discountPercent < 0 || discountPercent > 99) {
// the entire line must be considered invalid. totalInvalidLines++;
// - Use try-catch blocks and exception handling to safely continue;
// handle parsing and validation errors without stopping }
// the processing of the remaining lines.
// 6. Validate parsed values: Product foundProduct = null;
// - productId must be greater than 0
// - quantity must be greater than 0 for (Product product : productList) {
// - discountPercent must be between 0 and 99 inclusive if (product.getProductID() == productId) {
// 7. Look for a Product in productList that matches productId foundProduct = product;
// 8. If no Product with that ID exists in the catalog, break;
// 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 if (foundProduct == null) {
// finalCost = subtotal - discountValue totalInvalidLines++;
// 10. Update report totals: continue;
// - totalQuantity += quantity }
// - totalFinalCost += finalCost
// - totalDiscountValue += discountValue double subtotal = quantity * foundProduct.getPrice();
// 11. If any parsing error, validation failure, or missing product occurs: double discountValue =
// - increment totalInvalidLines subtotal * discountPercent / 100.0;
// - skip the line and continue processing the next line double finalCost =
// 12. Close all file resources subtotal - discountValue;
totalQuantity += quantity;
totalFinalCost += finalCost;
totalDiscountValue += discountValue;
} catch (NumberFormatException e) {
totalInvalidLines++;
}
}
scanner.close();
} catch (FileNotFoundException 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 ========= "========= SALES REPORT =========\n" +
Total Quantity Sold: 42 "Total Quantity Sold: " + totalQuantity + "\n" +
Total Final Cost: $1520.75 "Total Final Cost: $" + totalFinalCost + "\n" +
Total Discount Value: $230.50 "Total Discount Value: $" + totalDiscountValue + "\n" +
Invalid Rows: 3 "Invalid Rows: " + totalInvalidLines + "\n" +
================================ "================================";
*/
try {
PrintWriter writer = new PrintWriter("report.txt");
writer.println(report);
writer.close();
} catch (FileNotFoundException e) {
System.out.println("Could not create report.");
}
} }
} }