Merge pull request 'complete all parts' (#1) from develop into main { score : 500/500 }
Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
+20
-9
@@ -15,15 +15,26 @@ public class Main {
|
|||||||
}
|
}
|
||||||
Scanner scanner = new Scanner(input);
|
Scanner scanner = new Scanner(input);
|
||||||
|
|
||||||
// TODO:
|
while (scanner.hasNextLine())
|
||||||
// - Read each line from products.csv
|
{
|
||||||
// - For each line, parse productId, name, and price
|
String line = scanner.nextLine();
|
||||||
// - The format of the file is like this:
|
String[] parts = line.split(",");
|
||||||
// [productId],[name],[price]
|
if(parts.length == 3)
|
||||||
// - Store Product objects in the productCatalog ArrayList
|
{
|
||||||
|
try
|
||||||
// NOTE
|
{
|
||||||
// - The data in products.csv is guaranteed to be valid.
|
int id = Integer.parseInt(parts[0]);
|
||||||
|
String name = parts[1];
|
||||||
|
double price = Double.parseDouble(parts[2]);
|
||||||
|
productCatalog.add(new Product(id, name, price));
|
||||||
|
}
|
||||||
|
catch (NumberFormatException e)
|
||||||
|
{
|
||||||
|
System.err.println("Invalid format in products.csv: " + line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scanner.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args)
|
public static void main(String[] args)
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.FileWriter;
|
||||||
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class ReportGenerator {
|
public class ReportGenerator {
|
||||||
@@ -17,67 +21,84 @@ public class ReportGenerator {
|
|||||||
|
|
||||||
public void processFile()
|
public void processFile()
|
||||||
{
|
{
|
||||||
// TODO:
|
|
||||||
// 1. Open the order details CSV file
|
try (BufferedReader reader = new BufferedReader(new FileReader(ordersFilePath)))
|
||||||
// 2. Read the file line by line
|
{
|
||||||
// 3. Split each line using comma delimiter
|
String line;
|
||||||
// (each line is guaranteed to contain exactly two commas)
|
while ((line = reader.readLine()) != null)
|
||||||
// 4. Each line contains exactly 3 values in the format:
|
{
|
||||||
// [productId],[quantity],[discountPercent]
|
String[] parts = line.split(",");
|
||||||
// 5. Attempt to convert values:
|
if (parts.length != 3)
|
||||||
// - productId -> int
|
{
|
||||||
// - quantity -> int
|
totalInvalidLines++;
|
||||||
// - discountPercent -> int
|
continue;
|
||||||
// NOTE:
|
}
|
||||||
// - quantity and discountPercent may contain invalid characters
|
|
||||||
// (e.g., letters instead of numbers).
|
try
|
||||||
// - If parsing any value fails (NumberFormatException),
|
{
|
||||||
// the entire line must be considered invalid.
|
int id = Integer.parseInt(parts[0]);
|
||||||
// - Use try-catch blocks and exception handling to safely
|
int quantity = Integer.parseInt(parts[1]);
|
||||||
// handle parsing and validation errors without stopping
|
int discountPercent = Integer.parseInt(parts[2]);
|
||||||
// the processing of the remaining lines.
|
if (id <= 0 || quantity <= 0 || discountPercent < 0 || discountPercent > 99) {
|
||||||
// 6. Validate parsed values:
|
totalInvalidLines++;
|
||||||
// - productId must be greater than 0
|
continue;
|
||||||
// - quantity must be greater than 0
|
}
|
||||||
// - discountPercent must be between 0 and 99 inclusive
|
Product product = findProductById(id);
|
||||||
// 7. Look for a Product in productList that matches productId
|
if (product == null)
|
||||||
// 8. If no Product with that ID exists in the catalog,
|
{
|
||||||
// the entire line is considered invalid
|
totalInvalidLines++;
|
||||||
// 9. If all values are valid and the product exists, calculate:
|
continue;
|
||||||
// subtotal = quantity * product price
|
}
|
||||||
// discountValue = subtotal * discountPercent / 100
|
|
||||||
// finalCost = subtotal - discountValue
|
double subtotal = quantity * product.getPrice();
|
||||||
// 10. Update report totals:
|
double discountValue = subtotal * discountPercent / 100.0;
|
||||||
// - totalQuantity += quantity
|
double finalCost = subtotal - discountValue;
|
||||||
// - totalFinalCost += finalCost
|
totalQuantity += quantity;
|
||||||
// - totalDiscountValue += discountValue
|
totalFinalCost += finalCost;
|
||||||
// 11. If any parsing error, validation failure, or missing product occurs:
|
totalDiscountValue += discountValue;
|
||||||
// - increment totalInvalidLines
|
}
|
||||||
// - skip the line and continue processing the next line
|
catch (NumberFormatException e)
|
||||||
// 12. Close all file resources
|
{
|
||||||
|
totalInvalidLines++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
System.err.println("Error reading order file: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Product findProductById(int id) {
|
||||||
|
for (Product p : productList) {
|
||||||
|
if (p.getProductID() == id) {
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveReport()
|
public void saveReport()
|
||||||
{
|
{
|
||||||
// TODO:
|
String report = String.format(
|
||||||
// 1. Build a formatted report string
|
"========= SALES REPORT =========\n" +
|
||||||
// 2. Include:
|
"Total Quantity Sold: %d\n" +
|
||||||
// - total quantity
|
"Total Final Cost: $%.2f\n" +
|
||||||
// - total final cost
|
"Total Discount Value: $%.2f\n" +
|
||||||
// - total discount value
|
"Invalid Rows: %d\n" +
|
||||||
// - total invalid lines
|
"================================\n",
|
||||||
// 3. Create/open output report file (name it report.txt)
|
totalQuantity,
|
||||||
// 4. Write report string into file
|
totalFinalCost,
|
||||||
// 5. Handle file writing exceptions
|
totalDiscountValue,
|
||||||
// 6. Close writer resources
|
totalInvalidLines
|
||||||
|
);
|
||||||
/* Example Structure
|
try (FileWriter writer = new FileWriter("report.txt"))
|
||||||
========= SALES REPORT =========
|
{
|
||||||
Total Quantity Sold: 42
|
writer.write(report);
|
||||||
Total Final Cost: $1520.75
|
}
|
||||||
Total Discount Value: $230.50
|
catch (IOException e)
|
||||||
Invalid Rows: 3
|
{
|
||||||
================================
|
System.err.println("Error saving report: " + e.getMessage());
|
||||||
*/
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user