From 72e7a101b8a223945f7598bab3e524662436a360 Mon Sep 17 00:00:00 2001 From: CPlaiz Date: Sat, 13 Dec 2025 17:47:30 +0100 Subject: [PATCH] Add name/address input to checkout --- src/main/java/org/example/Main.java | 4 +-- src/main/java/org/example/TUI.java | 44 +++++++++++++++++++++++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/example/Main.java b/src/main/java/org/example/Main.java index 6a1e17d..370f53b 100644 --- a/src/main/java/org/example/Main.java +++ b/src/main/java/org/example/Main.java @@ -4,7 +4,6 @@ import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.HashMap; -import java.util.List; import java.util.Scanner; public class Main { @@ -14,7 +13,8 @@ public class Main { TUI tui = new TUI(shop); Scanner scanner = new Scanner(System.in); while (true) { - System.out.print("> "); + if (!tui.isCheckingOut()) + System.out.print("> "); String input = scanner.nextLine(); tui.processInput(input); if (tui.exit) { diff --git a/src/main/java/org/example/TUI.java b/src/main/java/org/example/TUI.java index 266b44c..f48b648 100644 --- a/src/main/java/org/example/TUI.java +++ b/src/main/java/org/example/TUI.java @@ -9,17 +9,24 @@ public class TUI { Shop shop; boolean exit = false; + int checkoutStep = 0; + String checkoutName = null; + String checkoutAddress = null; public TUI(Shop shop) { this.shop = shop; } public void processInput(String input) { + if (isCheckingOut()) { + checkout(input); + return; + } String[] tokens = input.split("\\s+"); String command = tokens[0]; switch (command) { case "exit" -> exit(); - case "checkout" -> checkout(); + case "checkout" -> checkout(null); case "list" -> listProducts(shop.products); case "cart" -> listCart(shop.getCart()); case "add" -> shop.addProductToCart(shop.getProductById(Integer.parseInt(tokens[1]))); @@ -46,11 +53,36 @@ public class TUI { }); } - 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() + "€"); + public void checkout(String input) { + switch (checkoutStep) { + case 0 -> { + System.out.println(); + System.out.println("Bestellung abschließen"); + System.out.print("Name: "); + checkoutStep = 1; + } + case 1 -> { + checkoutName = input; + System.out.print("Adresse: "); + checkoutStep = 2; + } + case 2 -> { + checkoutAddress = input; + Order order = shop.checkout(); + System.out.println("Vielen Dank für Ihre Bestellung, " + checkoutName + "!"); + System.out.println("Ihre Bestellung wird an folgende Adresse gesendet: " + checkoutAddress); + System.out.println("---"); + System.out.println("MwSt.: " + order.calculateTotalVat() + "€"); + System.out.println("Versand: " + order.calculateShippingCost() + "€"); + System.out.println("Brutto: " + order.calculateTotalPriceWithVat() + "€"); + System.out.println("---"); + checkoutStep = 0; + } + } + } + + public boolean isCheckingOut() { + return checkoutStep > 0; } }