Implement open file fucntionality

This commit is contained in:
2026-05-16 18:56:36 +03:30
parent 8e6d1d1760
commit d8c191e367
+43
View File
@@ -1,4 +1,8 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Scanner;
public class ReportGenerator {
@@ -17,6 +21,30 @@ public class ReportGenerator {
public void processFile()
{
try {
Scanner scanner = new Scanner(new File("products.csv"));
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] parts = line.split(",");
int productID = Integer.parseInt(parts[0]);
String productName = parts[1];
double price = Double.parseDouble(parts[2]);
Product product = new Product(productID, productName, price);
productList.add(product);
}
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
File source = new File(ordersFilePath);
Scanner scanner = new Scanner(source);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
// TODO:
// 1. Open the order details CSV file
// 2. Read the file line by line
@@ -59,6 +87,21 @@ public class ReportGenerator {
public void saveReport()
{
try {
File file = new File("report.txt");
PrintWriter printWriter = new PrintWriter(file);
printWriter.write("========= SALES REPORT =========\n");
printWriter.write("Total Quantity Sold: "+ totalQuantity + "\n");
printWriter.write("Total Final Cost: "+ totalFinalCost + "\n");
printWriter.write("Total Discount Value: "+ totalDiscountValue + "\n");
printWriter.write("================================");
printWriter.close();
} catch (FileNotFoundException e) {
System.out.println("File not found.");
} catch (Exception e) {
System.out.println(e.getMessage());
}
// TODO:
// 1. Build a formatted report string
// 2. Include: