package fassade; import domain.Product; import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; public class OnlineShopSystem { private ArrayList storage; public OnlineShopSystem(String filePath) throws FileNotFoundException { storage = new ArrayList(); 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 ++; } } }