LoadStoreController.java 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package ui.controller;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import org.json.simple.JSONArray;
  8. import org.json.simple.JSONObject;
  9. import classes.Category;
  10. import classes.CpsObject;
  11. import classes.HolonElement;
  12. import classes.HolonObject;
  13. import ui.model.Model;
  14. public class LoadStoreController {
  15. private Model MODEL;
  16. public LoadStoreController(Model model) {
  17. this.MODEL = model;
  18. }
  19. public void writeJSONFile() throws IOException {
  20. JSONObject json = new JSONObject();
  21. writeCategory(json);
  22. writeCategoryObjects(json);
  23. writeCategoryElements(json);
  24. FileWriter writer = new FileWriter("//Users//zheng//Desktop//Tesst.json");
  25. writer.flush();
  26. writer.close();
  27. }
  28. public void writeCategory(JSONObject json) throws IOException {
  29. JSONArray arr = new JSONArray();
  30. for (Category cat : MODEL.getCategories())
  31. arr.add(cat.getName());
  32. json.put("Category", arr);
  33. }
  34. public void writeCategoryObjects(JSONObject json) throws IOException {
  35. JSONArray arr = new JSONArray();
  36. int i = 1;
  37. for (Category cats : MODEL.getCategories())
  38. for (CpsObject cps : cats.getObjects()) {
  39. arr.add(cps.getStored());
  40. arr.add(cps.getObjName());
  41. arr.add(cps.getName());
  42. arr.add(cps.getImage());
  43. json.put("CategoryObject." + i++, arr);
  44. arr = new JSONArray();
  45. }
  46. }
  47. public void writeCategoryElements(JSONObject json) throws IOException {
  48. JSONArray arr = new JSONArray();
  49. int i = 1;
  50. for (Category cats : MODEL.getCategories())
  51. for (CpsObject cps : cats.getObjects())
  52. if (cps instanceof HolonObject)
  53. for (HolonElement ele : ((HolonObject) cps).getElements()) {
  54. arr.add(ele.getStored());
  55. arr.add(ele.getEleName());
  56. arr.add(ele.getAmount());
  57. arr.add(ele.getEnergy());
  58. json.put("CategoryElement." + i++, arr);
  59. arr = new JSONArray();
  60. }
  61. }
  62. public void readFromJSON(File jsonFile) throws IOException {
  63. String line;
  64. BufferedReader reader = new BufferedReader(new FileReader("textfile"));
  65. while ((line = reader.readLine()) != null) {
  66. // mach hier irgendwas mit der Gelesenen Zeile
  67. }
  68. }
  69. }