writing codes in reportGenerator file
This commit is contained in:
@@ -24,6 +24,22 @@ public class Main {
|
||||
|
||||
// NOTE
|
||||
// - The data in products.csv is guaranteed to be valid.
|
||||
while (scanner.hasNextLine()) {
|
||||
|
||||
String line = scanner.nextLine();
|
||||
|
||||
String[] parts = line.split(",");
|
||||
|
||||
int productId = Integer.parseInt(parts[0]);
|
||||
String name = parts[1];
|
||||
double price = Double.parseDouble(parts[2]);
|
||||
|
||||
Product product = new Product(productId, name, price);
|
||||
|
||||
productCatalog.add(product);
|
||||
}
|
||||
|
||||
scanner.close();
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
|
||||
@@ -55,6 +55,75 @@ public class ReportGenerator {
|
||||
// - increment totalInvalidLines
|
||||
// - skip the line and continue processing the next line
|
||||
// 12. Close all file resources
|
||||
try {
|
||||
|
||||
java.io.File file = new java.io.File(ordersFilePath);
|
||||
|
||||
java.util.Scanner scanner = new java.util.Scanner(file);
|
||||
|
||||
while (scanner.hasNextLine()) {
|
||||
|
||||
String line = scanner.nextLine();
|
||||
|
||||
try {
|
||||
|
||||
String[] parts = line.split(",");
|
||||
|
||||
int productId = Integer.parseInt(parts[0]);
|
||||
int quantity = Integer.parseInt(parts[1]);
|
||||
int discountPercent = Integer.parseInt(parts[2]);
|
||||
|
||||
if (productId <= 0 ||
|
||||
quantity <= 0 ||
|
||||
discountPercent < 0 ||
|
||||
discountPercent > 99) {
|
||||
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
|
||||
Product foundProduct = null;
|
||||
|
||||
for (Product product : productList) {
|
||||
|
||||
if (product.getProductID() == productId) {
|
||||
foundProduct = product;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (foundProduct == null) {
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
|
||||
double subtotal =
|
||||
quantity * foundProduct.getPrice();
|
||||
|
||||
double discountValue =
|
||||
subtotal * discountPercent / 100.0;
|
||||
|
||||
double finalCost =
|
||||
subtotal - discountValue;
|
||||
|
||||
totalQuantity += quantity;
|
||||
totalFinalCost += finalCost;
|
||||
totalDiscountValue += discountValue;
|
||||
|
||||
}
|
||||
catch (NumberFormatException e) {
|
||||
|
||||
totalInvalidLines++;
|
||||
}
|
||||
}
|
||||
|
||||
scanner.close();
|
||||
|
||||
}
|
||||
catch (java.io.IOException e) {
|
||||
|
||||
System.out.println("Error reading order file.");
|
||||
}
|
||||
}
|
||||
|
||||
public void saveReport()
|
||||
@@ -79,5 +148,29 @@ public class ReportGenerator {
|
||||
Invalid Rows: 3
|
||||
================================
|
||||
*/
|
||||
try {
|
||||
|
||||
java.io.FileWriter writer =
|
||||
new java.io.FileWriter("report.txt");
|
||||
|
||||
String report =
|
||||
"========= SALES REPORT =========\n" +
|
||||
"Total Quantity Sold: " + totalQuantity + "\n" +
|
||||
"Total Final Cost: $" +
|
||||
String.format("%.2f", totalFinalCost) + "\n" +
|
||||
"Total Discount Value: $" +
|
||||
String.format("%.2f", totalDiscountValue) + "\n" +
|
||||
"Invalid Rows: " + totalInvalidLines + "\n" +
|
||||
"================================";
|
||||
|
||||
writer.write(report);
|
||||
|
||||
writer.close();
|
||||
|
||||
}
|
||||
catch (java.io.IOException e) {
|
||||
|
||||
System.out.println("Error writing report file.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user