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.
main
Victor Hans-Georg Waitz 2024-04-15 22:08:37 +02:00
parent 42aae21ede
commit 73ef7ea19c
2 changed files with 86 additions and 5 deletions

View File

@ -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<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 ++;
}
}
}

View File

@ -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<String> searchResults = shop.getSearchResult(searchQueue);
for (String result : searchResults) {
System.out.println(result);
System.out.println();
}
}
}