Complete code
This commit is contained in:
+11
-8
@@ -15,15 +15,18 @@ public class Main {
|
||||
}
|
||||
Scanner scanner = new Scanner(input);
|
||||
|
||||
// TODO:
|
||||
// - Read each line from products.csv
|
||||
// - 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
|
||||
while(scanner.hasNextLine()){
|
||||
String[] parts = scanner.nextLine().split(",");
|
||||
|
||||
// NOTE
|
||||
// - The data in products.csv is guaranteed to be valid.
|
||||
int productID = Integer.parseInt(parts[0]);
|
||||
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)
|
||||
|
||||
@@ -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.Scanner;
|
||||
|
||||
public class ReportGenerator {
|
||||
|
||||
@@ -17,48 +23,90 @@ public class ReportGenerator {
|
||||
|
||||
public void processFile()
|
||||
{
|
||||
// TODO:
|
||||
// 1. Open the order details CSV file
|
||||
// 2. Read the file line by line
|
||||
// 3. Split each line using comma delimiter
|
||||
// (each line is guaranteed to contain exactly two commas)
|
||||
// 4. Each line contains exactly 3 values in the format:
|
||||
// [productId],[quantity],[discountPercent]
|
||||
// 5. Attempt to convert values:
|
||||
// - productId -> int
|
||||
// - quantity -> int
|
||||
// - discountPercent -> int
|
||||
// NOTE:
|
||||
// - quantity and discountPercent may contain invalid characters
|
||||
// (e.g., letters instead of numbers).
|
||||
// - If parsing any value fails (NumberFormatException),
|
||||
// the entire line must be considered invalid.
|
||||
// - Use try-catch blocks and exception handling to safely
|
||||
// handle parsing and validation errors without stopping
|
||||
// the processing of the remaining lines.
|
||||
// 6. Validate parsed values:
|
||||
// - productId must be greater than 0
|
||||
// - quantity must be greater than 0
|
||||
// - discountPercent must be between 0 and 99 inclusive
|
||||
// 7. Look for a Product in productList that matches productId
|
||||
// 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
|
||||
|
||||
Path path = Paths.get(ordersFilePath);
|
||||
try {
|
||||
Scanner scanner = new Scanner(path);
|
||||
|
||||
while(scanner.hasNextLine()){
|
||||
String[] parts = scanner.nextLine().split(",");
|
||||
int productID, quantity, discount;
|
||||
|
||||
// product ID should be a valid integer
|
||||
try{
|
||||
productID = Integer.parseInt(parts[0]);
|
||||
if(productID <= 0){
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
} catch(NumberFormatException e){
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// quantity should be a valid integer and not less than 1
|
||||
try{
|
||||
quantity = Integer.parseInt(parts[1]);
|
||||
if(quantity <= 0){
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
} catch(NumberFormatException e){
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
|
||||
// discount should be a valid integer and between 0 and 99
|
||||
try{
|
||||
discount = Integer.parseInt(parts[2]);
|
||||
if(discount < 0 || discount > 99){
|
||||
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()
|
||||
{
|
||||
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:
|
||||
// 1. Build a formatted report string
|
||||
// 2. Include:
|
||||
|
||||
Reference in New Issue
Block a user