Advertisement
Guest User

Untitled

a guest
May 7th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.83 KB | None | 0 0
  1. package comp1206.sushi.server;
  2.  
  3. import comp1206.sushi.common.*;
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.File;
  7. import java.io.FileReader;
  8. import java.io.IOException;
  9.  
  10. /**
  11. * Configuration class
  12. * Parses a configuration file
  13. * Sets the relevant models and states
  14. */
  15.  
  16. public class Configuration {
  17.  
  18.  
  19. /**
  20. * parseConfiguration method
  21. * @param fileName the configuration file name
  22. * @param serverInstance the server instance
  23. * Used to parse the file passed to the constructor
  24. */
  25.  
  26. public static void parseConfiguration(String fileName, Server serverInstance) throws IOException {
  27.  
  28. BufferedReader bufferedRead; // use a buffered reader to read through the file
  29. File file; // the configuration file
  30. String line; // each line of the configuration file
  31. String[] splitLine; // hold the different parts of each line
  32.  
  33.  
  34. file = new File(fileName);
  35. bufferedRead = new BufferedReader(new FileReader(file));
  36. line = bufferedRead.readLine();
  37.  
  38. // Loop until end of the file is reached
  39. while(line != null) {
  40.  
  41. if (line.equals("")) {
  42. line = bufferedRead.readLine();
  43. continue; // Skip to the next line if current on is empty
  44. }
  45.  
  46. splitLine = line.split(":"); // Split each line into its subparts
  47.  
  48. /*
  49. * Switch statement that sets up models as specified
  50. */
  51. try {
  52. switch (splitLine[0]) {
  53. case "RESTAURANT":
  54. // Create new restaurant
  55. String restName = splitLine[1];
  56. Postcode restPostcode = new Postcode(splitLine[2]);
  57. serverInstance.restaurant = new Restaurant(restName, restPostcode);
  58. break;
  59. case "POSTCODE":
  60. // Create new postcode
  61. String newPostcode = splitLine[1];
  62. serverInstance.addPostcode(newPostcode);
  63. break;
  64. case "SUPPLIER":
  65. // Create new supplier
  66. String supplierName = splitLine[1];
  67. Postcode supplierPcode = new Postcode(splitLine[2]);
  68. serverInstance.addSupplier(supplierName, supplierPcode);
  69. break;
  70. case "INGREDIENT":
  71. // Create new ingredient
  72.  
  73. String ingrName = splitLine[1];
  74. String ingrUnit = splitLine[2];
  75.  
  76. int threshold = Integer.parseInt(splitLine[4]);
  77. int amount = Integer.parseInt(splitLine[5]);
  78. int weight = Integer.parseInt(splitLine[6]);
  79.  
  80. Supplier supplier = null;
  81.  
  82. // Find the appropriate supplier
  83.  
  84. for (Supplier s : serverInstance.getSuppliers()) {
  85. if (s.getName().equals(splitLine[3])) {
  86. supplier = s;
  87. break;
  88. }
  89. }
  90.  
  91. //
  92. serverInstance.addIngredient(ingrName,
  93. ingrUnit,
  94. supplier,
  95. threshold, amount, weight);
  96.  
  97. break;
  98. case "DISH":
  99. // Create new dish
  100.  
  101. String dishName = splitLine[1];
  102. String description = splitLine[2];
  103. int dishPrice = Integer.parseInt(splitLine[3]);
  104. int dishThreshold = Integer.parseInt(splitLine[4]);
  105. int dishAmount = Integer.parseInt(splitLine[5]);
  106.  
  107. Dish dish = serverInstance.addDish(dishName, description,
  108. dishPrice, dishThreshold, dishAmount);
  109.  
  110. String[] dishIngredients = splitLine[6].split(",");
  111.  
  112. for(int i = 0; i < dishIngredients.length; i++){
  113. String[] temp = dishIngredients[i].split(" * ");
  114.  
  115. int quantity = Integer.parseInt(temp[0]);
  116. String ingredient = temp[2];
  117. Ingredient dishIngr = null;
  118.  
  119. for(Ingredient ingr : serverInstance.getIngredients()) {
  120. if(ingr.getName().equals(ingredient)) {
  121. dishIngr = ingr;
  122. }
  123. }
  124.  
  125. serverInstance.addIngredientToDish(dish, dishIngr, quantity);
  126. }
  127. break;
  128. case "USER":
  129. // Create new user
  130.  
  131. String username = splitLine[1];
  132. String password = splitLine[2];
  133. String location = splitLine[3];
  134.  
  135. Postcode userPostcode = new Postcode(splitLine[4]);
  136.  
  137. serverInstance.users.add(new User(username, password, location, userPostcode));
  138. break;
  139. case "ORDER":
  140. // Create new order
  141. Order newOrder = new Order();
  142. String user = splitLine[1];
  143. User orderUser = null;
  144.  
  145. for(User u : serverInstance.getUsers()) {
  146. if(u.getName().equals(user)) orderUser = u;
  147. }
  148.  
  149. String[] orderContents = splitLine[2].split(",");
  150.  
  151. for(int i = 0; i < orderContents.length; i++){
  152. String[] temp = orderContents[i].split(" \\* ");
  153.  
  154. int quantity = Integer.parseInt(temp[0]);
  155. String dishNam = temp[1];
  156. Dish dish1 = null;
  157.  
  158. for(Dish dsh : serverInstance.getDishes()) {
  159. if(dsh.getName().equals(dishNam)) {
  160. dish1 = dsh;
  161. }
  162. }
  163.  
  164. newOrder.addToOrder(dish1, quantity);
  165. }
  166.  
  167. newOrder.setUser(orderUser);
  168.  
  169. serverInstance.addOrder(newOrder);
  170. break;
  171. case "STAFF":
  172. // Create new staff
  173.  
  174. String staffName = splitLine[1];
  175.  
  176. serverInstance.addStaff(staffName);
  177. break;
  178. case "DRONE":
  179. // Create new drone
  180.  
  181. int droneSpeed = Integer.parseInt(splitLine[1]);
  182.  
  183. serverInstance.addDrone(droneSpeed);
  184. break;
  185. case "STOCK":
  186. // Create new stock
  187. String placeholder = splitLine[1];
  188. int quantity = Integer.parseInt(splitLine[2]);
  189.  
  190. for(Dish d : serverInstance.getDishes()) {
  191. if(d.getName().equals(placeholder)) {
  192. serverInstance.setStock(d, quantity);
  193. }
  194. }
  195.  
  196. for(Ingredient in : serverInstance.getIngredients()) {
  197. if(in.getName().equals(placeholder)) {
  198. serverInstance.setStock(in, quantity);
  199. }
  200. }
  201. break;
  202. default:
  203. // Unrecognized command
  204. // Skip the line
  205. break;
  206. }
  207.  
  208. line = bufferedRead.readLine(); // Read the next line
  209.  
  210. } catch (NumberFormatException | NullPointerException e) {
  211.  
  212. // Skip to the next line in case of a formating error in the config file
  213. // That has caused an exception
  214.  
  215. line = bufferedRead.readLine();
  216. }
  217. }
  218.  
  219.  
  220. }
  221.  
  222. /**
  223. * Clean the current configuration of the server
  224. * @param serverInstance the server instance
  225. * @throws ServerInterface.UnableToDeleteException
  226. */
  227.  
  228. public static void cleanConfiguration(Server serverInstance) throws ServerInterface.UnableToDeleteException {
  229.  
  230. for(Drone d : serverInstance.getDrones()) {
  231. if(!d.getStatus().equals("Idle"))
  232. throw new ServerInterface.UnableToDeleteException("Can't load a new config if there is a working drone");
  233. }
  234.  
  235. for(Staff s : serverInstance.getStaff()) {
  236. if(!s.getStatus().equals("Idle"))
  237. throw new ServerInterface.UnableToDeleteException("Can't load a new config if there is a working staff member");
  238. }
  239.  
  240.  
  241. serverInstance.restaurant = null;
  242. for(Dish d : serverInstance.getDishes()) serverInstance.setStock(d, -1);
  243. for(Ingredient i : serverInstance.getIngredients()) serverInstance.setStock(i, -1);
  244. //serverInstance.clearStockLevels();
  245.  
  246. // Remove all dishes
  247. int dishesSize = serverInstance.getDishes().size();
  248. for(int i = 0; i < dishesSize; i++) {
  249. Dish d = serverInstance.getDishes().get(0);
  250. serverInstance.removeDish(d);
  251. }
  252.  
  253. // Remove all drones
  254. int dronesSize = serverInstance.getDrones().size();
  255. for(int i = 0; i < dronesSize; i++) {
  256. Drone d = serverInstance.getDrones().get(0);
  257. serverInstance.removeDrone(d);
  258. }
  259.  
  260. // Remove all ingredients
  261. int ingredientsSize = serverInstance.getIngredients().size();
  262. for(int i = 0; i < ingredientsSize; i++) {
  263. Ingredient in = serverInstance.getIngredients().get(0);
  264. serverInstance.removeIngredient(in);
  265. }
  266.  
  267. // Remove all orders
  268. int ordersSize = serverInstance.getOrders().size();
  269. for(int i = 0; i < ordersSize; i++) {
  270. Order o = serverInstance.getOrders().get(0);
  271. serverInstance.removeOrder(o);
  272. }
  273.  
  274. // Remove all suppliers
  275. int suppliersSize = serverInstance.getSuppliers().size();
  276. for(int i = 0; i < suppliersSize; i++) {
  277. Supplier s = serverInstance.getSuppliers().get(0);
  278. serverInstance.removeSupplier(s);
  279. }
  280.  
  281. // Remove all postcodes
  282. int postcodesSize = serverInstance.getPostcodes().size();
  283. for(int i = 0; i < postcodesSize; i++) {
  284. Postcode p = serverInstance.getPostcodes().get(0);
  285. serverInstance.removePostcode(p);
  286. }
  287.  
  288. // Remove all staff members
  289. int staffSize = serverInstance.getStaff().size();
  290. for(int i = 0; i < staffSize; i++) {
  291. Staff s = serverInstance.getStaff().get(0);
  292. serverInstance.removeStaff(s);
  293. }
  294. }
  295. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement