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. 107
      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;
import org.bitbiome.Boot;
import org.bitbiome.classes.*;
import org.json.JSONObject;
import java.util.Scanner;

107
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.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<Item> allItems;
public ArrayList<Item> currentShopItems;
public JsonParser jsonParser = new JsonParser();
public QuizCommand quizCommand = new QuizCommand();
public BlackJackCommand blackJackCommand = new BlackJackCommand();
public Scanner scanner;
@ -66,18 +63,21 @@ public class Shop {
}
}
if (itemIndex == -1) {
System.out.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Dieses Item gibt es nicht!" + Colors.ANSI_RESET);
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;
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);
System.out
.println(Colors.ANSI_BG_RED + Colors.ANSI_BLACK + "Du hast zu wenig Gold!" + Colors.ANSI_RESET);
return false;
}
@ -96,7 +96,7 @@ 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;
}
@ -113,7 +113,7 @@ 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;
}
}
@ -134,7 +134,7 @@ 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) {
e.printStackTrace();
@ -143,51 +143,70 @@ public class Shop {
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");
ArrayList arrayList = new ArrayList<Item>();
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")));
}
itemJSON = new JSONArray(content3);
} catch (Exception e) {
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() {
ArrayList<Item> arrayList = new ArrayList<>();
JSONObject jsonObject = JsonParser.getJSONObject("src/main/resources/gameconfig.json");
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")));
return addToList(jsonArray, arrayList, "amount");
}
}catch (Exception e){
e.printStackTrace();
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;
}
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;
}
private ArrayList loadPartofItems(ArrayList<Item> alleItems, int itemCount) {
ArrayList arrayList = new ArrayList<Item>();
private ArrayList<Item> loadPartofItems(ArrayList<Item> alleItems, int itemCount) {
ArrayList<Item> 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<Integer> hashSet = new HashSet<>();
JSONArray shopitems = new JSONArray();
Random random = new Random();
@ -197,23 +216,21 @@ public class Shop {
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
gameConfig.remove("shopitems");
gameConfig.put("shopitems", shopitems);
jsonParser.writeObject("gameconfig.json", gameConfig);
JsonParser.writeObject("src/main/resources/gameconfig.json", gameConfig);
} catch (Exception e) {
e.printStackTrace();
}
return arrayList;
}
@ -238,11 +255,13 @@ public class Shop {
blackJackCommand.performCommand(scanner, isRunning, message, travelEngine);
System.out.println("");
}
private void printArrayList(ArrayList<Item> 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());
System.out.println(arrayList.get(i).getName() + " | Anzahl: " + arrayList.get(i).getAmount()
+ " | Kosten: " + arrayList.get(i).getGold());
}
}
System.out.println("");

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.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("");

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

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

6
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);
}
}
Loading…
Cancel
Save