The code is complete, but it has some bugs that need fixing.
This commit is contained in:
+5
-41
@@ -17,49 +17,13 @@ public class Main {
|
||||
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() )
|
||||
{
|
||||
String[] part = ln.split(",");
|
||||
String product_id = part[0] ;
|
||||
String product_name = part[1] ;
|
||||
String product_price = part[2] ;
|
||||
|
||||
if (x != ',')
|
||||
{
|
||||
part = part + x ;
|
||||
}
|
||||
else if (x == ',')
|
||||
{
|
||||
switch (part_number)
|
||||
{
|
||||
case 0 :
|
||||
product_id = part ;
|
||||
part = "" ;
|
||||
part_number ++ ;
|
||||
break;
|
||||
productCatalog.add(new Product(Integer.parseInt(product_id), product_name , Float.parseFloat(product_price))) ;
|
||||
|
||||
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:
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.List;
|
||||
@@ -27,53 +29,20 @@ public class ReportGenerator {
|
||||
}
|
||||
|
||||
public void processFile() throws IOException {
|
||||
InputStream input = ReportGenerator.class
|
||||
.getClassLoader()
|
||||
.getResourceAsStream("products.csv");
|
||||
InputStream input = getClass().getClassLoader().getResourceAsStream (ordersFilePath);
|
||||
if (input == null) {
|
||||
throw new IOException("products.csv not found");
|
||||
throw new IOException("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;
|
||||
String[] part = ln.split(",") ;
|
||||
String product_id = part[0] ;
|
||||
String product_number = part[1] ;
|
||||
String product_discount = part[2] ;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (isNumber(product_id) && isNumber(product_number ) && isNumber(product_discount))
|
||||
{
|
||||
@@ -97,6 +66,17 @@ public class ReportGenerator {
|
||||
totalInvalidLines ++ ;
|
||||
continue;
|
||||
}
|
||||
for (Product p : productList)
|
||||
{
|
||||
if (p.getProductID() == id)
|
||||
{
|
||||
totalFinalCost = totalFinalCost + ((p.getPrice()*(100-discount)/100)*number) ;
|
||||
totalDiscountValue = totalDiscountValue + ((p.getPrice()*discount/100)*number);
|
||||
}
|
||||
|
||||
}
|
||||
totalQuantity ++ ;
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -151,8 +131,22 @@ public class ReportGenerator {
|
||||
// 12. Close all file resources
|
||||
}
|
||||
|
||||
public void saveReport()
|
||||
{
|
||||
public void saveReport() throws IOException {
|
||||
BufferedWriter writer = new BufferedWriter(new FileWriter("report.txt")) ;
|
||||
writer.write("========= SALES REPORT =========");
|
||||
writer.newLine();
|
||||
writer.write("Total Quantity Sold:" + totalQuantity);
|
||||
writer.newLine();
|
||||
writer.write("Total Final Cost: $" + totalFinalCost);
|
||||
writer.newLine();
|
||||
writer.write("Total Discount Value: $" + totalDiscountValue);
|
||||
writer.newLine();
|
||||
writer.write("Invalid Rows: " + totalInvalidLines);
|
||||
writer.newLine();
|
||||
writer.close();
|
||||
|
||||
|
||||
|
||||
// TODO:
|
||||
// 1. Build a formatted report string
|
||||
// 2. Include:
|
||||
@@ -176,5 +170,3 @@ public class ReportGenerator {
|
||||
}
|
||||
}
|
||||
|
||||
public void saveReport() {
|
||||
}
|
||||
Reference in New Issue
Block a user