mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-08-18 08:24:59 +00:00
dependencies: replace json-simple with gson (#2358)
This commit is contained in:
@@ -39,11 +39,9 @@
|
|||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.code.gson</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>gson</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>2.8.9</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -233,6 +233,11 @@
|
|||||||
<artifactId>htmlunit</artifactId>
|
<artifactId>htmlunit</artifactId>
|
||||||
<version>${htmlunit.version}</version>
|
<version>${htmlunit.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.google.code.gson</groupId>
|
||||||
|
<artifactId>gson</artifactId>
|
||||||
|
<version>2.10</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.google.inject</groupId>
|
<groupId>com.google.inject</groupId>
|
||||||
<artifactId>guice</artifactId>
|
<artifactId>guice</artifactId>
|
||||||
|
|||||||
@@ -35,9 +35,8 @@
|
|||||||
<artifactId>typeobjectpattern</artifactId>
|
<artifactId>typeobjectpattern</artifactId>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.googlecode.json-simple</groupId>
|
<groupId>com.google.code.gson</groupId>
|
||||||
<artifactId>json-simple</artifactId>
|
<artifactId>gson</artifactId>
|
||||||
<version>1.1.1</version>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
|||||||
@@ -24,9 +24,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.typeobject;
|
package com.iluwatar.typeobject;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
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
|
* <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
|
* @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 givenTime = 50; //50ms
|
||||||
var toWin = 500; //points
|
var toWin = 500; //points
|
||||||
var pointsWon = 0;
|
var pointsWon = 0;
|
||||||
|
|||||||
@@ -24,13 +24,12 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.typeobject;
|
package com.iluwatar.typeobject;
|
||||||
|
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
import com.iluwatar.typeobject.Candy.Type;
|
import com.iluwatar.typeobject.Candy.Type;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.security.SecureRandom;
|
import java.security.SecureRandom;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
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.
|
* 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++;
|
pointer++;
|
||||||
}
|
}
|
||||||
|
|
||||||
Candy[] assignRandomCandytypes() throws IOException, ParseException {
|
Candy[] assignRandomCandytypes() throws JsonParseException {
|
||||||
var jp = new JsonParser();
|
var jp = new JsonParser();
|
||||||
jp.parse();
|
jp.parse();
|
||||||
var randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'
|
var randomCode = new Candy[jp.candies.size() - 2]; //exclude generic types 'fruit' and 'candy'
|
||||||
|
|||||||
@@ -24,17 +24,13 @@
|
|||||||
*/
|
*/
|
||||||
package com.iluwatar.typeobject;
|
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 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 java.io.InputStreamReader;
|
||||||
import org.json.simple.JSONObject;
|
import java.util.Hashtable;
|
||||||
import org.json.simple.parser.JSONParser;
|
|
||||||
import org.json.simple.parser.ParseException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The JsonParser class helps parse the json file candy.json to get all the different candies.
|
* 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<>();
|
this.candies = new Hashtable<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse() throws IOException, ParseException {
|
void parse() throws JsonParseException {
|
||||||
var parser = new JSONParser();
|
var is = this.getClass().getClassLoader().getResourceAsStream("candy.json");
|
||||||
var workingDirectory = new File("").getAbsolutePath();
|
var reader = new InputStreamReader(is);
|
||||||
var filePath = List.of("src", "main", "java", "com", "iluwatar", "typeobject", "candy.json");
|
var json = (JsonObject) com.google.gson.JsonParser.parseReader(reader);
|
||||||
var absolutePath = workingDirectory + File.separator + String.join(File.separator, filePath);
|
var array = (JsonArray) json.get("candies");
|
||||||
var jo = (JSONObject) parser.parse(new FileReader(absolutePath));
|
for (var item : array) {
|
||||||
var a = (JSONArray) jo.get("candies");
|
var candy = (JsonObject) item;
|
||||||
for (var o : a) {
|
var name = candy.get("name").getAsString();
|
||||||
var candy = (JSONObject) o;
|
var parentName = candy.get("parent").getAsString();
|
||||||
var name = (String) candy.get("name");
|
var t = candy.get("type").getAsString();
|
||||||
var parentName = (String) candy.get("parent");
|
|
||||||
var t = (String) candy.get("type");
|
|
||||||
var type = Type.CRUSHABLE_CANDY;
|
var type = Type.CRUSHABLE_CANDY;
|
||||||
if (t.equals("rewardFruit")) {
|
if (t.equals("rewardFruit")) {
|
||||||
type = Type.REWARD_FRUIT;
|
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);
|
var c = new Candy(name, parentName, type, points);
|
||||||
this.candies.put(name, c);
|
this.candies.put(name, c);
|
||||||
}
|
}
|
||||||
|
|||||||
+7
-7
@@ -3,43 +3,43 @@
|
|||||||
"name" : "fruit",
|
"name" : "fruit",
|
||||||
"parent" : "null",
|
"parent" : "null",
|
||||||
"type" : "rewardFruit",
|
"type" : "rewardFruit",
|
||||||
"points" : "20"
|
"points" : 20
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name" : "candy",
|
"name" : "candy",
|
||||||
"parent" : "null",
|
"parent" : "null",
|
||||||
"type" : "crushableCandy",
|
"type" : "crushableCandy",
|
||||||
"points" : "10"
|
"points" : 10
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name" : "cherry",
|
"name" : "cherry",
|
||||||
"parent" : "fruit",
|
"parent" : "fruit",
|
||||||
"type" : "rewardFruit",
|
"type" : "rewardFruit",
|
||||||
"points" : "0"
|
"points" : 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name" : "mango",
|
"name" : "mango",
|
||||||
"parent" : "fruit",
|
"parent" : "fruit",
|
||||||
"type" : "rewardFruit",
|
"type" : "rewardFruit",
|
||||||
"points" : "0"
|
"points" : 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name" : "purple popsicle",
|
"name" : "purple popsicle",
|
||||||
"parent" : "candy",
|
"parent" : "candy",
|
||||||
"type" : "crushableCandy",
|
"type" : "crushableCandy",
|
||||||
"points" : "0"
|
"points" : 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name" : "green jellybean",
|
"name" : "green jellybean",
|
||||||
"parent" : "candy",
|
"parent" : "candy",
|
||||||
"type" : "crushableCandy",
|
"type" : "crushableCandy",
|
||||||
"points" : "0"
|
"points" : 0
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name" : "orange gum",
|
"name" : "orange gum",
|
||||||
"parent" : "candy",
|
"parent" : "candy",
|
||||||
"type" : "crushableCandy",
|
"type" : "crushableCandy",
|
||||||
"points" : "0"
|
"points" : 0
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user