57 lines
2.0 KiB
Java
57 lines
2.0 KiB
Java
package org.example;
|
|
|
|
import java.util.Collection;
|
|
import java.util.Comparator;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
public class TUI {
|
|
|
|
Shop shop;
|
|
boolean exit = false;
|
|
|
|
public TUI(Shop shop) {
|
|
this.shop = shop;
|
|
}
|
|
|
|
public void processInput(String input) {
|
|
String[] tokens = input.split("\\s+");
|
|
String command = tokens[0];
|
|
switch (command) {
|
|
case "exit" -> exit();
|
|
case "checkout" -> checkout();
|
|
case "list" -> listProducts(shop.products);
|
|
case "cart" -> listCart(shop.getCart());
|
|
case "add" -> shop.addProductToCart(shop.getProductById(Integer.parseInt(tokens[1])));
|
|
case "remove" -> shop.removeProductFromCart(shop.getProductById(Integer.parseInt(tokens[1])));
|
|
case "set" ->
|
|
shop.setProductQuantityInCart(shop.getProductById(Integer.parseInt(tokens[1])), Integer.parseInt(tokens[2]));
|
|
default -> System.out.println("Unbekannter Befehl: " + input);
|
|
}
|
|
}
|
|
|
|
public void exit() {
|
|
exit = true;
|
|
}
|
|
|
|
public void listProducts(HashMap<Product, Integer> productStock) {
|
|
productStock.keySet().stream().sorted(Comparator.comparingInt(p -> p.id)).forEach(product -> {
|
|
System.out.println("(" + product.id + ") " + product.name + ", Preis: " + product.price + ", Gewicht: " + product.weight + ", Auf Lager: " + productStock.get(product));
|
|
});
|
|
}
|
|
|
|
public void listCart(HashMap<Product, Integer> cardContents) {
|
|
cardContents.forEach((product, count) -> {
|
|
System.out.println("(" + product.id + ") " + product.name + ", Preis: " + product.price + ", Gewicht: " + product.weight + ", Anzahl: " + count);
|
|
});
|
|
}
|
|
|
|
public void checkout() {
|
|
Order order = shop.checkout();
|
|
System.out.println("MwSt.: " + order.calculateTotalVat() + "€");
|
|
System.out.println("Versand: " + order.calculateShippingCost() + "€");
|
|
System.out.println("Brutto: " + order.calculateTotalPriceWithVat() + "€");
|
|
}
|
|
|
|
}
|