98 lines
3.2 KiB
Java
98 lines
3.2 KiB
Java
import java.util.List;
|
|
|
|
import java.io.*;
|
|
import java.util.Scanner;
|
|
import java.io.IOException;
|
|
import java.io.FileWriter;
|
|
import java.io.InputStream;
|
|
import java.util.List;
|
|
|
|
public class ReportGenerator {
|
|
|
|
private final String ordersFilePath;
|
|
private final List<Product> productList;
|
|
|
|
private double totalFinalCost;
|
|
private int totalQuantity;
|
|
private double totalDiscountValue;
|
|
private int totalInvalidLines;
|
|
|
|
public ReportGenerator(String ordersFilePath, List<Product> productList) {
|
|
this.ordersFilePath = ordersFilePath;
|
|
this.productList = productList;
|
|
}
|
|
|
|
public void processFile() {
|
|
// TODO:
|
|
InputStream input = ReportGenerator.class
|
|
.getClassLoader()
|
|
.getResourceAsStream(ordersFilePath);
|
|
if (input == null) {
|
|
System.out.println("Order file not found");
|
|
return;
|
|
}
|
|
Scanner scanner = new Scanner(input);
|
|
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;
|
|
int i = 0;
|
|
while (i < productList.size()) {
|
|
Product product = productList.get(i);
|
|
if (product.getProductID() == productId) {
|
|
foundProduct = product;
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
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();
|
|
}
|
|
public void saveReport()
|
|
{
|
|
// TODO:
|
|
String report =
|
|
"======= SALES REPORT ========\n" +
|
|
"Total Quantity Sold: " + totalQuantity + "\n" +
|
|
"Total Final Cost: $" + totalFinalCost + "\n" +
|
|
"Total Discount Value: $" + totalDiscountValue + "\n" +
|
|
"Invalid Rows: " + totalInvalidLines + "\n" +
|
|
"================";
|
|
try {
|
|
FileWriter writer = new FileWriter("report.txt");
|
|
writer.write(report);
|
|
writer.close();
|
|
} catch (IOException e) {
|
|
System.out.println("Error writing report file");
|
|
}
|
|
}
|
|
} |