From 73ef7ea19c457851186471e77eac2ab472018230 Mon Sep 17 00:00:00 2001 From: 3013050 <3013050@stud.hs-mannheim.de> Date: Mon, 15 Apr 2024 22:08:37 +0200 Subject: [PATCH] Added all major features Added the feature to import a csv file containing the current storage, added a feature to list all products from storage and search from them. --- shop/fassade/OnlineShopSystem.java | 61 +++++++++++++++++++++++++++++- shop/tui/TUI.java | 30 +++++++++++++-- 2 files changed, 86 insertions(+), 5 deletions(-) diff --git a/shop/fassade/OnlineShopSystem.java b/shop/fassade/OnlineShopSystem.java index 14db803..c7f3f4c 100644 --- a/shop/fassade/OnlineShopSystem.java +++ b/shop/fassade/OnlineShopSystem.java @@ -16,16 +16,75 @@ public class OnlineShopSystem { loadProducts(filePath); } + public String listAllProducts(){ + StringBuilder sb = new StringBuilder(); + + for (Product product : storage){ + String name = product.getName(); + String description = product.getDescription(); + + sb.append(String.format("%s - %s \n", name, description)); + + } + + return sb.toString(); + } + + public ArrayList getSearchResult(String searchQueue){ + ArrayList matchingProducts = new ArrayList<>(); + + for (Product product : storage){ + if (product.getName().contains(searchQueue) || product.getDescription().contains(searchQueue)){ + matchingProducts.add(product); + } + } + + System.out.printf("amount matches: %d \n", matchingProducts.size()); + + ArrayList matchingStrings = new ArrayList<>(); + + if (matchingProducts.isEmpty()) { + matchingStrings.add(String.format("No matches for your search: %s \n", searchQueue)); + } + + StringBuilder sb = new StringBuilder(); + + sb.append(String.format("%d match(es) found: \n", matchingProducts.size())); + for (Product product : matchingProducts){ + + sb.append(String.format("%s - %s \n", product.getName(), product.getDescription())); + sb.append(String.format("Price: %d€, Weight: %dg, Stock: %d", product.getWeight(), product.getWeight(), product.getBestand())); + + matchingStrings.add(sb.toString()); + sb = new StringBuilder(); + } + + return matchingStrings; + } + private void loadProducts(String filePath) throws FileNotFoundException { Scanner sc = new Scanner(new File(filePath)); int counter = 0; + while (sc.hasNextLine()) { String product = sc.nextLine(); + if (counter == 0){ + counter ++; + continue; + } + + String[] rows = product.split(";"); + rows[2] = rows[2].replaceAll(",", "."); + rows[3] = rows[3].substring(0, rows[3].indexOf(",")); + rows[4] = rows[4].substring(0, rows[4].indexOf(",")); + + Product currentProduct = new Product(rows[0], rows[1], Float.parseFloat(rows[2]), Integer.parseInt(rows[3]), Integer.parseInt(rows[4])); + + storage.add(currentProduct); counter ++; } - } } diff --git a/shop/tui/TUI.java b/shop/tui/TUI.java index 2566682..1bb91f5 100644 --- a/shop/tui/TUI.java +++ b/shop/tui/TUI.java @@ -1,8 +1,10 @@ package tui; +import domain.Product; import fassade.OnlineShopSystem; import java.io.FileNotFoundException; +import java.util.ArrayList; import java.util.Scanner; public class TUI { @@ -16,9 +18,8 @@ public class TUI { try { new TUI(); } catch ( FileNotFoundException e){ - // resources/productList.csv - System.out.printf("Storage list couldn't be loaded. \n"); - System.out.printf("Cancelling. \n\n"); + System.out.print("Storage list couldn't be loaded. \n"); + System.out.print("Cancelling. \n\n"); } } @@ -28,8 +29,11 @@ public class TUI { String filePath = welcomeMessage(); shop = new OnlineShopSystem(filePath); + itemListing(); - + while (true){ + itemSearch(); + } } private String welcomeMessage(){ @@ -37,4 +41,22 @@ public class TUI { System.out.print("Please input the product list location: "); return sc.nextLine(); } + + private void itemListing(){ + System.out.print("We have this items in our storage: \n\n"); + System.out.println(shop.listAllProducts()); + } + + private void itemSearch(){ + System.out.print("What are you looking for? \n"); + System.out.print("> "); + String searchQueue = sc.nextLine(); + + ArrayList searchResults = shop.getSearchResult(searchQueue); + + for (String result : searchResults) { + System.out.println(result); + System.out.println(); + } + } }