Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4df6e61314 |
+21
-7
@@ -1,3 +1,5 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -6,6 +8,7 @@ import java.util.Scanner;
|
|||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
static ArrayList<Product> productCatalog = new ArrayList<>();
|
static ArrayList<Product> productCatalog = new ArrayList<>();
|
||||||
|
|
||||||
public static void loadProducts() throws IOException{
|
public static void loadProducts() throws IOException{
|
||||||
InputStream input = Main.class
|
InputStream input = Main.class
|
||||||
.getClassLoader()
|
.getClassLoader()
|
||||||
@@ -13,15 +16,25 @@ public class Main {
|
|||||||
if (input == null) {
|
if (input == null) {
|
||||||
throw new IOException("products.csv not found");
|
throw new IOException("products.csv not found");
|
||||||
}
|
}
|
||||||
Scanner scanner = new Scanner(input);
|
|
||||||
|
|
||||||
// TODO:
|
try (Scanner scanner = new Scanner(input)) {
|
||||||
// - 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 line = scanner.nextLine();
|
||||||
|
String[] items = line.split(",");
|
||||||
|
|
||||||
|
int productId = Integer.parseInt(items[0].trim());
|
||||||
|
String name = items[1].trim();
|
||||||
|
double price = Double.parseDouble(items[2].trim());
|
||||||
|
|
||||||
|
Product product = new Product(productId, name, price);
|
||||||
|
productCatalog.add(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
// NOTE
|
// NOTE
|
||||||
// - The data in products.csv is guaranteed to be valid.
|
// - The data in products.csv is guaranteed to be valid.
|
||||||
}
|
}
|
||||||
@@ -42,5 +55,6 @@ public class Main {
|
|||||||
System.out.println("=== Order file processed");
|
System.out.println("=== Order file processed");
|
||||||
reportGenerator.saveReport();
|
reportGenerator.saveReport();
|
||||||
System.out.println("=== Report saved as report.txt");
|
System.out.println("=== Report saved as report.txt");
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
public class ReportGenerator {
|
public class ReportGenerator {
|
||||||
|
|
||||||
private final String ordersFilePath;
|
private final String ordersFilePath;
|
||||||
private final List<Product> productList;
|
private final List<Product> productList;
|
||||||
|
|
||||||
private double totalFinalCost;
|
private double totalFinalCost = 0;
|
||||||
private int totalQuantity;
|
private int totalQuantity = 0;
|
||||||
private double totalDiscountValue;
|
private double totalDiscountValue = 0;
|
||||||
private int totalInvalidLines;
|
private int totalInvalidLines = 0;
|
||||||
|
|
||||||
public ReportGenerator(String ordersFilePath, List<Product> productList) {
|
public ReportGenerator(String ordersFilePath, List<Product> productList) {
|
||||||
this.ordersFilePath = ordersFilePath;
|
this.ordersFilePath = ordersFilePath;
|
||||||
@@ -17,9 +21,70 @@ public class ReportGenerator {
|
|||||||
|
|
||||||
public void processFile()
|
public void processFile()
|
||||||
{
|
{
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// 1. Open the order details CSV file
|
// 1. Open the order details CSV file
|
||||||
// 2. Read the file line by line
|
// 2. Read the file line by line
|
||||||
|
try {
|
||||||
|
FileReader reader = new FileReader(this.ordersFilePath);
|
||||||
|
BufferedReader bufferedReader = new BufferedReader(reader);
|
||||||
|
|
||||||
|
String line;
|
||||||
|
|
||||||
|
while ((line = bufferedReader.readLine()) != null) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
String[] values = line.split(",");
|
||||||
|
|
||||||
|
// Convert to integers
|
||||||
|
int productId = Integer.parseInt(values[0].trim());
|
||||||
|
int quantity = Integer.parseInt(values[1].trim());
|
||||||
|
int discountPercent = Integer.parseInt(values[2].trim());
|
||||||
|
|
||||||
|
// Validate values
|
||||||
|
if (productId <= 0 || productId > 9 || quantity <= 0 || discountPercent < 0 || discountPercent > 99) {
|
||||||
|
totalInvalidLines++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if product exists in productList?
|
||||||
|
Product product = null;
|
||||||
|
for (Product p : productList) {
|
||||||
|
if (p.getProductID() == productId) {
|
||||||
|
product = p;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (product == null) {
|
||||||
|
totalInvalidLines++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
double subtotal = quantity * product.getPrice();
|
||||||
|
double discountValue = subtotal * discountPercent / 100;
|
||||||
|
double finalCost = subtotal - discountValue;
|
||||||
|
|
||||||
|
totalQuantity += quantity;
|
||||||
|
totalDiscountValue += discountValue;
|
||||||
|
totalFinalCost += finalCost;
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
|
||||||
|
totalInvalidLines++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bufferedReader.close();
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
// 3. Split each line using comma delimiter
|
// 3. Split each line using comma delimiter
|
||||||
// (each line is guaranteed to contain exactly two commas)
|
// (each line is guaranteed to contain exactly two commas)
|
||||||
// 4. Each line contains exactly 3 values in the format:
|
// 4. Each line contains exactly 3 values in the format:
|
||||||
@@ -71,13 +136,28 @@ public class ReportGenerator {
|
|||||||
// 5. Handle file writing exceptions
|
// 5. Handle file writing exceptions
|
||||||
// 6. Close writer resources
|
// 6. Close writer resources
|
||||||
|
|
||||||
|
try {
|
||||||
|
FileWriter writer = new FileWriter("report.txt");
|
||||||
|
writer.write("========= SALES REPORT =========\n");
|
||||||
|
writer.write("Total Quantity Sold: " + totalQuantity);
|
||||||
|
writer.write(String.format("\nTotal Final Cost: $%.2f", totalFinalCost));
|
||||||
|
writer.write(String.format("\nTotal Discount Value: $%.2f", totalDiscountValue));
|
||||||
|
writer.write("\nInvalid Rows: " + totalInvalidLines);
|
||||||
|
writer.write("\n================================");
|
||||||
|
|
||||||
|
writer.close();
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
/* Example Structure
|
/* Example Structure
|
||||||
========= SALES REPORT =========
|
========= SALES REPORT =========
|
||||||
Total Quantity Sold: 42
|
Total Quantity Sold: 42
|
||||||
Total Final Cost: $1520.75
|
Total Final Cost: $1520.75
|
||||||
Total Discount Value: $230.50
|
Total Discount Value: $230.50
|
||||||
Invalid Rows: 3
|
Invalid Rows: 3
|
||||||
================================
|
=================================
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user