1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- package ui.controller;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import org.json.simple.JSONArray;
- import org.json.simple.JSONObject;
- import classes.Category;
- 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 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 json) throws IOException {
- JSONArray arr = new JSONArray();
-
- for (Category cat : MODEL.getCategories())
- arr.add(cat.getName());
- json.put("Category", arr);
- }
- 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());
- json.put("CategoryObject." + i++, arr);
- arr = new JSONArray();
- }
- }
- 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)
- for (HolonElement ele : ((HolonObject) cps).getElements()) {
- arr.add(ele.getStored());
- arr.add(ele.getEleName());
- arr.add(ele.getAmount());
- arr.add(ele.getEnergy());
- json.put("CategoryElement." + i++, arr);
- arr = new JSONArray();
- }
- }
- public void readFromJSON(File jsonFile) throws IOException {
- String line;
- BufferedReader reader = new BufferedReader(new FileReader("textfile"));
- while ((line = reader.readLine()) != null) {
- // mach hier irgendwas mit der Gelesenen Zeile
- }
- }
- }
|