I fixed the main part and part of the report.
This commit is contained in:
+48
-2
@@ -14,6 +14,53 @@ public class Main {
|
||||
throw new IOException("products.csv not found");
|
||||
}
|
||||
Scanner scanner = new Scanner(input);
|
||||
while (scanner.hasNextLine())
|
||||
{
|
||||
String ln = scanner.nextLine() ;
|
||||
int part_number = 0 ;
|
||||
String part = "";
|
||||
String product_id = "" ;
|
||||
String product_name = "" ;
|
||||
String product_price = "" ;
|
||||
for (char x : ln.toCharArray() )
|
||||
{
|
||||
|
||||
if (x != ',')
|
||||
{
|
||||
part = part + x ;
|
||||
}
|
||||
else if (x == ',')
|
||||
{
|
||||
switch (part_number)
|
||||
{
|
||||
case 0 :
|
||||
product_id = part ;
|
||||
part = "" ;
|
||||
part_number ++ ;
|
||||
break;
|
||||
|
||||
case 1 :
|
||||
product_name = part ;
|
||||
part = "" ;
|
||||
part_number ++ ;
|
||||
break;
|
||||
|
||||
case 2 :
|
||||
product_price = part ;
|
||||
part = "" ;
|
||||
part_number ++ ;
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
productCatalog.add(new Product(Integer.parseInt(product_id), product_name , Float.parseFloat(product_price))) ;
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// TODO:
|
||||
// - Read each line from products.csv
|
||||
@@ -26,8 +73,7 @@ public class Main {
|
||||
// - The data in products.csv is guaranteed to be valid.
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
public static void main(String[] args) throws IOException {
|
||||
try {
|
||||
loadProducts();
|
||||
} catch (IOException e) {
|
||||
|
||||
@@ -1,22 +1,116 @@
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class ReportGenerator {
|
||||
|
||||
private final String ordersFilePath;
|
||||
private final List<Product> productList;
|
||||
|
||||
private double totalFinalCost;
|
||||
private int totalQuantity;
|
||||
private double totalDiscountValue;
|
||||
private int totalInvalidLines;
|
||||
private double totalFinalCost = 0;
|
||||
private int totalQuantity = 0 ;
|
||||
private double totalDiscountValue = 0 ;
|
||||
private int totalInvalidLines = 0 ;
|
||||
|
||||
public ReportGenerator(String ordersFilePath, List<Product> productList) {
|
||||
this.ordersFilePath = ordersFilePath;
|
||||
this.productList = productList;
|
||||
}
|
||||
public static boolean isNumber(String str) {
|
||||
try {
|
||||
Integer.parseInt(str);
|
||||
return true;
|
||||
} catch (NumberFormatException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void processFile() throws IOException {
|
||||
InputStream input = ReportGenerator.class
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("products.csv");
|
||||
if (input == null) {
|
||||
throw new IOException("products.csv not found");
|
||||
}
|
||||
Scanner scanner = new Scanner(input);
|
||||
while (scanner.hasNextLine())
|
||||
{
|
||||
String ln = scanner.nextLine() ;
|
||||
int part_number = 0 ;
|
||||
String part = "";
|
||||
String product_id = "" ;
|
||||
String product_number = "" ;
|
||||
String product_discount = "" ;
|
||||
for (char x : ln.toCharArray() )
|
||||
{
|
||||
|
||||
if (x != ',')
|
||||
{
|
||||
part = part + x ;
|
||||
}
|
||||
else if (x == ',')
|
||||
{
|
||||
switch (part_number)
|
||||
{
|
||||
case 0 :
|
||||
product_id = part ;
|
||||
part = "" ;
|
||||
part_number ++ ;
|
||||
break;
|
||||
|
||||
case 1 :
|
||||
product_number = part ;
|
||||
part = "" ;
|
||||
part_number ++ ;
|
||||
break;
|
||||
|
||||
case 2 :
|
||||
product_discount = part ;
|
||||
part = "" ;
|
||||
part_number ++ ;
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (isNumber(product_id) && isNumber(product_number ) && isNumber(product_discount))
|
||||
{
|
||||
int id = Integer.parseInt(product_id) ;
|
||||
int number = Integer.parseInt(product_number) ;
|
||||
int discount = Integer.parseInt(product_discount) ;
|
||||
|
||||
if (id < 1 || id > 9 )
|
||||
{
|
||||
totalInvalidLines ++ ;
|
||||
continue;
|
||||
|
||||
}
|
||||
if (number < 1)
|
||||
{
|
||||
totalInvalidLines ++ ;
|
||||
continue;
|
||||
}
|
||||
if (discount >= 100 || discount < 0 )
|
||||
{
|
||||
totalInvalidLines ++ ;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
totalInvalidLines ++ ;
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void processFile()
|
||||
{
|
||||
// TODO:
|
||||
// 1. Open the order details CSV file
|
||||
// 2. Read the file line by line
|
||||
@@ -80,4 +174,7 @@ public class ReportGenerator {
|
||||
================================
|
||||
*/
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void saveReport() {
|
||||
}
|
||||
Reference in New Issue
Block a user