1 Commits
Author SHA1 Message Date
Mobina 5111e30762 HW-06 2026-05-23 03:10:30 +03:30
2 changed files with 93 additions and 0 deletions
+17
View File
@@ -1,7 +1,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Main {
@@ -15,6 +18,20 @@ public class Main {
}
Scanner scanner = new Scanner(input);
int productId=0;
String name=" ";
double price=0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
productId = Integer.parseInt(parts[0]);
price= Double.parseDouble(parts[2]);
Product p= new Product(productId, parts[1],price);
productCatalog.add(p);
}
// TODO:
// - Read each line from products.csv
// - For each line, parse productId, name, and price
+76
View File
@@ -1,4 +1,7 @@
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;
public class ReportGenerator {
@@ -17,6 +20,54 @@ public class ReportGenerator {
public void processFile()
{
try {
List<String> lines = Files.readAllLines(Paths.get(ordersFilePath));
for(String line: lines)
{
String[] parts = line.split(",");
int id=0;
int quantity=0;
int discountPercent=0;
double subtotal=0;
double discountValue=0;
double finalCost=0;
try {
id = Integer.parseInt(parts[0]);
quantity=Integer.parseInt(parts[1]);
discountPercent=Integer.parseInt(parts[2]);
if(id<=0 || quantity<=0 || discountPercent<0 || discountPercent>99) {
totalDiscountValue++;
continue;
}
if(!exist(id, productList)) {
totalInvalidLines++;
continue;
}
for(Product p:productList)
{
if(id== p.getProductID())
{
subtotal = quantity * p.getPrice();
discountValue = subtotal * discountPercent / 100;
finalCost = subtotal - discountValue;
}
}
totalQuantity += quantity;
totalFinalCost += finalCost;
}
catch (NumberFormatException e) {
continue;
}
}
}
catch (IOException e) {
e.printStackTrace();
}
// TODO:
// 1. Open the order details CSV file
// 2. Read the file line by line
@@ -57,8 +108,33 @@ public class ReportGenerator {
// 12. Close all file resources
}
public boolean exist(int id, List<Product> productList)
{
for(Product p:productList)
{
if(id== p.getProductID())
return true;
}
return false;
}
public void saveReport()
{
String report = "========= SALES REPORT =========\n";
report += "Total Quantity Sold: " + totalQuantity + "\n";
report += "Total Final Cost: $" + String.format("%.2f", totalFinalCost) + "\n";
report += "Total Discount Value: $" + String.format("%.2f", totalDiscountValue) + "\n";
report += "Invalid Rows: " + totalInvalidLines + "\n";
report += "================================\n";
try {
Files.writeString(Paths.get("report.txt"), report);
System.out.println("Report saved successfully!");
} catch (IOException e) {
System.err.println("Error saving report: " + e.getMessage());
e.printStackTrace();
}
// TODO:
// 1. Build a formatted report string
// 2. Include: