Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6d6606b166 |
@@ -14,7 +14,15 @@ public class Main {
|
||||
throw new IOException("products.csv not found");
|
||||
}
|
||||
Scanner scanner = new Scanner(input);
|
||||
|
||||
while(scanner.hasNextLine())
|
||||
{
|
||||
String line = scanner.nextLine();
|
||||
String[] information = line.split(",");
|
||||
int productId = Integer.parseInt(information[0].trim());
|
||||
String productName = information[1].trim();
|
||||
double price = Double.parseDouble(information[2].trim());
|
||||
productCatalog.add(new Product(productId, productName, price));
|
||||
}
|
||||
// TODO:
|
||||
// - Read each line from products.csv
|
||||
// - For each line, parse productId, name, and price
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
public class ReportGenerator {
|
||||
@@ -55,6 +59,63 @@ public class ReportGenerator {
|
||||
// - increment totalInvalidLines
|
||||
// - skip the line and continue processing the next line
|
||||
// 12. Close all file resources
|
||||
Path path = Path.of(ordersFilePath);
|
||||
try
|
||||
{
|
||||
List<String> lines = Files.readAllLines(path);
|
||||
for(String line: lines)
|
||||
{
|
||||
String[] information = line.split(",");
|
||||
int productId;
|
||||
int quantity;
|
||||
double discountPercent;
|
||||
try
|
||||
{
|
||||
productId = Integer.parseInt((information[0].trim()));
|
||||
quantity = Integer.parseInt(information[1].trim());
|
||||
discountPercent = Double.parseDouble(information[2].trim());
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
if((productId <= 0) || (quantity <= 0))
|
||||
{
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
else if((discountPercent <= 0) || (discountPercent > 99))
|
||||
{
|
||||
totalInvalidLines++;
|
||||
continue;
|
||||
}
|
||||
boolean haveValidId = false;
|
||||
for (Product product: productList)
|
||||
{
|
||||
if(productId == product.getProductID())
|
||||
{
|
||||
haveValidId = true;
|
||||
double subtotal = quantity * product.getPrice();
|
||||
double discountValue = subtotal * discountPercent / 100;
|
||||
double finalCost = subtotal - discountValue;
|
||||
totalQuantity += quantity;
|
||||
totalFinalCost += finalCost;
|
||||
totalDiscountValue += discountValue;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!haveValidId)
|
||||
{
|
||||
totalInvalidLines++;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void saveReport()
|
||||
@@ -79,5 +140,24 @@ public class ReportGenerator {
|
||||
Invalid Rows: 3
|
||||
================================
|
||||
*/
|
||||
String report = String.format(
|
||||
"========= SALES REPORT =========\n" +
|
||||
"Total Quantity Sold: %d\n" +
|
||||
"Total Final Cost: $%.2f\n" +
|
||||
"Total Discount Value: $%.2f\n" +
|
||||
"Invalid Rows: %d\n" +
|
||||
"================================",
|
||||
totalQuantity,
|
||||
totalFinalCost,
|
||||
totalDiscountValue,
|
||||
totalInvalidLines
|
||||
);
|
||||
|
||||
Path path = Path.of("report.txt");
|
||||
try {
|
||||
Files.write(path, List.of(report));
|
||||
} catch (IOException e) {
|
||||
System.out.println("Making report file failed!");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user