Add name/address input to checkout
parent
a987746e01
commit
72e7a101b8
|
|
@ -4,7 +4,6 @@ import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
@ -14,7 +13,8 @@ public class Main {
|
||||||
TUI tui = new TUI(shop);
|
TUI tui = new TUI(shop);
|
||||||
Scanner scanner = new Scanner(System.in);
|
Scanner scanner = new Scanner(System.in);
|
||||||
while (true) {
|
while (true) {
|
||||||
System.out.print("> ");
|
if (!tui.isCheckingOut())
|
||||||
|
System.out.print("> ");
|
||||||
String input = scanner.nextLine();
|
String input = scanner.nextLine();
|
||||||
tui.processInput(input);
|
tui.processInput(input);
|
||||||
if (tui.exit) {
|
if (tui.exit) {
|
||||||
|
|
|
||||||
|
|
@ -9,17 +9,24 @@ public class TUI {
|
||||||
|
|
||||||
Shop shop;
|
Shop shop;
|
||||||
boolean exit = false;
|
boolean exit = false;
|
||||||
|
int checkoutStep = 0;
|
||||||
|
String checkoutName = null;
|
||||||
|
String checkoutAddress = null;
|
||||||
|
|
||||||
public TUI(Shop shop) {
|
public TUI(Shop shop) {
|
||||||
this.shop = shop;
|
this.shop = shop;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void processInput(String input) {
|
public void processInput(String input) {
|
||||||
|
if (isCheckingOut()) {
|
||||||
|
checkout(input);
|
||||||
|
return;
|
||||||
|
}
|
||||||
String[] tokens = input.split("\\s+");
|
String[] tokens = input.split("\\s+");
|
||||||
String command = tokens[0];
|
String command = tokens[0];
|
||||||
switch (command) {
|
switch (command) {
|
||||||
case "exit" -> exit();
|
case "exit" -> exit();
|
||||||
case "checkout" -> checkout();
|
case "checkout" -> checkout(null);
|
||||||
case "list" -> listProducts(shop.products);
|
case "list" -> listProducts(shop.products);
|
||||||
case "cart" -> listCart(shop.getCart());
|
case "cart" -> listCart(shop.getCart());
|
||||||
case "add" -> shop.addProductToCart(shop.getProductById(Integer.parseInt(tokens[1])));
|
case "add" -> shop.addProductToCart(shop.getProductById(Integer.parseInt(tokens[1])));
|
||||||
|
|
@ -46,11 +53,36 @@ public class TUI {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void checkout() {
|
public void checkout(String input) {
|
||||||
Order order = shop.checkout();
|
switch (checkoutStep) {
|
||||||
System.out.println("MwSt.: " + order.calculateTotalVat() + "€");
|
case 0 -> {
|
||||||
System.out.println("Versand: " + order.calculateShippingCost() + "€");
|
System.out.println();
|
||||||
System.out.println("Brutto: " + order.calculateTotalPriceWithVat() + "€");
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue