From ad1efd076f13f2cee4cf9ce382502cfa7100493e Mon Sep 17 00:00:00 2001 From: Max Gerbeth Date: Wed, 18 Jan 2023 09:39:28 +0100 Subject: [PATCH] refactoring: ShopCommand.java --- .../org/bitbiome/commands/ShopCommand.java | 52 ++++++++----------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/bitbiome/commands/ShopCommand.java b/src/main/java/org/bitbiome/commands/ShopCommand.java index 369e7a4..4891942 100644 --- a/src/main/java/org/bitbiome/commands/ShopCommand.java +++ b/src/main/java/org/bitbiome/commands/ShopCommand.java @@ -3,18 +3,22 @@ package org.bitbiome.commands; import org.bitbiome.shop.Item; import org.bitbiome.shop.Shop; +import java.io.IOException; import java.util.ArrayList; import java.util.Scanner; public class ShopCommand implements CommandAPI{ - Shop shop = new Shop(); + + Shop shop = new Shop(); + public ShopCommand(){ + + } @Override public void performCommand(Scanner scanner, boolean isRunning, String message) { System.out.println("Willkommen im Shop!"); - System.out.println("Folgende Items sind aktuell im Shop:"); - shop.printCurrentShopItems(); - ArrayList currentItems; + ArrayList currentItems = shop.loadCurrentShopItems(); + while (true){ System.out.println("Was willst Du hier im Shop?"); @@ -25,34 +29,24 @@ public class ShopCommand implements CommandAPI{ String input = scanner.nextLine(); if(validInput(input)){ if(input.equals("1")){ - currentItems = shop.currentShopItems; - String inp = ""; - System.out.println(""); - System.out.println("Welches Item wollen Sie kaufen? "); + System.out.println("Folgende sind folgende Items im Shop: "); for(int i = 0; i < currentItems.size(); i++){ - System.out.println((i + 1) + " eingaben fuer " + currentItems.get(i).name + " | Kosten: " + currentItems.get(i).gold + " | Anzahl: " + currentItems.get(i).amount); + System.out.println((i + 1) + ". " + currentItems.get(i).name + " | Anzahl: " + currentItems.get(i).amount + " | Gold: " + currentItems.get(i).gold); } - System.out.println("0 eingeben fuer eine andere Option."); - - while (true){ - inp = scanner.nextLine(); - if(inp.equals("0")){ - break; - } + System.out.println("0 Eingeben um den Shop zu verlassen."); + System.out.println(""); + System.out.print("Welches Item moechtest du kaufen? "); + String itemNumber = scanner.nextLine(); + if(!itemNumber.equals("0")){ System.out.print("Anzahl eingeben: "); - int existingCount = currentItems.get(Integer.parseInt(inp) - 1).amount; - String inpAmount = scanner.nextLine(); - if(Integer.parseInt(inpAmount) <= existingCount){ - if(shop.buy(currentItems.get(Integer.parseInt(inp) - 1).name, Integer.parseInt(inpAmount))){ - System.out.println(""); - System.out.println("Vielen Dank fuer deinen Einkauf!"); - System.out.println(""); - break; - }else { - break; - } - }else { - System.out.println("Error!"); + String amount = scanner.nextLine(); + if((Integer.parseInt(amount) <= currentItems.get(Integer.parseInt(itemNumber) - 1).amount) && ((Integer.parseInt(amount) - 1) > -1)){ + shop.buy(currentItems.get(Integer.parseInt(itemNumber) - 1).name, Integer.parseInt(amount)); + currentItems = shop.loadCurrentShopItems(); + System.out.println(""); + System.out.println("Vielen Dank für Ihren Einkauf!"); + }else{ + System.out.println("Fehler"); } } } else if(input.equals("2")){