From dd7078822697e560e9772026e536b1a563b79b77 Mon Sep 17 00:00:00 2001 From: CPlaiz Date: Sat, 13 Dec 2025 17:19:25 +0100 Subject: [PATCH] Add UI --- src/main/java/org/example/TUI.java | 56 ++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/org/example/TUI.java diff --git a/src/main/java/org/example/TUI.java b/src/main/java/org/example/TUI.java new file mode 100644 index 0000000..266b44c --- /dev/null +++ b/src/main/java/org/example/TUI.java @@ -0,0 +1,56 @@ +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 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 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() + "€"); + } + +}