1 Commits
Author SHA1 Message Date
Matin 757ecee35a Mobile phone shop report work 2026-05-20 22:31:37 +03:30
2 changed files with 66 additions and 65 deletions
+8 -8
View File
@@ -15,15 +15,15 @@ public class Main {
} }
Scanner scanner = new Scanner(input); Scanner scanner = new Scanner(input);
// TODO: while (scanner.hasNextLine()){
// - Read each line from products.csv String line = scanner.nextLine();
// - For each line, parse productId, name, and price String[] part = line.split(",");
// - The format of the file is like this: int productId = Integer.parseInt(part[0]);
// [productId],[name],[price] String name = part[1];
// - Store Product objects in the productCatalog ArrayList double price = Double.parseDouble(part[2]);
productCatalog.add(new Product(productId, name, price));
}
// NOTE
// - The data in products.csv is guaranteed to be valid.
} }
public static void main(String[] args) public static void main(String[] args)
+58 -57
View File
@@ -1,4 +1,9 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List; import java.util.List;
import java.util.Scanner;
public class ReportGenerator { public class ReportGenerator {
@@ -17,67 +22,63 @@ public class ReportGenerator {
public void processFile() public void processFile()
{ {
// TODO: InputStream input = getClass().getResourceAsStream(ordersFilePath);
// 1. Open the order details CSV file if (input == null){
// 2. Read the file line by line totalInvalidLines++;
// 3. Split each line using comma delimiter return;
// (each line is guaranteed to contain exactly two commas) }
// 4. Each line contains exactly 3 values in the format: Scanner scanner = new Scanner(input);
// [productId],[quantity],[discountPercent] while (scanner.hasNextLine()){
// 5. Attempt to convert values: String line = scanner.nextLine();
// - productId -> int String[] part = line.split(",");
// - quantity -> int try {
// - discountPercent -> int int productId = Integer.parseInt(part[0]);
// NOTE: int quantity = Integer.parseInt(part[1]);
// - quantity and discountPercent may contain invalid characters int discountPercent = Integer.parseInt(part[2]);
// (e.g., letters instead of numbers).
// - If parsing any value fails (NumberFormatException), if(productId <= 0 || quantity <= 0 || discountPercent < 0 || discountPercent > 99){
// the entire line must be considered invalid. totalInvalidLines++;
// - Use try-catch blocks and exception handling to safely continue;
// handle parsing and validation errors without stopping }
// the processing of the remaining lines.
// 6. Validate parsed values: Product foundProduct = null ;
// - productId must be greater than 0 for (Product p : productList){
// - quantity must be greater than 0 if(p.getProductID() == productId){
// - discountPercent must be between 0 and 99 inclusive foundProduct = p;
// 7. Look for a Product in productList that matches productId break;
// 8. If no Product with that ID exists in the catalog, }
// the entire line is considered invalid }
// 9. If all values are valid and the product exists, calculate:
// subtotal = quantity * product price if(foundProduct == null){
// discountValue = subtotal * discountPercent / 100 totalInvalidLines++;
// finalCost = subtotal - discountValue continue;
// 10. Update report totals: }
// - totalQuantity += quantity double price = foundProduct.getPrice();
// - totalFinalCost += finalCost double subtotal = quantity * price;
// - totalDiscountValue += discountValue double discountValue = subtotal * discountPercent / 100;
// 11. If any parsing error, validation failure, or missing product occurs: double finalCost = subtotal - discountValue;
// - increment totalInvalidLines
// - skip the line and continue processing the next line totalQuantity += quantity;
// 12. Close all file resources totalFinalCost += finalCost;
totalDiscountValue += discountValue;
}catch (NumberFormatException e){
totalInvalidLines++;
continue;
}
}
scanner.close();
} }
public void saveReport() public void saveReport()
{ {
// TODO: String report = "=========Report=========\n" + "Total quantity : " + totalQuantity + "\n" + "Total final cost : " + totalFinalCost + "\n" +
// 1. Build a formatted report string "Total discount value : " + totalDiscountValue + "\n" + "Total invalid lines : " + totalInvalidLines + "\n";
// 2. Include:
// - total quantity
// - total final cost
// - total discount value
// - total invalid lines
// 3. Create/open output report file (name it report.txt)
// 4. Write report string into file
// 5. Handle file writing exceptions
// 6. Close writer resources
/* Example Structure try (PrintWriter writer = new PrintWriter(new File("report.txt"))){
========= SALES REPORT ========= writer.print(report);
Total Quantity Sold: 42 } catch (FileNotFoundException e) {
Total Final Cost: $1520.75 System.err.println("Error creating report file: " + e.getMessage());
Total Discount Value: $230.50 }
Invalid Rows: 3
================================
*/
} }
} }