Merge pull request 'complete' (#1) from develop into main
This commit was merged in pull request #1.
This commit is contained in:
+11
-9
@@ -6,7 +6,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()
|
||||||
.getResourceAsStream("products.csv");
|
.getResourceAsStream("products.csv");
|
||||||
@@ -15,15 +15,17 @@ 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
|
String[] parts = line.split(",");
|
||||||
// - The format of the file is like this:
|
|
||||||
// [productId],[name],[price]
|
|
||||||
// - Store Product objects in the productCatalog ArrayList
|
|
||||||
|
|
||||||
// NOTE
|
int id = Integer.parseInt(parts[0]);
|
||||||
// - The data in products.csv is guaranteed to be valid.
|
String name = parts[1];
|
||||||
|
double price = Double.parseDouble(parts[2]);
|
||||||
|
|
||||||
|
productCatalog.add(new Product(id, name, price));
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
|
|||||||
@@ -1,4 +1,8 @@
|
|||||||
|
import java.io.File;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ReportGenerator {
|
public class ReportGenerator {
|
||||||
|
|
||||||
@@ -15,69 +19,64 @@ public class ReportGenerator {
|
|||||||
this.productList = productList;
|
this.productList = productList;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processFile()
|
public void processFile() {
|
||||||
{
|
try {
|
||||||
// TODO:
|
Scanner scanner = new Scanner(new File(ordersFilePath));
|
||||||
// 1. Open the order details CSV file
|
while (scanner.hasNextLine()) {
|
||||||
// 2. Read the file line by line
|
String line = scanner.nextLine();
|
||||||
// 3. Split each line using comma delimiter
|
String[] parts = line.split(",");
|
||||||
// (each line is guaranteed to contain exactly two commas)
|
|
||||||
// 4. Each line contains exactly 3 values in the format:
|
try {
|
||||||
// [productId],[quantity],[discountPercent]
|
int productId = Integer.parseInt(parts[0]);
|
||||||
// 5. Attempt to convert values:
|
int quantity = Integer.parseInt(parts[1]);
|
||||||
// - productId -> int
|
int discount = Integer.parseInt(parts[2]);
|
||||||
// - quantity -> int
|
|
||||||
// - discountPercent -> int
|
if (productId <= 0 || quantity <= 0 || discount < 0 || discount > 99) {
|
||||||
// NOTE:
|
totalInvalidLines++;
|
||||||
// - quantity and discountPercent may contain invalid characters
|
continue;
|
||||||
// (e.g., letters instead of numbers).
|
}
|
||||||
// - If parsing any value fails (NumberFormatException),
|
Product found = null;
|
||||||
// the entire line must be considered invalid.
|
for (Product p : productList) {
|
||||||
// - Use try-catch blocks and exception handling to safely
|
if (p.getProductID() == productId) {
|
||||||
// handle parsing and validation errors without stopping
|
found = p;
|
||||||
// the processing of the remaining lines.
|
break;
|
||||||
// 6. Validate parsed values:
|
}
|
||||||
// - productId must be greater than 0
|
}
|
||||||
// - quantity must be greater than 0
|
if (found == null) {
|
||||||
// - discountPercent must be between 0 and 99 inclusive
|
totalInvalidLines++;
|
||||||
// 7. Look for a Product in productList that matches productId
|
continue;
|
||||||
// 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:
|
double subtotal = quantity * found.getPrice();
|
||||||
// subtotal = quantity * product price
|
double discountValue = subtotal * discount / 100.0;
|
||||||
// discountValue = subtotal * discountPercent / 100
|
double finalCost = subtotal - discountValue;
|
||||||
// finalCost = subtotal - discountValue
|
|
||||||
// 10. Update report totals:
|
totalQuantity += quantity;
|
||||||
// - totalQuantity += quantity
|
totalFinalCost += finalCost;
|
||||||
// - totalFinalCost += finalCost
|
totalDiscountValue += discountValue;
|
||||||
// - totalDiscountValue += discountValue
|
|
||||||
// 11. If any parsing error, validation failure, or missing product occurs:
|
} catch (NumberFormatException e) {
|
||||||
// - increment totalInvalidLines
|
totalInvalidLines++;
|
||||||
// - skip the line and continue processing the next line
|
}
|
||||||
// 12. Close all file resources
|
}
|
||||||
|
scanner.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error reading order file: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveReport()
|
public void saveReport() {
|
||||||
{
|
try {
|
||||||
// TODO:
|
FileWriter writer = new FileWriter("report.txt");
|
||||||
// 1. Build a formatted report string
|
writer.write("========= SALES REPORT =========\n");
|
||||||
// 2. Include:
|
writer.write("Total Quantity Sold: " + totalQuantity + "\n");
|
||||||
// - total quantity
|
writer.write(String.format("Total Final Cost: $%.2f%n", totalFinalCost));
|
||||||
// - total final cost
|
writer.write(String.format("Total Discount Value: $%.2f%n", totalDiscountValue));
|
||||||
// - total discount value
|
writer.write("Invalid Rows: " + totalInvalidLines + "\n");
|
||||||
// - total invalid lines
|
writer.write("================================\n");
|
||||||
// 3. Create/open output report file (name it report.txt)
|
writer.close();
|
||||||
// 4. Write report string into file
|
} catch (IOException e) {
|
||||||
// 5. Handle file writing exceptions
|
System.out.println("Error writing report: " + e.getMessage());
|
||||||
// 6. Close writer resources
|
}
|
||||||
|
|
||||||
/* Example Structure
|
|
||||||
========= SALES REPORT =========
|
|
||||||
Total Quantity Sold: 42
|
|
||||||
Total Final Cost: $1520.75
|
|
||||||
Total Discount Value: $230.50
|
|
||||||
Invalid Rows: 3
|
|
||||||
================================
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user