mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 20:58:35 +00:00
dependencies: replace json-simple with gson (#2358)
This commit is contained in:
@@ -24,9 +24,7 @@
|
||||
*/
|
||||
package com.iluwatar.typeobject;
|
||||
|
||||
import java.io.IOException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
/**
|
||||
* <p>Type object pattern is the pattern we use when the OOP concept of creating a base class and
|
||||
@@ -54,7 +52,7 @@ public class App {
|
||||
*
|
||||
* @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) throws IOException, ParseException {
|
||||
public static void main(String[] args) {
|
||||
var givenTime = 50; //50ms
|
||||
var toWin = 500; //points
|
||||
var pointsWon = 0;
|
||||
|
||||
@@ -24,13 +24,12 @@
|
||||
*/
|
||||
package com.iluwatar.typeobject;
|
||||
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
/**
|
||||
* The CellPool class allows the reuse of crushed cells instead of creation of new cells each time.
|
||||
@@ -80,7 +79,7 @@ public class CellPool {
|
||||
pointer++;
|
||||
}
|
||||
|
||||
Candy[] assignRandomCandytypes() throws IOException, ParseException {
|
||||
Candy[] assignRandomCandytypes() throws JsonParseException {
|
||||
var jp = new JsonParser();
|
||||
jp.parse();
|
||||
var randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'
|
||||
|
||||
@@ -24,17 +24,13 @@
|
||||
*/
|
||||
package com.iluwatar.typeobject;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.iluwatar.typeobject.Candy.Type;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Hashtable;
|
||||
|
||||
/**
|
||||
* The JsonParser class helps parse the json file candy.json to get all the different candies.
|
||||
@@ -47,23 +43,21 @@ public class JsonParser {
|
||||
this.candies = new Hashtable<>();
|
||||
}
|
||||
|
||||
void parse() throws IOException, ParseException {
|
||||
var parser = new JSONParser();
|
||||
var workingDirectory = new File("").getAbsolutePath();
|
||||
var filePath = List.of("src", "main", "java", "com", "iluwatar", "typeobject", "candy.json");
|
||||
var absolutePath = workingDirectory + File.separator + String.join(File.separator, filePath);
|
||||
var jo = (JSONObject) parser.parse(new FileReader(absolutePath));
|
||||
var a = (JSONArray) jo.get("candies");
|
||||
for (var o : a) {
|
||||
var candy = (JSONObject) o;
|
||||
var name = (String) candy.get("name");
|
||||
var parentName = (String) candy.get("parent");
|
||||
var t = (String) candy.get("type");
|
||||
void parse() throws JsonParseException {
|
||||
var is = this.getClass().getClassLoader().getResourceAsStream("candy.json");
|
||||
var reader = new InputStreamReader(is);
|
||||
var json = (JsonObject) com.google.gson.JsonParser.parseReader(reader);
|
||||
var array = (JsonArray) json.get("candies");
|
||||
for (var item : array) {
|
||||
var candy = (JsonObject) item;
|
||||
var name = candy.get("name").getAsString();
|
||||
var parentName = candy.get("parent").getAsString();
|
||||
var t = candy.get("type").getAsString();
|
||||
var type = Type.CRUSHABLE_CANDY;
|
||||
if (t.equals("rewardFruit")) {
|
||||
type = Type.REWARD_FRUIT;
|
||||
}
|
||||
var points = Integer.parseInt((String) candy.get("points"));
|
||||
var points = candy.get("points").getAsInt();
|
||||
var c = new Candy(name, parentName, type, points);
|
||||
this.candies.put(name, c);
|
||||
}
|
||||
|
||||
+7
-7
@@ -3,43 +3,43 @@
|
||||
"name" : "fruit",
|
||||
"parent" : "null",
|
||||
"type" : "rewardFruit",
|
||||
"points" : "20"
|
||||
"points" : 20
|
||||
},
|
||||
{
|
||||
"name" : "candy",
|
||||
"parent" : "null",
|
||||
"type" : "crushableCandy",
|
||||
"points" : "10"
|
||||
"points" : 10
|
||||
},
|
||||
{
|
||||
"name" : "cherry",
|
||||
"parent" : "fruit",
|
||||
"type" : "rewardFruit",
|
||||
"points" : "0"
|
||||
"points" : 0
|
||||
},
|
||||
{
|
||||
"name" : "mango",
|
||||
"parent" : "fruit",
|
||||
"type" : "rewardFruit",
|
||||
"points" : "0"
|
||||
"points" : 0
|
||||
},
|
||||
{
|
||||
"name" : "purple popsicle",
|
||||
"parent" : "candy",
|
||||
"type" : "crushableCandy",
|
||||
"points" : "0"
|
||||
"points" : 0
|
||||
},
|
||||
{
|
||||
"name" : "green jellybean",
|
||||
"parent" : "candy",
|
||||
"type" : "crushableCandy",
|
||||
"points" : "0"
|
||||
"points" : 0
|
||||
},
|
||||
{
|
||||
"name" : "orange gum",
|
||||
"parent" : "candy",
|
||||
"type" : "crushableCandy",
|
||||
"points" : "0"
|
||||
"points" : 0
|
||||
}
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user