1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- 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;
- 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();
- }
- public void writeCategory(JSONObject jsonObj) throws IOException {
- JSONArray arr = new JSONArray();
- for (Category cat : MODEL.getCategories())
- arr.add(cat.getName());
- jsonObj.put("Category", arr);
- }
- public void writeCategoryObjects(JSONObject jsonObj) {
- int i = 1;
- JSONArray arr = new JSONArray();
- 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);
- arr = new JSONArray();
- }
- }
- public void writeCategoryElements(JSONObject jsonObj) {
- int i = 1;
- JSONArray arr = new JSONArray();
- 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());
- jsonObj.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
- }
- }
- }
|