develop #1

Open
mahdi_Goudarzi wants to merge 2 commits from develop into main
2 changed files with 25 additions and 25 deletions
Showing only changes of commit 9c940c1cfb - Show all commits
+1 -1
View File
@@ -29,7 +29,7 @@ public class Main {
Product product = new Product(id , name , price); Product product = new Product(id , name , price);
productCatalog.add(product); productCatalog.add(product);
} }
+24 -24
View File
@@ -9,10 +9,10 @@ public class ReportGenerator {
private final String ordersFilePath; private final String ordersFilePath;
private final List<Product> productList; private final List<Product> productList;
private double totalFinalCost; private double totalFinalCost =0;
private int totalQuantity; private int totalQuantity=0;
private double totalDiscountValue; private double totalDiscountValue=0;
private int totalInvalidLines; private int totalInvalidLines=0;
public ReportGenerator(String ordersFilePath, List<Product> productList) { public ReportGenerator(String ordersFilePath, List<Product> productList) {
this.ordersFilePath = ordersFilePath; this.ordersFilePath = ordersFilePath;
@@ -26,38 +26,42 @@ public class ReportGenerator {
try { try {
input = new Scanner(inputfile); input = new Scanner(inputfile);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
System.out.println("there is no csv file to open it");; System.out.println("We cannot open csv file");;
} }
while (input.hasNextLine()){ while (input.hasNextLine()){
input.useDelimiter(",");
try{ try{
int id = input.nextInt(); String line = input.nextLine();
int quantity = input.nextInt(); String[] data = line.split(",");
int discount = input.nextInt(); int id = Integer.parseInt(data[0]);
if(quantity<0 || discount<0 || discount>= 100 || id <= 0 || id > 9){ int quantity = Integer.parseInt(data[1]);
int discount = Integer.parseInt(data[2]);
if(quantity <= 0 || discount<0 || discount>= 100 || id <= 0 || id > 9){
totalInvalidLines++; totalInvalidLines++;
continue; continue;
} }
double priceOfproduct = 0; Product myProduct = null;
for (Product p : productList){ for (Product p : productList) {
if(id == p.getProductID()){ if (p.getProductID() == id) {
priceOfproduct = p.getPrice(); myProduct = p;
break;
} }
} }
if(priceOfproduct == 0){ if (myProduct == null) {
totalInvalidLines++; totalInvalidLines++;
continue; continue;
} }
double subtotal = quantity * priceOfproduct;
double discountValue = subtotal * discount / 100 ;
double finalCost = subtotal - discountValue;
double subtotal = quantity * myProduct.getPrice();
double discountValue = subtotal * (discount / 100.0);
double finalCost = subtotal - discountValue;
totalQuantity += quantity; totalQuantity += quantity;
totalFinalCost += finalCost; totalFinalCost += finalCost;
@@ -65,11 +69,6 @@ public class ReportGenerator {
}catch (NumberFormatException e){ }catch (NumberFormatException e){
totalInvalidLines++; totalInvalidLines++;
continue; continue;
@@ -129,9 +128,10 @@ public class ReportGenerator {
report.println("=======REPORT========"); report.println("=======REPORT========");
report.println("Totsl Quantity sold :" + totalQuantity); report.println("Totsl Quantity sold :" + totalQuantity);
report.println("Toal final cost: $" + totalFinalCost); report.println("Toal final cost: $" + totalFinalCost);
report.println("TOtal discount value : $" + totalDiscountValue); report.println("Total discount value : $" + totalDiscountValue);
report.println("Invalid rows: " + totalInvalidLines); report.println("Invalid rows: " + totalInvalidLines);
report.println("====================="); report.println("=====================");
report.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);