Implement open file fucntionality
This commit is contained in:
@@ -1,4 +1,8 @@
|
|||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ReportGenerator {
|
public class ReportGenerator {
|
||||||
|
|
||||||
@@ -17,6 +21,30 @@ public class ReportGenerator {
|
|||||||
|
|
||||||
public void processFile()
|
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:
|
// 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 +87,21 @@ public class ReportGenerator {
|
|||||||
|
|
||||||
public void saveReport()
|
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:
|
// TODO:
|
||||||
// 1. Build a formatted report string
|
// 1. Build a formatted report string
|
||||||
// 2. Include:
|
// 2. Include:
|
||||||
|
|||||||
Reference in New Issue
Block a user