Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9c940c1cfb | ||
|
|
151d9feb70 |
@@ -2,10 +2,14 @@ import java.io.IOException;
|
|||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.InputMismatchException;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
static ArrayList<Product> productCatalog = new ArrayList<>();
|
static ArrayList<Product> productCatalog = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
public static void loadProducts() throws IOException{
|
public static void loadProducts() throws IOException{
|
||||||
InputStream input = Main.class
|
InputStream input = Main.class
|
||||||
.getClassLoader()
|
.getClassLoader()
|
||||||
@@ -15,6 +19,20 @@ public class Main {
|
|||||||
}
|
}
|
||||||
Scanner scanner = new Scanner(input);
|
Scanner scanner = new Scanner(input);
|
||||||
|
|
||||||
|
while (scanner.hasNextLine()){
|
||||||
|
String line = scanner.nextLine();
|
||||||
|
String[] lines = line.split(",");
|
||||||
|
|
||||||
|
int id = Integer.parseInt(lines[0]);
|
||||||
|
String name = lines[1];
|
||||||
|
double price = Double.parseDouble(lines[2]);
|
||||||
|
|
||||||
|
Product product = new Product(id , name , price);
|
||||||
|
productCatalog.add(product);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// TODO:
|
// TODO:
|
||||||
// - Read each line from products.csv
|
// - Read each line from products.csv
|
||||||
// - For each line, parse productId, name, and price
|
// - For each line, parse productId, name, and price
|
||||||
|
|||||||
@@ -1,14 +1,18 @@
|
|||||||
|
import java.io.File;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class ReportGenerator {
|
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;
|
||||||
@@ -17,6 +21,64 @@ public class ReportGenerator {
|
|||||||
|
|
||||||
public void processFile()
|
public void processFile()
|
||||||
{
|
{
|
||||||
|
File inputfile = new File(ordersFilePath);
|
||||||
|
Scanner input = null;
|
||||||
|
try {
|
||||||
|
input = new Scanner(inputfile);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
System.out.println("We cannot open csv file");;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (input.hasNextLine()){
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
try{
|
||||||
|
String line = input.nextLine();
|
||||||
|
String[] data = line.split(",");
|
||||||
|
int id = Integer.parseInt(data[0]);
|
||||||
|
int quantity = Integer.parseInt(data[1]);
|
||||||
|
int discount = Integer.parseInt(data[2]);
|
||||||
|
|
||||||
|
if(quantity <= 0 || discount<0 || discount>= 100 || id <= 0 || id > 9){
|
||||||
|
totalInvalidLines++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Product myProduct = null;
|
||||||
|
for (Product p : productList) {
|
||||||
|
if (p.getProductID() == id) {
|
||||||
|
myProduct = p;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (myProduct == null) {
|
||||||
|
totalInvalidLines++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
double subtotal = quantity * myProduct.getPrice();
|
||||||
|
double discountValue = subtotal * (discount / 100.0);
|
||||||
|
double finalCost = subtotal - discountValue;
|
||||||
|
|
||||||
|
totalQuantity += quantity;
|
||||||
|
totalFinalCost += finalCost;
|
||||||
|
totalDiscountValue += discountValue;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}catch (NumberFormatException e){
|
||||||
|
totalInvalidLines++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
input.close();
|
||||||
// TODO:
|
// TODO:
|
||||||
// 1. Open the order details CSV file
|
// 1. Open the order details CSV file
|
||||||
// 2. Read the file line by line
|
// 2. Read the file line by line
|
||||||
@@ -59,6 +121,21 @@ public class ReportGenerator {
|
|||||||
|
|
||||||
public void saveReport()
|
public void saveReport()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
PrintWriter report = new PrintWriter("Report.txt");
|
||||||
|
report.println("=======REPORT========");
|
||||||
|
report.println("Totsl Quantity sold :" + totalQuantity);
|
||||||
|
report.println("Toal final cost: $" + totalFinalCost);
|
||||||
|
report.println("Total discount value : $" + totalDiscountValue);
|
||||||
|
report.println("Invalid rows: " + totalInvalidLines);
|
||||||
|
report.println("=====================");
|
||||||
|
report.close();
|
||||||
|
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
// TODO:
|
// TODO:
|
||||||
// 1. Build a formatted report string
|
// 1. Build a formatted report string
|
||||||
// 2. Include:
|
// 2. Include:
|
||||||
|
|||||||
Reference in New Issue
Block a user