LoadStoreController.java 2.2 KB

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