2 Commits
Author SHA1 Message Date
reyhne 5614d5a5ff Merge pull request 'HW 06' (#1) from develop into main
Reviewed-on: Arshida_0/HW-06-exceptions-and-file-handling-Arshida#1
2026-07-16 18:02:12 +00:00
Arshida_0 c328e8d6b2 HW 06 2026-05-19 00:32:40 +04:30
2 changed files with 77 additions and 2 deletions
+15 -2
View File
@@ -1,3 +1,5 @@
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
@@ -6,7 +8,7 @@ import java.util.Scanner;
public class Main {
static ArrayList<Product> productCatalog = new ArrayList<>();
public static void loadProducts() throws IOException{
public static void loadProducts() throws IOException {
InputStream input = Main.class
.getClassLoader()
.getResourceAsStream("products.csv");
@@ -15,7 +17,7 @@ public class Main {
}
Scanner scanner = new Scanner(input);
// TODO:
// TODO: .
// - Read each line from products.csv
// - For each line, parse productId, name, and price
// - The format of the file is like this:
@@ -24,6 +26,17 @@ public class Main {
// NOTE
// - The data in products.csv is guaranteed to be valid.
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
String[] fields = line.split(",");
int id = Integer.parseInt(fields[0]);
String name = fields[1];
double price = Double.parseDouble(fields[2]);
Product product = new Product(id, name, price);
productCatalog.add(product);
}
scanner.close();
}
public static void main(String[] args)
+62
View File
@@ -1,3 +1,4 @@
import java.io.*;
import java.util.List;
public class ReportGenerator {
@@ -55,6 +56,55 @@ public class ReportGenerator {
// - increment totalInvalidLines
// - skip the line and continue processing the next line
// 12. Close all file resources
try (FileReader fileReader = new FileReader(ordersFilePath)) {
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] fields = line.split(",");
try {
int productId_rd = Integer.parseInt(fields[0]);
int quantity_rd = Integer.parseInt(fields[1]);
int discountPercent_rd = Integer.parseInt(fields[2]);
if ((productId_rd <= 0)||(quantity_rd <= 0)||(discountPercent_rd < 0)||(discountPercent_rd > 99)) {
totalInvalidLines++;
continue;
}
boolean isTheId = false;
for (Product p : productList) {
if (p.getProductID() == productId_rd) {
double subtotal = quantity_rd * p.getPrice();
double discountValue = subtotal * discountPercent_rd / 100;
double finalCost = subtotal - discountValue;
totalFinalCost += finalCost;
totalQuantity += quantity_rd;
totalDiscountValue += discountValue;
isTheId = true;
break;
}
}
if (!isTheId) {
totalInvalidLines++;
}
}
catch (NumberFormatException e) {
totalInvalidLines++;
}
}
bufferedReader.close();
}
catch (IOException e) {
System.out.println("Can't read " + ordersFilePath);
System.out.println(e.getMessage());
}
}
public void saveReport()
@@ -79,5 +129,17 @@ public class ReportGenerator {
Invalid Rows: 3
================================
*/
String report_format = "========= SALES REPORT =========\nTotal Quantity Sold: %d\nTotal Final Cost: %.4f\nTotal Discount Value: %.4f\nInvalid Rows: %d\n================================\n";
String report_str = String.format(report_format, totalQuantity, totalFinalCost, totalDiscountValue, totalInvalidLines);
try (FileWriter fw = new FileWriter("report.txt", true)) {
BufferedWriter bw = new BufferedWriter(fw);
bw.write(report_str);
bw.close();
}
catch (IOException e) {
System.out.println(e.getMessage());
}
}
}