From 4d9ec59d3fe30129085eb554a05b85ebbedb48a8 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Tue, 7 Feb 2023 19:20:47 +0100 Subject: [PATCH] refactoring: code revision of shop Fixed all warnings Outsourced methods due to code duplication Moving classes to new folders Merging of item class --- .../org/bitbiome/classes/InteractionLoop.java | 1 - .../org/bitbiome/{shop => classes}/Shop.java | 173 ++++++++++-------- .../org/bitbiome/commands/ShopCommand.java | 10 +- src/main/java/org/bitbiome/entities/Item.java | 28 ++- src/main/java/org/bitbiome/shop/Item.java | 38 ---- .../bitbiome/commands/ShopCommandTest.java | 3 +- .../org/bitbiome/entitiesTest/ItemTest.java | 6 +- 7 files changed, 130 insertions(+), 129 deletions(-) rename src/main/java/org/bitbiome/{shop => classes}/Shop.java (56%) delete mode 100644 src/main/java/org/bitbiome/shop/Item.java diff --git a/src/main/java/org/bitbiome/classes/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java index 062852b..ee504d6 100644 --- a/src/main/java/org/bitbiome/classes/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -1,7 +1,6 @@ package org.bitbiome.classes; import org.bitbiome.Boot; -import org.bitbiome.classes.*; import org.json.JSONObject; import java.util.Scanner; diff --git a/src/main/java/org/bitbiome/shop/Shop.java b/src/main/java/org/bitbiome/classes/Shop.java similarity index 56% rename from src/main/java/org/bitbiome/shop/Shop.java rename to src/main/java/org/bitbiome/classes/Shop.java index 5013a22..6358078 100644 --- a/src/main/java/org/bitbiome/shop/Shop.java +++ b/src/main/java/org/bitbiome/classes/Shop.java @@ -1,10 +1,8 @@ -package org.bitbiome.shop; +package org.bitbiome.classes; -import org.bitbiome.classes.Colors; -import org.bitbiome.classes.JsonParser; -import org.bitbiome.classes.TravelEngine; import org.bitbiome.commands.BlackJackCommand; import org.bitbiome.commands.QuizCommand; +import org.bitbiome.entities.Item; import org.json.JSONArray; import org.json.JSONObject; @@ -19,7 +17,6 @@ import java.util.Scanner; public class Shop { public ArrayList allItems; public ArrayList currentShopItems; - public JsonParser jsonParser = new JsonParser(); public QuizCommand quizCommand = new QuizCommand(); public BlackJackCommand blackJackCommand = new BlackJackCommand(); public Scanner scanner; @@ -36,19 +33,19 @@ public class Shop { try { allItems = loadAllItems(); currentShopItems = loadPartofItems(allItems, 3); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } - public boolean buy(String itemName, int amount){ - //Create File Objects + public boolean buy(String itemName, int amount) { + // Create File Objects currentShopItems = loadCurrentShopItems(); File filePlayerConfig = new File("src/main/resources/playerconfig.json"); File fileGameConfig = new File("src/main/resources/gameconfig.json"); File fileItem = new File("src/main/resources/items.json"); try { - //Create JSONObjects + // Create JSONObjects String content1 = new String(Files.readAllBytes(Paths.get(filePlayerConfig.toURI())), "UTF-8"); JSONObject playerConfig = new JSONObject(content1); @@ -58,37 +55,40 @@ public class Shop { String content3 = new String(Files.readAllBytes(Paths.get(fileItem.toURI())), "UTF-8"); JSONArray itemJSON = new JSONArray(content3); - //Test if item still available in the shop + // Test if item still available in the shop int itemIndex = -1; - for(int i = 0; i < currentShopItems.size(); i++){ - if(currentShopItems.get(i).getName().equals(itemName)){ + for (int i = 0; i < currentShopItems.size(); i++) { + if (currentShopItems.get(i).getName().equals(itemName)) { itemIndex = i; } } - if(itemIndex == -1){ - System.out.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Dieses Item gibt es nicht!" + Colors.ANSI_RESET); + if (itemIndex == -1) { + System.out.println( + Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Dieses Item gibt es nicht!" + Colors.ANSI_RESET); return false; } - if(!(currentShopItems.get(itemIndex).amount > 0)){ - System.out.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Es gibt zu wenige Items!" + Colors.ANSI_RESET); + if (!(currentShopItems.get(itemIndex).getAmount() > 0)) { + System.out.println( + Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Es gibt zu wenige Items!" + Colors.ANSI_RESET); return false; } - //Test if the player has enough gold - int costs = currentShopItems.get(itemIndex).gold * amount; + // Test if the player has enough gold + int costs = currentShopItems.get(itemIndex).getGold() * amount; int gold = (int) playerConfig.get("gold"); - if(!(gold >= costs)){ - System.out.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Du hast zu wenig Gold!" + Colors.ANSI_RESET); + if (!(gold >= costs)) { + System.out + .println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Du hast zu wenig Gold!" + Colors.ANSI_RESET); return false; } - //Player gold subtract + // Player gold subtract playerConfig.put("gold", subtractGold(gold, costs)); - //Gameconfig amount reduese + // Gameconfig amount reduese JSONArray jsonArray2 = gameConfig.getJSONArray("shopitems"); int intNewAmount; - for(int i = 0; i < jsonArray2.length(); i++) { + for (int i = 0; i < jsonArray2.length(); i++) { JSONObject tempJSON = jsonArray2.getJSONObject(i); if (tempJSON.getString("name").equals(itemName)) { intNewAmount = tempJSON.getInt("amount") - amount; @@ -96,16 +96,16 @@ public class Shop { tempJSON.put("amount", intNewAmount); jsonArray2.put(tempJSON); gameConfig.put("shopitems", jsonArray2); - jsonParser.writeObject("gameconfig.json", gameConfig); + JsonParser.writeObject("src/main/resources/gameconfig.json", gameConfig); currentShopItems = loadCurrentShopItems(); break; } } - //Give Player the Item + // Give Player the Item JSONArray jsonArray = playerConfig.getJSONArray("inventory"); int newAmount; - for(int i = 0; i < jsonArray.length(); i++) { + for (int i = 0; i < jsonArray.length(); i++) { JSONObject tempJSON = jsonArray.getJSONObject(i); if (tempJSON.getString("name").equals(itemName)) { newAmount = tempJSON.getInt("amount") + amount; @@ -113,14 +113,14 @@ public class Shop { tempJSON.put("amount", newAmount); jsonArray.put(tempJSON); playerConfig.put("inventory", jsonArray); - jsonParser.writeObject("playerconfig.json", playerConfig); + JsonParser.writeObject("src/main/resources/playerconfig.json", playerConfig); return true; } } - //Item do not exist in the playerinventory + // Item do not exist in the playerinventory int durability = 0; - for(int i = 0; i < itemJSON.length(); i++) { + for (int i = 0; i < itemJSON.length(); i++) { JSONObject tempJSON = itemJSON.getJSONObject(i); if (tempJSON.getString("name").equals(itemName)) { durability = (int) tempJSON.get("durability"); @@ -134,121 +134,140 @@ public class Shop { jsonArray.put(inventory); playerConfig.put("inventory", jsonArray); - jsonParser.writeObject("playerconfig.json", playerConfig); + JsonParser.writeObject("src/main/resources/playerconfig.json", playerConfig); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } return true; } - private ArrayList loadAllItems(){ + private ArrayList loadAllItems() { + ArrayList arrayList = new ArrayList<>(); + JSONArray itemJSON = returnJSONArrayOfAllItems(); + + arrayList = addToList(itemJSON, arrayList, "amountShop"); + return arrayList; + } + + public JSONArray returnJSONArrayOfAllItems() { File file = new File("src/main/resources/items.json"); - ArrayList arrayList = new ArrayList(); + JSONArray itemJSON = null; try { String content3 = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8"); - JSONArray itemJSON = new JSONArray(content3); - - for(int i = 0; i < itemJSON.length(); i++){ - JSONObject tempJSON = itemJSON.getJSONObject(i); - arrayList.add(new Item(tempJSON.getString("name"), tempJSON.getInt("amountShop"), tempJSON.getInt("gold"))); - } - }catch (Exception e){ + itemJSON = new JSONArray(content3); + } catch (Exception e) { e.printStackTrace(); } + return itemJSON; + } - return arrayList; + public ArrayList loadCurrentShopItems() { + + ArrayList arrayList = new ArrayList<>(); + JSONObject jsonObject = JsonParser.getJSONObject("src/main/resources/gameconfig.json"); + JSONArray jsonArray = jsonObject.getJSONArray("shopitems"); + + return addToList(jsonArray, arrayList, "amount"); } - public ArrayList loadCurrentShopItems(){ - File file = new File("src/main/resources/gameconfig.json"); - ArrayList arrayList = new ArrayList(); - try { - String content = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8"); - JSONObject jsonObject = new JSONObject(content); + public JSONObject getItemByName(String itemName, JSONArray itemArray) { + for (int i = 0; i < itemArray.length(); i++) { + if (itemArray.getJSONObject(i).getString("name").equals(itemName)) { + return itemArray.getJSONObject(i); + } + } + return null; + } - JSONArray jsonArray = jsonObject.getJSONArray("shopitems"); - for(int i = 0; i < jsonArray.length(); i++){ - JSONObject tempJSON = jsonArray.getJSONObject(i); - arrayList.add(new Item(tempJSON.getString("name"), tempJSON.getInt("amount"), tempJSON.getInt("gold"))); + public ArrayList addToList(JSONArray itemJSON, ArrayList arrayList, String key) { + JSONArray allItems = returnJSONArrayOfAllItems(); + for (int i = 0; i < itemJSON.length(); i++) { + JSONObject tempJSON = itemJSON.getJSONObject(i); + String damage; + boolean doesDmg; + if (key.equals("amount")) { + damage = getItemByName(tempJSON.getString("name"), allItems).getString("damage"); + } else { + damage = tempJSON.getString("damage"); } - }catch (Exception e){ - e.printStackTrace(); + doesDmg = !damage.equals("0") ? false : true; + arrayList.add(new Item(tempJSON.getString("name"), doesDmg, damage, tempJSON.getInt(key), + tempJSON.getInt("gold"))); } - return arrayList; } - private ArrayList loadPartofItems(ArrayList alleItems, int itemCount) { - ArrayList arrayList = new ArrayList(); - try{ + private ArrayList loadPartofItems(ArrayList alleItems, int itemCount) { + ArrayList arrayList = new ArrayList<>(); + try { File fileGameConfig = new File("src/main/resources/gameconfig.json"); String content2 = new String(Files.readAllBytes(Paths.get(fileGameConfig.toURI())), "UTF-8"); JSONObject gameConfig = new JSONObject(content2); - JSONArray jsonArray = gameConfig.getJSONArray("shopitems"); + //JSONArray jsonArray = gameConfig.getJSONArray("shopitems"); HashSet hashSet = new HashSet<>(); JSONArray shopitems = new JSONArray(); Random random = new Random(); - while (hashSet.size() < itemCount){ + while (hashSet.size() < itemCount) { int rand = random.nextInt(alleItems.size()); - if(!hashSet.contains(rand)){ + if (!hashSet.contains(rand)) { hashSet.add(rand); arrayList.add(alleItems.get(rand)); JSONObject jsonObject = new JSONObject(); - jsonObject.put("name", alleItems.get(rand).name); - jsonObject.put("amount", alleItems.get(rand).amount); - jsonObject.put("gold", alleItems.get(rand).gold); + jsonObject.put("name", alleItems.get(rand).getName()); + jsonObject.put("amount", alleItems.get(rand).getAmount()); + jsonObject.put("gold", alleItems.get(rand).getGold()); shopitems.put(jsonObject); } } - //write in gameconfig.json + // write in gameconfig.json gameConfig.remove("shopitems"); gameConfig.put("shopitems", shopitems); - jsonParser.writeObject("gameconfig.json", gameConfig); - + JsonParser.writeObject("src/main/resources/gameconfig.json", gameConfig); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } - return arrayList; } public void itemRotation() { try { currentShopItems = loadPartofItems(allItems, 3); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } - public void printCurrentShopItems(){ + public void printCurrentShopItems() { printArrayList(currentShopItems); } - public void quiz(){ + public void quiz() { quizCommand.performCommand(scanner, isRunning, message, travelEngine); } - public void blackJack(){ + public void blackJack() { System.out.println(""); blackJackCommand.performCommand(scanner, isRunning, message, travelEngine); System.out.println(""); } - private void printArrayList(ArrayList arrayList){ + + private void printArrayList(ArrayList arrayList) { System.out.println(""); - for(int i = 0; i < arrayList.size(); i++){ - if(arrayList.get(i).getAmount() != 0) { - System.out.println(arrayList.get(i).getName() + " | Anzahl: " + arrayList.get(i).getAmount() + " | Kosten: " + arrayList.get(i).getGold()); + for (int i = 0; i < arrayList.size(); i++) { + if (arrayList.get(i).getAmount() != 0) { + System.out.println(arrayList.get(i).getName() + " | Anzahl: " + arrayList.get(i).getAmount() + + " | Kosten: " + arrayList.get(i).getGold()); } } System.out.println(""); } - public int subtractGold(int gold, int cost){ + public int subtractGold(int gold, int cost) { return gold - cost; } } diff --git a/src/main/java/org/bitbiome/commands/ShopCommand.java b/src/main/java/org/bitbiome/commands/ShopCommand.java index b4d8fbd..4de36ef 100644 --- a/src/main/java/org/bitbiome/commands/ShopCommand.java +++ b/src/main/java/org/bitbiome/commands/ShopCommand.java @@ -2,9 +2,9 @@ package org.bitbiome.commands; import org.bitbiome.classes.BlackJack; import org.bitbiome.classes.Colors; +import org.bitbiome.classes.Shop; import org.bitbiome.classes.TravelEngine; -import org.bitbiome.shop.Item; -import org.bitbiome.shop.Shop; +import org.bitbiome.entities.Item; import java.util.ArrayList; import java.util.Scanner; @@ -37,7 +37,7 @@ public class ShopCommand implements CommandAPI{ if(input.equals("1")){ System.out.println("Folgende Items sind im Shop: "); for(int i = 0; i < currentItems.size(); i++){ - System.out.println((i + 1) + ". " + currentItems.get(i).name + " | Anzahl: " + currentItems.get(i).amount + " | Gold: " + currentItems.get(i).gold); + System.out.println((i + 1) + ". " + currentItems.get(i).getName() + " | Anzahl: " + currentItems.get(i).getAmount() + " | Gold: " + currentItems.get(i).getGold()); } System.out.println("0 Eingeben um den Shop zu verlassen."); System.out.println(""); @@ -47,8 +47,8 @@ public class ShopCommand implements CommandAPI{ System.out.print("Anzahl eingeben: "); String amount = scanner.nextLine(); try { - if ((Integer.parseInt(amount) <= currentItems.get(Integer.parseInt(itemNumber) - 1).amount) && ((Integer.parseInt(amount) - 1) > -1)) { - boolean bool = shop.buy(currentItems.get(Integer.parseInt(itemNumber) - 1).name, Integer.parseInt(amount)); + if ((Integer.parseInt(amount) <= currentItems.get(Integer.parseInt(itemNumber) - 1).getAmount()) && ((Integer.parseInt(amount) - 1) > -1)) { + boolean bool = shop.buy(currentItems.get(Integer.parseInt(itemNumber) - 1).getName(), Integer.parseInt(amount)); currentItems = shop.loadCurrentShopItems(); if (bool) { System.out.println(""); diff --git a/src/main/java/org/bitbiome/entities/Item.java b/src/main/java/org/bitbiome/entities/Item.java index 0ded3ff..cb3f280 100644 --- a/src/main/java/org/bitbiome/entities/Item.java +++ b/src/main/java/org/bitbiome/entities/Item.java @@ -4,13 +4,17 @@ public class Item { private String name; private boolean doesDamage; - private float damage; + private String damage; + private int amount; + private int gold; - public Item(String name, boolean doesDamage, float damage) { + public Item(String name, boolean doesDamage, String damage, int amount, int gold) { this.name = name; this.doesDamage = doesDamage; this.damage = damage; + this.amount = amount; + this.gold = gold; } public Item() { @@ -21,10 +25,26 @@ public class Item { return name; } - public float getDamage() { + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } + + public String getDamage() { return damage; } + public int getGold() { + return gold; + } + + public void setGold(int gold){ + this.gold = gold; + } + public boolean doesDamage() { return doesDamage; } @@ -33,7 +53,7 @@ public class Item { this.name = name; } - public void setDamage(float damage) { + public void setDamage(String damage) { this.damage = damage; } diff --git a/src/main/java/org/bitbiome/shop/Item.java b/src/main/java/org/bitbiome/shop/Item.java deleted file mode 100644 index c6f9672..0000000 --- a/src/main/java/org/bitbiome/shop/Item.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.bitbiome.shop; - -public class Item { - public String name; - public int amount; - public int gold; - - - public Item(String name, int amount, int gold){ - this.name = name; - this.amount = amount; - this.gold = gold; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAmount() { - return amount; - } - - public void setAmount(int amount) { - this.amount = amount; - } - - public int getGold() { - return gold; - } - - public void setGold(int gold) { - this.gold = gold; - } -} \ No newline at end of file diff --git a/src/test/java/org/bitbiome/commands/ShopCommandTest.java b/src/test/java/org/bitbiome/commands/ShopCommandTest.java index 5190e63..69c3de6 100755 --- a/src/test/java/org/bitbiome/commands/ShopCommandTest.java +++ b/src/test/java/org/bitbiome/commands/ShopCommandTest.java @@ -1,10 +1,11 @@ package org.bitbiome.commands; -import org.bitbiome.shop.Shop; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; +import org.bitbiome.classes.Shop; + public class ShopCommandTest { final Shop shop = new Shop(null, true, null, null); @Test diff --git a/src/test/java/org/bitbiome/entitiesTest/ItemTest.java b/src/test/java/org/bitbiome/entitiesTest/ItemTest.java index 4214043..069c5a7 100644 --- a/src/test/java/org/bitbiome/entitiesTest/ItemTest.java +++ b/src/test/java/org/bitbiome/entitiesTest/ItemTest.java @@ -15,7 +15,7 @@ public class ItemTest { public static void setItem() { item = new Item(); item.setName("Unit"); - item.setDamage(12.5F); + item.setDamage("12,5"); item.changeDoesDamage(true); } @@ -26,13 +26,13 @@ public class ItemTest { @Test public void testGetDamage() { - assertEquals(12.5, item.getDamage()); + assertEquals("12,5", item.getDamage()); } @Test public void testDoesDamage() { boolean doesDamage = item.doesDamage(); - assumeTrue(item.getDamage() > 0); + assumeTrue(item.getDamage().equals("12,5")); assumeTrue(doesDamage); } }