Browse Source

refactoring: code revision of shop

Fixed all warnings
Outsourced methods due to code duplication
Moving classes to new folders
Merging of item class
remotes/origin/develop
David Hermann 2 years ago
parent
commit
4d9ec59d3f
  1. 1
      src/main/java/org/bitbiome/classes/InteractionLoop.java
  2. 173
      src/main/java/org/bitbiome/classes/Shop.java
  3. 10
      src/main/java/org/bitbiome/commands/ShopCommand.java
  4. 28
      src/main/java/org/bitbiome/entities/Item.java
  5. 38
      src/main/java/org/bitbiome/shop/Item.java
  6. 3
      src/test/java/org/bitbiome/commands/ShopCommandTest.java
  7. 6
      src/test/java/org/bitbiome/entitiesTest/ItemTest.java

1
src/main/java/org/bitbiome/classes/InteractionLoop.java

@ -1,7 +1,6 @@
package org.bitbiome.classes; package org.bitbiome.classes;
import org.bitbiome.Boot; import org.bitbiome.Boot;
import org.bitbiome.classes.*;
import org.json.JSONObject; import org.json.JSONObject;
import java.util.Scanner; import java.util.Scanner;

173
src/main/java/org/bitbiome/shop/Shop.java → 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.BlackJackCommand;
import org.bitbiome.commands.QuizCommand; import org.bitbiome.commands.QuizCommand;
import org.bitbiome.entities.Item;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
@ -19,7 +17,6 @@ import java.util.Scanner;
public class Shop { public class Shop {
public ArrayList<Item> allItems; public ArrayList<Item> allItems;
public ArrayList<Item> currentShopItems; public ArrayList<Item> currentShopItems;
public JsonParser jsonParser = new JsonParser();
public QuizCommand quizCommand = new QuizCommand(); public QuizCommand quizCommand = new QuizCommand();
public BlackJackCommand blackJackCommand = new BlackJackCommand(); public BlackJackCommand blackJackCommand = new BlackJackCommand();
public Scanner scanner; public Scanner scanner;
@ -36,19 +33,19 @@ public class Shop {
try { try {
allItems = loadAllItems(); allItems = loadAllItems();
currentShopItems = loadPartofItems(allItems, 3); currentShopItems = loadPartofItems(allItems, 3);
}catch (Exception e){
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public boolean buy(String itemName, int amount){
//Create File Objects
public boolean buy(String itemName, int amount) {
// Create File Objects
currentShopItems = loadCurrentShopItems(); currentShopItems = loadCurrentShopItems();
File filePlayerConfig = new File("src/main/resources/playerconfig.json"); File filePlayerConfig = new File("src/main/resources/playerconfig.json");
File fileGameConfig = new File("src/main/resources/gameconfig.json"); File fileGameConfig = new File("src/main/resources/gameconfig.json");
File fileItem = new File("src/main/resources/items.json"); File fileItem = new File("src/main/resources/items.json");
try { try {
//Create JSONObjects
// Create JSONObjects
String content1 = new String(Files.readAllBytes(Paths.get(filePlayerConfig.toURI())), "UTF-8"); String content1 = new String(Files.readAllBytes(Paths.get(filePlayerConfig.toURI())), "UTF-8");
JSONObject playerConfig = new JSONObject(content1); JSONObject playerConfig = new JSONObject(content1);
@ -58,37 +55,40 @@ public class Shop {
String content3 = new String(Files.readAllBytes(Paths.get(fileItem.toURI())), "UTF-8"); String content3 = new String(Files.readAllBytes(Paths.get(fileItem.toURI())), "UTF-8");
JSONArray itemJSON = new JSONArray(content3); JSONArray itemJSON = new JSONArray(content3);
//Test if item still available in the shop
// Test if item still available in the shop
int itemIndex = -1; 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; 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; 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; 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"); 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; return false;
} }
//Player gold subtract
// Player gold subtract
playerConfig.put("gold", subtractGold(gold, costs)); playerConfig.put("gold", subtractGold(gold, costs));
//Gameconfig amount reduese
// Gameconfig amount reduese
JSONArray jsonArray2 = gameConfig.getJSONArray("shopitems"); JSONArray jsonArray2 = gameConfig.getJSONArray("shopitems");
int intNewAmount; int intNewAmount;
for(int i = 0; i < jsonArray2.length(); i++) {
for (int i = 0; i < jsonArray2.length(); i++) {
JSONObject tempJSON = jsonArray2.getJSONObject(i); JSONObject tempJSON = jsonArray2.getJSONObject(i);
if (tempJSON.getString("name").equals(itemName)) { if (tempJSON.getString("name").equals(itemName)) {
intNewAmount = tempJSON.getInt("amount") - amount; intNewAmount = tempJSON.getInt("amount") - amount;
@ -96,16 +96,16 @@ public class Shop {
tempJSON.put("amount", intNewAmount); tempJSON.put("amount", intNewAmount);
jsonArray2.put(tempJSON); jsonArray2.put(tempJSON);
gameConfig.put("shopitems", jsonArray2); gameConfig.put("shopitems", jsonArray2);
jsonParser.writeObject("gameconfig.json", gameConfig);
JsonParser.writeObject("src/main/resources/gameconfig.json", gameConfig);
currentShopItems = loadCurrentShopItems(); currentShopItems = loadCurrentShopItems();
break; break;
} }
} }
//Give Player the Item
// Give Player the Item
JSONArray jsonArray = playerConfig.getJSONArray("inventory"); JSONArray jsonArray = playerConfig.getJSONArray("inventory");
int newAmount; int newAmount;
for(int i = 0; i < jsonArray.length(); i++) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject tempJSON = jsonArray.getJSONObject(i); JSONObject tempJSON = jsonArray.getJSONObject(i);
if (tempJSON.getString("name").equals(itemName)) { if (tempJSON.getString("name").equals(itemName)) {
newAmount = tempJSON.getInt("amount") + amount; newAmount = tempJSON.getInt("amount") + amount;
@ -113,14 +113,14 @@ public class Shop {
tempJSON.put("amount", newAmount); tempJSON.put("amount", newAmount);
jsonArray.put(tempJSON); jsonArray.put(tempJSON);
playerConfig.put("inventory", jsonArray); playerConfig.put("inventory", jsonArray);
jsonParser.writeObject("playerconfig.json", playerConfig);
JsonParser.writeObject("src/main/resources/playerconfig.json", playerConfig);
return true; return true;
} }
} }
//Item do not exist in the playerinventory
// Item do not exist in the playerinventory
int durability = 0; int durability = 0;
for(int i = 0; i < itemJSON.length(); i++) {
for (int i = 0; i < itemJSON.length(); i++) {
JSONObject tempJSON = itemJSON.getJSONObject(i); JSONObject tempJSON = itemJSON.getJSONObject(i);
if (tempJSON.getString("name").equals(itemName)) { if (tempJSON.getString("name").equals(itemName)) {
durability = (int) tempJSON.get("durability"); durability = (int) tempJSON.get("durability");
@ -134,121 +134,140 @@ public class Shop {
jsonArray.put(inventory); jsonArray.put(inventory);
playerConfig.put("inventory", jsonArray); 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(); e.printStackTrace();
} }
return true; return true;
} }
private ArrayList loadAllItems(){
private ArrayList<Item> loadAllItems() {
ArrayList<Item> 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"); File file = new File("src/main/resources/items.json");
ArrayList arrayList = new ArrayList<Item>();
JSONArray itemJSON = null;
try { try {
String content3 = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8"); 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(); e.printStackTrace();
} }
return arrayList;
return itemJSON;
} }
public ArrayList loadCurrentShopItems(){
File file = new File("src/main/resources/gameconfig.json");
ArrayList arrayList = new ArrayList<Item>();
try {
String content = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8");
JSONObject jsonObject = new JSONObject(content);
public ArrayList<Item> loadCurrentShopItems() {
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")));
ArrayList<Item> arrayList = new ArrayList<>();
JSONObject jsonObject = JsonParser.getJSONObject("src/main/resources/gameconfig.json");
JSONArray jsonArray = jsonObject.getJSONArray("shopitems");
return addToList(jsonArray, arrayList, "amount");
}
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);
} }
}catch (Exception e){
e.printStackTrace();
} }
return null;
}
public ArrayList<Item> addToList(JSONArray itemJSON, ArrayList<Item> 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");
}
doesDmg = !damage.equals("0") ? false : true;
arrayList.add(new Item(tempJSON.getString("name"), doesDmg, damage, tempJSON.getInt(key),
tempJSON.getInt("gold")));
}
return arrayList; return arrayList;
} }
private ArrayList loadPartofItems(ArrayList<Item> alleItems, int itemCount) {
ArrayList arrayList = new ArrayList<Item>();
try{
private ArrayList<Item> loadPartofItems(ArrayList<Item> alleItems, int itemCount) {
ArrayList<Item> arrayList = new ArrayList<>();
try {
File fileGameConfig = new File("src/main/resources/gameconfig.json"); File fileGameConfig = new File("src/main/resources/gameconfig.json");
String content2 = new String(Files.readAllBytes(Paths.get(fileGameConfig.toURI())), "UTF-8"); String content2 = new String(Files.readAllBytes(Paths.get(fileGameConfig.toURI())), "UTF-8");
JSONObject gameConfig = new JSONObject(content2); JSONObject gameConfig = new JSONObject(content2);
JSONArray jsonArray = gameConfig.getJSONArray("shopitems");
//JSONArray jsonArray = gameConfig.getJSONArray("shopitems");
HashSet<Integer> hashSet = new HashSet<>(); HashSet<Integer> hashSet = new HashSet<>();
JSONArray shopitems = new JSONArray(); JSONArray shopitems = new JSONArray();
Random random = new Random(); Random random = new Random();
while (hashSet.size() < itemCount){
while (hashSet.size() < itemCount) {
int rand = random.nextInt(alleItems.size()); int rand = random.nextInt(alleItems.size());
if(!hashSet.contains(rand)){
if (!hashSet.contains(rand)) {
hashSet.add(rand); hashSet.add(rand);
arrayList.add(alleItems.get(rand)); arrayList.add(alleItems.get(rand));
JSONObject jsonObject = new JSONObject(); 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); shopitems.put(jsonObject);
} }
} }
//write in gameconfig.json
// write in gameconfig.json
gameConfig.remove("shopitems"); gameConfig.remove("shopitems");
gameConfig.put("shopitems", 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(); e.printStackTrace();
} }
return arrayList; return arrayList;
} }
public void itemRotation() { public void itemRotation() {
try { try {
currentShopItems = loadPartofItems(allItems, 3); currentShopItems = loadPartofItems(allItems, 3);
}catch (Exception e){
} catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void printCurrentShopItems(){
public void printCurrentShopItems() {
printArrayList(currentShopItems); printArrayList(currentShopItems);
} }
public void quiz(){
public void quiz() {
quizCommand.performCommand(scanner, isRunning, message, travelEngine); quizCommand.performCommand(scanner, isRunning, message, travelEngine);
} }
public void blackJack(){
public void blackJack() {
System.out.println(""); System.out.println("");
blackJackCommand.performCommand(scanner, isRunning, message, travelEngine); blackJackCommand.performCommand(scanner, isRunning, message, travelEngine);
System.out.println(""); System.out.println("");
} }
private void printArrayList(ArrayList<Item> arrayList){
private void printArrayList(ArrayList<Item> arrayList) {
System.out.println(""); 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(""); System.out.println("");
} }
public int subtractGold(int gold, int cost){
public int subtractGold(int gold, int cost) {
return gold - cost; return gold - cost;
} }
} }

10
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.BlackJack;
import org.bitbiome.classes.Colors; import org.bitbiome.classes.Colors;
import org.bitbiome.classes.Shop;
import org.bitbiome.classes.TravelEngine; 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.ArrayList;
import java.util.Scanner; import java.util.Scanner;
@ -37,7 +37,7 @@ public class ShopCommand implements CommandAPI{
if(input.equals("1")){ if(input.equals("1")){
System.out.println("Folgende Items sind im Shop: "); System.out.println("Folgende Items sind im Shop: ");
for(int i = 0; i < currentItems.size(); i++){ 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("0 Eingeben um den Shop zu verlassen.");
System.out.println(""); System.out.println("");
@ -47,8 +47,8 @@ public class ShopCommand implements CommandAPI{
System.out.print("Anzahl eingeben: "); System.out.print("Anzahl eingeben: ");
String amount = scanner.nextLine(); String amount = scanner.nextLine();
try { 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(); currentItems = shop.loadCurrentShopItems();
if (bool) { if (bool) {
System.out.println(""); System.out.println("");

28
src/main/java/org/bitbiome/entities/Item.java

@ -4,13 +4,17 @@ public class Item {
private String name; private String name;
private boolean doesDamage; 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.name = name;
this.doesDamage = doesDamage; this.doesDamage = doesDamage;
this.damage = damage; this.damage = damage;
this.amount = amount;
this.gold = gold;
} }
public Item() { public Item() {
@ -21,10 +25,26 @@ public class Item {
return name; return name;
} }
public float getDamage() {
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getDamage() {
return damage; return damage;
} }
public int getGold() {
return gold;
}
public void setGold(int gold){
this.gold = gold;
}
public boolean doesDamage() { public boolean doesDamage() {
return doesDamage; return doesDamage;
} }
@ -33,7 +53,7 @@ public class Item {
this.name = name; this.name = name;
} }
public void setDamage(float damage) {
public void setDamage(String damage) {
this.damage = damage; this.damage = damage;
} }

38
src/main/java/org/bitbiome/shop/Item.java

@ -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;
}
}

3
src/test/java/org/bitbiome/commands/ShopCommandTest.java

@ -1,10 +1,11 @@
package org.bitbiome.commands; package org.bitbiome.commands;
import org.bitbiome.shop.Shop;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.bitbiome.classes.Shop;
public class ShopCommandTest { public class ShopCommandTest {
final Shop shop = new Shop(null, true, null, null); final Shop shop = new Shop(null, true, null, null);
@Test @Test

6
src/test/java/org/bitbiome/entitiesTest/ItemTest.java

@ -15,7 +15,7 @@ public class ItemTest {
public static void setItem() { public static void setItem() {
item = new Item(); item = new Item();
item.setName("Unit"); item.setName("Unit");
item.setDamage(12.5F);
item.setDamage("12,5");
item.changeDoesDamage(true); item.changeDoesDamage(true);
} }
@ -26,13 +26,13 @@ public class ItemTest {
@Test @Test
public void testGetDamage() { public void testGetDamage() {
assertEquals(12.5, item.getDamage());
assertEquals("12,5", item.getDamage());
} }
@Test @Test
public void testDoesDamage() { public void testDoesDamage() {
boolean doesDamage = item.doesDamage(); boolean doesDamage = item.doesDamage();
assumeTrue(item.getDamage() > 0);
assumeTrue(item.getDamage().equals("12,5"));
assumeTrue(doesDamage); assumeTrue(doesDamage);
} }
} }
Loading…
Cancel
Save