OnlineShop/shop/fassade/OnlineShopSystem.java

91 lines
2.7 KiB
Java

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<Product> storage;
public OnlineShopSystem(String filePath) throws FileNotFoundException {
storage = new ArrayList<Product>();
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<String> getSearchResult(String searchQueue){
ArrayList<Product> 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<String> 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 ++;
}
}
}