I fixed the main part and part of the report.

This commit is contained in:
Matin
2026-05-21 02:26:19 +04:30
parent 8e6d1d1760
commit b3918fd47f
2 changed files with 152 additions and 9 deletions
+48 -2
View File
@@ -14,6 +14,53 @@ public class Main {
throw new IOException("products.csv not found"); throw new IOException("products.csv not found");
} }
Scanner scanner = new Scanner(input); 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: // TODO:
// - Read each line from products.csv // - Read each line from products.csv
@@ -26,8 +73,7 @@ public class Main {
// - The data in products.csv is guaranteed to be valid. // - 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 { try {
loadProducts(); loadProducts();
} catch (IOException e) { } catch (IOException e) {
+104 -7
View File
@@ -1,22 +1,116 @@
import java.io.IOException;
import java.io.InputStream;
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;
this.productList = productList; 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: // 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
@@ -80,4 +174,7 @@ public class ReportGenerator {
================================ ================================
*/ */
} }
} }
public void saveReport() {
}