From 13f6640dc7cecd4a846bacf0bbeb807538f297e9 Mon Sep 17 00:00:00 2001 From: David Hermann Date: Wed, 11 Jan 2023 18:39:19 +0100 Subject: [PATCH 1/2] refactoring: Moved InteractionLoop and JsonParser to the new directory "classes". Removed test code from JsonParser. Removed comment in Main --- src/main/java/org/bitbiome/Boot.java | 1 + src/main/java/org/bitbiome/JsonParser.java | 30 ---------------- src/main/java/org/bitbiome/Main.java | 1 - .../{ => classes}/InteractionLoop.java | 4 ++- .../java/org/bitbiome/classes/JsonParser.java | 36 +++++++++++++++++++ 5 files changed, 40 insertions(+), 32 deletions(-) delete mode 100644 src/main/java/org/bitbiome/JsonParser.java rename src/main/java/org/bitbiome/{ => classes}/InteractionLoop.java (88%) create mode 100644 src/main/java/org/bitbiome/classes/JsonParser.java diff --git a/src/main/java/org/bitbiome/Boot.java b/src/main/java/org/bitbiome/Boot.java index 590ef38..b264413 100644 --- a/src/main/java/org/bitbiome/Boot.java +++ b/src/main/java/org/bitbiome/Boot.java @@ -1,5 +1,6 @@ package org.bitbiome; +import org.bitbiome.classes.InteractionLoop; import org.bitbiome.commands.CommandListener; public class Boot { diff --git a/src/main/java/org/bitbiome/JsonParser.java b/src/main/java/org/bitbiome/JsonParser.java deleted file mode 100644 index 5852e7e..0000000 --- a/src/main/java/org/bitbiome/JsonParser.java +++ /dev/null @@ -1,30 +0,0 @@ -package org.bitbiome; - -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONTokener; - -import java.io.InputStream; - -public class JsonParser { - public static JSONObject getJSONObject(String fileName) { - String resourceName = "./../../" + fileName; - InputStream is = JsonParser.class.getResourceAsStream(resourceName); - if (is == null) { - throw new NullPointerException("Cannot find resource file " + resourceName); - } - - JSONTokener tokener = new JSONTokener(is); - JSONObject object = new JSONObject(tokener); - System.out.println("Name: " + object.getString("name")); - - System.out.println("Inventory: "); - JSONArray inventory = object.getJSONArray("inventory"); - - for (int i = 0; i < inventory.length(); i++) { - JSONObject invObj = inventory.getJSONObject(i); - System.out.println(" - " + invObj.getString("name")); - } - return object; - } -} diff --git a/src/main/java/org/bitbiome/Main.java b/src/main/java/org/bitbiome/Main.java index e614ea5..05a3de9 100644 --- a/src/main/java/org/bitbiome/Main.java +++ b/src/main/java/org/bitbiome/Main.java @@ -5,7 +5,6 @@ public class Main { public static void main(String[] args) { - //JSONObject playerConfig = JsonParser.getJSONObject("playerconfig.json"); new Boot(); } diff --git a/src/main/java/org/bitbiome/InteractionLoop.java b/src/main/java/org/bitbiome/classes/InteractionLoop.java similarity index 88% rename from src/main/java/org/bitbiome/InteractionLoop.java rename to src/main/java/org/bitbiome/classes/InteractionLoop.java index e2a1cd0..6566b0a 100644 --- a/src/main/java/org/bitbiome/InteractionLoop.java +++ b/src/main/java/org/bitbiome/classes/InteractionLoop.java @@ -1,4 +1,6 @@ -package org.bitbiome; +package org.bitbiome.classes; + +import org.bitbiome.Boot; import java.util.Scanner; diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java new file mode 100644 index 0000000..be0e9b8 --- /dev/null +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -0,0 +1,36 @@ +package org.bitbiome.classes; + +import org.json.JSONObject; +import org.json.JSONTokener; + +import java.io.InputStream; + +public class JsonParser { + + /* + Usage of this method: + Get a whole json file with this method. + After that you have the JSON File as an object. + The following methods are available: + object.getString(String keyName); -> For values. + + object.getJSONArray(String keyName); -> For Arrays + jsonArray.getJSONObject(int index); + + More: https://www.baeldung.com/java-org-json + https://stleary.github.io/JSON-java/index.html + https://github.com/stleary/JSON-java + */ + + public static JSONObject getJSONObject(String fileName) { + String resourceName = "./../../" + fileName; + InputStream is = JsonParser.class.getResourceAsStream(resourceName); + if (is == null) { + throw new NullPointerException("Cannot find resource file " + resourceName); + } + + JSONTokener tokener = new JSONTokener(is); + + return new JSONObject(tokener); + } +} From dc77373d072d6d09f0816d9368322a7c749ea91e Mon Sep 17 00:00:00 2001 From: Juliakn66 <117075450+Juliakn66@users.noreply.github.com> Date: Mon, 16 Jan 2023 10:14:20 +0100 Subject: [PATCH 2/2] refactoring: static to non-static method --- src/main/java/org/bitbiome/classes/JsonParser.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/bitbiome/classes/JsonParser.java b/src/main/java/org/bitbiome/classes/JsonParser.java index be0e9b8..e63dd87 100644 --- a/src/main/java/org/bitbiome/classes/JsonParser.java +++ b/src/main/java/org/bitbiome/classes/JsonParser.java @@ -22,7 +22,7 @@ public class JsonParser { https://github.com/stleary/JSON-java */ - public static JSONObject getJSONObject(String fileName) { + public JSONObject getJSONObject(String fileName) { String resourceName = "./../../" + fileName; InputStream is = JsonParser.class.getResourceAsStream(resourceName); if (is == null) {