Guest User

Untitled

a guest
Dec 12th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. package bgu.spl.mics.application;
  2. import bgu.spl.mics.application.passiveObjects.*;
  3. import bgu.spl.mics.application.services.TimeService;
  4. import com.google.gson.*;
  5.  
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.Reader;
  9.  
  10. /** This is the Main class of the application. You should parse the input file,
  11. * create the different instances of the objects, and run the system.
  12. * In the end, you should output serialized objects.
  13. */
  14. public class BookStoreRunner {
  15. public static void main(String[] args) {
  16. Gson gson = new Gson();
  17. JsonParser parser = new JsonParser();
  18. InputStream inputStream = BookStoreRunner.class.getClassLoader().getResourceAsStream("input.json");
  19. Reader reader = new InputStreamReader(inputStream);
  20. JsonElement rootElement = parser.parse(reader);
  21. JsonObject rootObject = rootElement.getAsJsonObject();
  22.  
  23. //-------------------------- initialInventory --------------------------
  24.  
  25. JsonArray initialInventoryArray = rootObject.getAsJsonArray("initialInventory");
  26. BookInventoryInfo[] bookInventoryInfo = gson.fromJson(initialInventoryArray,BookInventoryInfo[].class);
  27. Inventory inventory = Inventory.getInstance();
  28. inventory.load(bookInventoryInfo);
  29.  
  30. //-------------------------- initialResources --------------------------
  31.  
  32. JsonArray initialResources = rootObject.getAsJsonArray("initialResources");
  33. JsonObject jsonObject = initialResources.get(0).getAsJsonObject();
  34. JsonArray jsonArray = jsonObject.getAsJsonArray("vehicles");
  35. DeliveryVehicle[] deliveryVehicles = gson.fromJson(jsonArray,DeliveryVehicle[].class);
  36. ResourcesHolder resourcesHolder = ResourcesHolder.getInstance();
  37. resourcesHolder.load(deliveryVehicles);
  38.  
  39. //-------------------------- Services Object --------------------------
  40.  
  41. JsonObject jsonServicesObj = rootObject.getAsJsonObject("services");
  42.  
  43. //--------------------- selling ---------------------
  44.  
  45. JsonPrimitive sellingNum = jsonServicesObj.getAsJsonPrimitive("selling");
  46. int sellingNumber = sellingNum.getAsInt();
  47.  
  48. //--------------------- time ---------------------
  49.  
  50. JsonObject jsonTimeObject = jsonServicesObj.getAsJsonObject("time");
  51. TimeService timeService = gson.fromJson(jsonTimeObject, TimeService.class);
  52.  
  53. //--------------------- logistics ---------------------
  54.  
  55. JsonPrimitive logistics = jsonServicesObj.getAsJsonPrimitive("logistics");
  56. int logisticsNumber = logistics.getAsInt();
  57.  
  58. //--------------------- resource service ---------------------
  59.  
  60. JsonPrimitive resourcesService = jsonServicesObj.getAsJsonPrimitive("resourcesService");
  61. int resourceServiceNum = resourcesService.getAsInt();
  62.  
  63. //--------------------- customers ---------------------
  64.  
  65. JsonArray customers = jsonServicesObj.getAsJsonArray("customers");
  66. Customer [] customersArr = gson.fromJson(customers,Customer[].class);
  67.  
  68. }
  69. }
Add Comment
Please, Sign In to add comment