From c9fad47496fd1ab317b296921d820976515b2372 Mon Sep 17 00:00:00 2001 From: Max Gerbeth Date: Thu, 19 Jan 2023 23:32:48 +0100 Subject: [PATCH] bugfixes --- .../org/bitbiome/commands/ShopCommand.java | 8 +-- src/main/java/org/bitbiome/shop/Shop.java | 64 +++++++++++++------ .../bitbiome/commands/HelpCommandTest.java | 0 .../bitbiome/commands/QuitCommandTest.java | 0 .../bitbiome/commands/ShopCommandTest.java | 0 5 files changed, 50 insertions(+), 22 deletions(-) mode change 100644 => 100755 src/test/java/org/bitbiome/commands/HelpCommandTest.java mode change 100644 => 100755 src/test/java/org/bitbiome/commands/QuitCommandTest.java mode change 100644 => 100755 src/test/java/org/bitbiome/commands/ShopCommandTest.java diff --git a/src/main/java/org/bitbiome/commands/ShopCommand.java b/src/main/java/org/bitbiome/commands/ShopCommand.java index 4891942..b718926 100644 --- a/src/main/java/org/bitbiome/commands/ShopCommand.java +++ b/src/main/java/org/bitbiome/commands/ShopCommand.java @@ -9,10 +9,10 @@ import java.util.Scanner; public class ShopCommand implements CommandAPI{ - Shop shop = new Shop(); - public ShopCommand(){ + Shop shop = new Shop(); + public ShopCommand(){ - } + } @Override public void performCommand(Scanner scanner, boolean isRunning, String message) { @@ -41,7 +41,7 @@ public class ShopCommand implements CommandAPI{ System.out.print("Anzahl eingeben: "); 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)); + boolean bool = 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!"); diff --git a/src/main/java/org/bitbiome/shop/Shop.java b/src/main/java/org/bitbiome/shop/Shop.java index de31cfa..aa15493 100644 --- a/src/main/java/org/bitbiome/shop/Shop.java +++ b/src/main/java/org/bitbiome/shop/Shop.java @@ -5,6 +5,7 @@ import org.json.JSONObject; import java.io.File; import java.io.FileWriter; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; @@ -15,13 +16,18 @@ public class Shop { public ArrayList allItems; public ArrayList currentShopItems; - public Shop(){ - allItems = loadAllItems(); - currentShopItems = loadPartofItems(allItems, 2); + public Shop() { + try { + allItems = loadAllItems(); + currentShopItems = loadPartofItems(allItems, 3); + }catch (Exception e){ + e.printStackTrace(); + } } 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"); @@ -51,7 +57,6 @@ public class Shop { System.out.println("Es gibt zu wenige Items"); return false; } - //Test if the player has enough gold int costs = currentShopItems.get(itemIndex).gold * amount; int gold = (int) playerConfig.get("gold"); @@ -71,7 +76,7 @@ public class Shop { for(int i = 0; i < jsonArray2.length(); i++) { JSONObject tempJSON = jsonArray2.getJSONObject(i); if (tempJSON.getString("name").equals(itemName)) { - intNewAmount = (int) tempJSON.get("amount") - amount; + intNewAmount = tempJSON.getInt("amount") - amount; jsonArray2.remove(i); tempJSON.put("amount", intNewAmount); jsonArray2.put(tempJSON); @@ -80,16 +85,17 @@ public class Shop { fileWriter.write(gameConfig.toString()); fileWriter.close(); currentShopItems = loadCurrentShopItems(); + break; } } //Give Player the Item JSONArray jsonArray = playerConfig.getJSONArray("inventory"); - String newAmount; + int newAmount; for(int i = 0; i < jsonArray.length(); i++) { JSONObject tempJSON = jsonArray.getJSONObject(i); if (tempJSON.getString("name").equals(itemName)) { - newAmount = String.valueOf((Integer.parseInt(tempJSON.getString("amount"))) + amount); + newAmount = tempJSON.getInt("amount") + amount; jsonArray.remove(i); tempJSON.put("amount", newAmount); jsonArray.put(tempJSON); @@ -103,9 +109,9 @@ public class Shop { //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)){ + if (tempJSON.getString("name").equals(itemName)) { durability = (int) tempJSON.get("durability"); } } @@ -132,12 +138,11 @@ public class Shop { File file = new File("src/main/resources/items.json"); ArrayList arrayList = new ArrayList(); try { - String content = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8"); - JSONObject jsonObject = new JSONObject(content); + String content3 = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8"); + JSONArray itemJSON = new JSONArray(content3); - JSONArray jsonArray = jsonObject.getJSONArray("items"); - for(int i = 0; i < jsonArray.length(); i++){ - JSONObject tempJSON = jsonArray.getJSONObject(i); + 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){ @@ -147,7 +152,7 @@ public class Shop { return arrayList; } - private ArrayList loadCurrentShopItems(){ + public ArrayList loadCurrentShopItems(){ File file = new File("src/main/resources/gameconfig.json"); ArrayList arrayList = new ArrayList(); try { @@ -166,22 +171,45 @@ public class Shop { return arrayList; } - private ArrayList loadPartofItems(ArrayList alleItems, int itemCount){ + 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"); HashSet hashSet = new HashSet<>(); + JSONArray shopitems = new JSONArray(); Random random = new Random(); while (hashSet.size() < itemCount){ int rand = random.nextInt(alleItems.size()); 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); + shopitems.put(jsonObject); } } + //write in gameconfig.json + gameConfig.remove("shopitems"); + gameConfig.put("shopitems", shopitems); + FileWriter fileWriter = new FileWriter("src/main/resources/gameconfig.json"); + fileWriter.write(gameConfig.toString()); + fileWriter.close(); + }catch (Exception e){ + e.printStackTrace(); + } + + return arrayList; } - public void itemRotation(){ - currentShopItems = loadPartofItems(allItems, 2); + public void itemRotation() throws IOException { + currentShopItems = loadPartofItems(allItems, 3); } public void printCurrentShopItems(){ diff --git a/src/test/java/org/bitbiome/commands/HelpCommandTest.java b/src/test/java/org/bitbiome/commands/HelpCommandTest.java old mode 100644 new mode 100755 diff --git a/src/test/java/org/bitbiome/commands/QuitCommandTest.java b/src/test/java/org/bitbiome/commands/QuitCommandTest.java old mode 100644 new mode 100755 diff --git a/src/test/java/org/bitbiome/commands/ShopCommandTest.java b/src/test/java/org/bitbiome/commands/ShopCommandTest.java old mode 100644 new mode 100755