diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 148a46e..bb3c24e 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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) { diff --git a/src/main/java/ReportGenerator.java b/src/main/java/ReportGenerator.java index 8eb458b..04f9df5 100644 --- a/src/main/java/ReportGenerator.java +++ b/src/main/java/ReportGenerator.java @@ -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 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 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 { ================================ */ } -} \ No newline at end of file +} + + public void saveReport() { + } \ No newline at end of file