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");
}
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) {
+103 -6
View File
@@ -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
@@ -81,3 +175,6 @@ public class ReportGenerator {
*/
}
}
public void saveReport() {
}