Advertisement
IvaAnd

Untitled

Nov 30th, 2020
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.25 KB | None | 0 0
  1. package onlineShop.core.interfaces;
  2.  
  3. import onlineShop.models.products.components.*;
  4. import onlineShop.models.products.computers.Computer;
  5. import onlineShop.models.products.computers.DesktopComputer;
  6. import onlineShop.models.products.computers.Laptop;
  7. import onlineShop.models.products.peripherals.*;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. import static onlineShop.common.constants.ExceptionMessages.*;
  13. import static onlineShop.common.constants.OutputMessages.*;
  14.  
  15.  
  16. public class ControllerImpl implements Controller {
  17. List<Computer> computers;
  18. List<Component> components;
  19. List<Peripheral> peripherals;
  20.  
  21.  
  22. public ControllerImpl() {
  23. this.computers = new ArrayList<>();
  24. this.components = new ArrayList<>();
  25. this.peripherals = new ArrayList<>();
  26.  
  27. }
  28.  
  29.  
  30. @Override
  31. public String addComputer(String computerType, int id, String manufacturer, String model, double price) {
  32.  
  33. // for (Computer computer : computers) {
  34. // if (computer.getId()== id){
  35. // throw new IllegalArgumentException(INVALID_COMPUTER_TYPE);
  36. // }
  37. // }
  38.  
  39. if (ComputerExistsById(id, computers)) {
  40. throw new IllegalArgumentException(EXISTING_COMPUTER_ID);
  41. }
  42.  
  43. if (computerType.equals("Laptop")) {
  44. computers.add(new Laptop(id, manufacturer, model, price));
  45. return String.format(ADDED_COMPUTER, id);
  46.  
  47. } else if (computerType.equals("DesktopComputer")) {
  48. computers.add(new DesktopComputer(id, manufacturer, model, price));
  49. return String.format(ADDED_COMPUTER, id);
  50. }
  51.  
  52. throw new IllegalArgumentException(INVALID_COMPUTER_TYPE);
  53.  
  54. }
  55.  
  56.  
  57. @Override
  58. public String addComponent(int computerId, int id, String componentType, String manufacturer, String model, double price, double overallPerformance, int generation) {
  59.  
  60. notExistingComputerByIdThrow(computerId);
  61. existingComponentByIdThrow(id);
  62.  
  63. Component component = generateComponent(id, componentType, manufacturer, model, price, overallPerformance, generation);
  64. existingComputer(computerId, computers).addComponent(component);
  65. components.add(component);
  66.  
  67. return String.format(ADDED_COMPONENT, componentType, id, computerId);
  68. }
  69.  
  70.  
  71. @Override
  72. public String removeComponent(String componentType, int computerId) {
  73.  
  74. // Removes a component, with the given type from the computer with that id,
  75. // then removes component from the collection of components.
  76.  
  77. // int componentId = 0;
  78. //
  79. // List<Component> componentsByComputerID = existingComputer(computerId, computers).getComponents();
  80. // for (Component component : componentsByComputerID) {
  81. // if (component.getClass().getSimpleName().equals(componentType)) {
  82. // existingComputer(computerId, computers).removeComponent(componentType);
  83. // componentId = component.getId();
  84. //
  85. // components.removeIf(compon -> compon.getClass().getSimpleName().equals(componentType));
  86. //
  87. // }
  88. // }
  89.  
  90. // for (Component component : existingComputer(computerId, computers).getComponents()) {
  91. // if (component.getClass().getSimpleName().equals(componentType)) {
  92. // existingComputer(computerId, computers).removeComponent(componentType);
  93. // componentId = component.getId();
  94. // this.components.remove(component);
  95. // }
  96. //
  97. // }
  98. // for (Computer computer : computers) {
  99. // if (computer.getId() == computerId) {
  100. // for (Component component : computer.getComponents()) {
  101. // if (component.getClass().getSimpleName().equals(componentType)) {
  102. // computer.removeComponent(componentType);
  103. // componentId = component.getId();
  104. // components.remove(component);
  105. // break;
  106. // }
  107. // }
  108. // }
  109. //
  110. // }
  111. // "Successfully removed {component type} with id {component id}.".
  112. return null;//String.format(REMOVED_COMPONENT, componentType, componentId);
  113. }
  114.  
  115.  
  116. @Override
  117. public String addPeripheral(int computerId, int id, String peripheralType, String manufacturer,
  118. String model, double price, double overallPerformance, String connectionType) {
  119.  
  120. notExistingComputerByIdThrow(computerId);
  121. existingPeripheralByIdThrow(id);
  122.  
  123. Peripheral peripheral = generatePeripheral(computerId, id, peripheralType, manufacturer, model, price, overallPerformance, connectionType);
  124. existingComputer(computerId, computers).addPeripheral(peripheral);
  125. peripherals.add(peripheral);
  126.  
  127. return String.format(ADDED_PERIPHERAL, peripheralType, id, computerId);
  128. }
  129.  
  130. @Override
  131. public String removePeripheral(String peripheralType, int computerId) {
  132.  
  133. for (Peripheral peripheral : peripherals) {
  134. if (peripheral.getClass().getSimpleName().equals(peripheralType)){
  135. peripherals.remove(peripheral);
  136. return String.format(REMOVED_PERIPHERAL, peripheralType, peripheral.getId()) ;
  137. }
  138. }
  139. // int peripheralId =0;
  140. // for (Computer computer : computers) {
  141. // if (computer.getId() == computerId) {
  142. // for (Peripheral peripheral : computer.getPeripherals()) {
  143. // if (peripheral.getClass().getSimpleName().equals(peripheralType)) {
  144. // computer.removePeripheral(peripheralType);
  145. // peripheralId = peripheral.getId();
  146. // peripherals.remove(peripheral);
  147. // }
  148. // }
  149. // }
  150. // }
  151. //
  152. // return null; // String.format(REMOVED_PERIPHERAL, peripheralType, peripheralId) ;
  153.  
  154. throw new IllegalArgumentException(String.format(NOT_EXISTING_PERIPHERAL, peripheralType,
  155. existingComputer(computerId, computers).getClass().getSimpleName(), computerId));
  156. }
  157.  
  158. // Functionality
  159. // Removes a computer, with the given id, from the collection of computers.
  160. // If it's successful, it returns toString method on the removed computer.
  161.  
  162. @Override
  163. public String buyComputer(int id) {
  164.  
  165. notExistingComputerByIdThrow(id);
  166.  
  167. for (Computer computer : computers) {
  168. if (computer.getId() == id ){
  169. computers.remove(computer);
  170. return computer.toString();
  171. }
  172. }
  173. return null;
  174. }
  175.  
  176. // Removes the computer with the highest overall performance and with a price, less or equal to the budget, from the collection of computers.
  177. // If there are not any computers in the collection or the budget is insufficient for any computer, throws an IllegalArgumentException with the message "Can't buy a computer with a budget of ${budget}."
  178. // If it's successful, it returns toString method on the removed computer.
  179.  
  180. @Override
  181. public String BuyBestComputer(double budget) {
  182. return null;
  183. }
  184.  
  185. @Override
  186. public String getComputerData(int id) {
  187. for (Computer computer : computers) {
  188. if(computer.getId()==id){
  189. return computer.toString();
  190. }
  191.  
  192. }
  193. return null;
  194. }
  195.  
  196.  
  197. private boolean ComputerExistsById(int id, List<Computer> computers) {
  198. for (Computer computer : computers) {
  199. if (computer.getId() == id) {
  200. return true;
  201. }
  202. }
  203. return false;
  204. }
  205.  
  206. private Computer existingComputer(int id, List<Computer> computers) {
  207. for (Computer computer : computers) {
  208. if (computer.getId() == id) {
  209. return computer;
  210. }
  211. }
  212. return null;
  213. }
  214.  
  215. private void notExistingComputerByIdThrow(int computerId) {
  216. if (!ComputerExistsById(computerId, computers)) {
  217. throw new IllegalArgumentException(NOT_EXISTING_COMPUTER_ID);
  218. }
  219. }
  220.  
  221.  
  222. private void existingComponentByIdThrow(int id) {
  223. for (Component component : components) {
  224. if (component.getId() == id) {
  225. throw new IllegalArgumentException(EXISTING_COMPONENT_ID);
  226. }
  227. }
  228. }
  229.  
  230.  
  231. private Component generateComponent(int id, String componentType, String manufacturer, String model,
  232. double price, double overallPerformance, int generation) {
  233.  
  234. if (componentType.equals("CentralProcessingUnit")) {
  235. return new CentralProcessingUnit(id, manufacturer, model, price, overallPerformance, generation);
  236.  
  237. } else if (componentType.equals("Motherboard")) {
  238. return new Motherboard(id, manufacturer, model, price, overallPerformance, generation);
  239.  
  240. } else if (componentType.equals("PowerSupply")) {
  241. return new PowerSupply(id, manufacturer, model, price, overallPerformance, generation);
  242.  
  243. } else if (componentType.equals("RandomAccessMemory")) {
  244. return new RandomAccessMemory(id, manufacturer, model, price, overallPerformance, generation);
  245.  
  246. } else if (componentType.equals("SolidStateDrive")) {
  247. return new SolidStateDrive(id, manufacturer, model, price, overallPerformance, generation);
  248.  
  249. } else if (componentType.equals("VideoCard")) {
  250. return new VideoCard(id, manufacturer, model, price, overallPerformance, generation);
  251.  
  252. }
  253. throw new IllegalArgumentException(INVALID_COMPONENT_TYPE);
  254.  
  255. }
  256.  
  257. private void existingPeripheralByIdThrow(int id) {
  258. for (Peripheral peripheral : peripherals) {
  259. if (peripheral.getId() == id) {
  260. throw new IllegalArgumentException(EXISTING_PERIPHERAL_ID);
  261. }
  262. }
  263.  
  264. }
  265.  
  266.  
  267. private Peripheral generatePeripheral(int computerId, int id, String peripheralType, String manufacturer, String model, double price, double overallPerformance, String connectionType) {
  268. if (peripheralType.equals("Headset")) {
  269. return new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
  270. // existingComputer(computerId, computers).addPeripheral(h);
  271. // peripherals.add(h);
  272. } else if (peripheralType.equals("Keyboard")) {
  273. return new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
  274. // existingComputer(computerId, computers).addPeripheral(k);
  275. // peripherals.add(k);
  276.  
  277.  
  278. } else if (peripheralType.equals("Monitor")) {
  279. return new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
  280. // existingComputer(computerId, computers).addPeripheral(m);
  281. // peripherals.add(m);
  282.  
  283.  
  284. } else if (peripheralType.equals("Mouse")) {
  285. return new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
  286. // existingComputer(computerId, computers).addPeripheral(mouse);
  287. // peripherals.add(mouse);
  288. }
  289. throw new IllegalArgumentException(INVALID_PERIPHERAL_TYPE);
  290. }
  291.  
  292.  
  293. }
  294.  
  295.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement