Merge pull request 'develop' (#1) from develop into main { score : 500/500 }

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-07-31 23:01:05 +00:00
2 changed files with 90 additions and 1 deletions
+11 -1
View File
@@ -1,3 +1,5 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
@@ -14,7 +16,15 @@ public class Main {
throw new IOException("products.csv not found"); throw new IOException("products.csv not found");
} }
Scanner scanner = new Scanner(input); Scanner scanner = new Scanner(input);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
int productID = Integer.parseInt(parts[0].trim());
String productName = parts[1].trim();
double price = Double.parseDouble(parts[2].trim());
Product product = new Product(productID, productName, price);
productCatalog.add(product);
}
// TODO: // TODO:
// - Read each line from products.csv // - Read each line from products.csv
// - For each line, parse productId, name, and price // - For each line, parse productId, name, and price
+79
View File
@@ -1,4 +1,9 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List; import java.util.List;
import java.util.Scanner;
public class ReportGenerator { public class ReportGenerator {
@@ -17,6 +22,64 @@ public class ReportGenerator {
public void processFile() public void processFile()
{ {
//opening orders and doing the math
try {
File source = new File(ordersFilePath);
Scanner scanner = new Scanner(source);
//reading line by line
while (scanner.hasNextLine()) {
int productId;
int count;
int discountPercent;
String line = scanner.nextLine();
String[] parts = line.split(",");
try {
productId = Integer.parseInt(parts[0].trim());
if (productId < 1 || productId > productList.size()) {
totalInvalidLines++;
continue;
}
} catch (NumberFormatException e) {
totalInvalidLines++;
continue;
}
try {
count = Integer.parseInt(parts[1].trim());
if(count < 1) {
totalInvalidLines++;
continue;
}
} catch (NumberFormatException e) {
totalInvalidLines++;
continue;
}
try {
discountPercent = Integer.parseInt(parts[2].trim());
if (discountPercent < 0 || discountPercent > 99) {
totalInvalidLines++;
continue;
}
} catch (NumberFormatException e) {
totalInvalidLines++;
continue;
}
//computing the results
double cost = count * productList.get(productId - 1).getPrice();
double discount = count * productList.get(productId - 1).getPrice() * discountPercent / 100;
totalFinalCost += cost - discount;
totalDiscountValue += discount;
totalQuantity += count;
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (IOException e) {
System.out.println(e.getMessage());
}
// 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
@@ -59,6 +122,22 @@ public class ReportGenerator {
public void saveReport() public void saveReport()
{ {
try {
File file = new File("report.txt");
PrintWriter printWriter = new PrintWriter(file);
printWriter.println("========= SALES REPORT =========");
printWriter.println("Total Quantity Sold: " + totalQuantity);
printWriter.printf("Total Final Cost: %.2f\n", totalFinalCost);
printWriter.printf("Total Discount Value: %.2f\n", totalDiscountValue);
printWriter.println("Invalid Lines: " + totalInvalidLines);
printWriter.println("================================");
printWriter.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (IOException e) {
System.out.println(e.getMessage());
}
// TODO: // TODO:
// 1. Build a formatted report string // 1. Build a formatted report string
// 2. Include: // 2. Include: