|
@@ -1,10 +1,10 @@
|
|
|
package ui.controller;
|
|
|
+
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.File;
|
|
|
import java.io.FileReader;
|
|
|
import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.ArrayList;
|
|
|
import org.json.simple.JSONArray;
|
|
|
import org.json.simple.JSONObject;
|
|
|
import classes.Category;
|
|
@@ -12,43 +12,60 @@ import classes.CpsObject;
|
|
|
import classes.HolonElement;
|
|
|
import classes.HolonObject;
|
|
|
import ui.model.Model;
|
|
|
+
|
|
|
public class LoadStoreController {
|
|
|
private Model MODEL;
|
|
|
+
|
|
|
public LoadStoreController(Model model) {
|
|
|
this.MODEL = model;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
public void writeJSONFile() throws IOException {
|
|
|
- JSONObject jsonObj = new JSONObject();
|
|
|
- writeCategory(jsonObj);
|
|
|
- writeCategoryObjects(jsonObj);
|
|
|
- writeCategoryElements(jsonObj);
|
|
|
- FileWriter file = new FileWriter("C:/Users/krabs/Desktop/Tesst.json");
|
|
|
- file.write(jsonObj.toJSONString());
|
|
|
- file.flush();
|
|
|
- file.close();
|
|
|
+
|
|
|
+ JSONObject json = new JSONObject();
|
|
|
+
|
|
|
+
|
|
|
+ writeCategory(json);
|
|
|
+ writeCategoryObjects(json);
|
|
|
+ writeCategoryElements(json);
|
|
|
+
|
|
|
+ FileWriter writer = new FileWriter("//Users//zheng//Desktop//Tesst.json");
|
|
|
+ writer.flush();
|
|
|
+ writer.close();
|
|
|
}
|
|
|
- public void writeCategory(JSONObject jsonObj) throws IOException {
|
|
|
+
|
|
|
+ public void writeCategory(JSONObject json) throws IOException {
|
|
|
JSONArray arr = new JSONArray();
|
|
|
+
|
|
|
for (Category cat : MODEL.getCategories())
|
|
|
arr.add(cat.getName());
|
|
|
- jsonObj.put("Category", arr);
|
|
|
+ json.put("Category", arr);
|
|
|
}
|
|
|
- public void writeCategoryObjects(JSONObject jsonObj) {
|
|
|
- int i = 1;
|
|
|
+
|
|
|
+ public void writeCategoryObjects(JSONObject json) throws IOException {
|
|
|
+
|
|
|
JSONArray arr = new JSONArray();
|
|
|
+ int i = 1;
|
|
|
+
|
|
|
for (Category cats : MODEL.getCategories())
|
|
|
for (CpsObject cps : cats.getObjects()) {
|
|
|
arr.add(cps.getStored());
|
|
|
arr.add(cps.getObjName());
|
|
|
arr.add(cps.getName());
|
|
|
arr.add(cps.getImage());
|
|
|
- jsonObj.put("CategoryObject." + i++, arr);
|
|
|
+ json.put("CategoryObject." + i++, arr);
|
|
|
arr = new JSONArray();
|
|
|
}
|
|
|
}
|
|
|
- public void writeCategoryElements(JSONObject jsonObj) {
|
|
|
- int i = 1;
|
|
|
+
|
|
|
+ public void writeCategoryElements(JSONObject json) throws IOException {
|
|
|
+
|
|
|
+
|
|
|
JSONArray arr = new JSONArray();
|
|
|
+ int i = 1;
|
|
|
+
|
|
|
for (Category cats : MODEL.getCategories())
|
|
|
for (CpsObject cps : cats.getObjects())
|
|
|
if (cps instanceof HolonObject)
|
|
@@ -57,10 +74,11 @@ public class LoadStoreController {
|
|
|
arr.add(ele.getEleName());
|
|
|
arr.add(ele.getAmount());
|
|
|
arr.add(ele.getEnergy());
|
|
|
- jsonObj.put("CategoryElement." + i++, arr);
|
|
|
+ json.put("CategoryElement." + i++, arr);
|
|
|
arr = new JSONArray();
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
public void readFromJSON(File jsonFile) throws IOException {
|
|
|
String line;
|
|
|
BufferedReader reader = new BufferedReader(new FileReader("textfile"));
|