Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package onlineShop.core.interfaces;
- import onlineShop.models.products.components.*;
- import onlineShop.models.products.computers.Computer;
- import onlineShop.models.products.computers.DesktopComputer;
- import onlineShop.models.products.computers.Laptop;
- import onlineShop.models.products.peripherals.*;
- import java.util.ArrayList;
- import java.util.List;
- import static onlineShop.common.constants.ExceptionMessages.*;
- import static onlineShop.common.constants.OutputMessages.*;
- public class ControllerImpl implements Controller {
- List<Computer> computers;
- List<Component> components;
- List<Peripheral> peripherals;
- public ControllerImpl() {
- this.computers = new ArrayList<>();
- this.components = new ArrayList<>();
- this.peripherals = new ArrayList<>();
- }
- @Override
- public String addComputer(String computerType, int id, String manufacturer, String model, double price) {
- // for (Computer computer : computers) {
- // if (computer.getId()== id){
- // throw new IllegalArgumentException(INVALID_COMPUTER_TYPE);
- // }
- // }
- if (ComputerExistsById(id, computers)) {
- throw new IllegalArgumentException(EXISTING_COMPUTER_ID);
- }
- if (computerType.equals("Laptop")) {
- computers.add(new Laptop(id, manufacturer, model, price));
- return String.format(ADDED_COMPUTER, id);
- } else if (computerType.equals("DesktopComputer")) {
- computers.add(new DesktopComputer(id, manufacturer, model, price));
- return String.format(ADDED_COMPUTER, id);
- }
- throw new IllegalArgumentException(INVALID_COMPUTER_TYPE);
- }
- @Override
- public String addComponent(int computerId, int id, String componentType, String manufacturer, String model, double price, double overallPerformance, int generation) {
- notExistingComputerByIdThrow(computerId);
- existingComponentByIdThrow(id);
- Component component = generateComponent(id, componentType, manufacturer, model, price, overallPerformance, generation);
- existingComputer(computerId, computers).addComponent(component);
- components.add(component);
- return String.format(ADDED_COMPONENT, componentType, id, computerId);
- }
- @Override
- public String removeComponent(String componentType, int computerId) {
- // Removes a component, with the given type from the computer with that id,
- // then removes component from the collection of components.
- // int componentId = 0;
- //
- // List<Component> componentsByComputerID = existingComputer(computerId, computers).getComponents();
- // for (Component component : componentsByComputerID) {
- // if (component.getClass().getSimpleName().equals(componentType)) {
- // existingComputer(computerId, computers).removeComponent(componentType);
- // componentId = component.getId();
- //
- // components.removeIf(compon -> compon.getClass().getSimpleName().equals(componentType));
- //
- // }
- // }
- // for (Component component : existingComputer(computerId, computers).getComponents()) {
- // if (component.getClass().getSimpleName().equals(componentType)) {
- // existingComputer(computerId, computers).removeComponent(componentType);
- // componentId = component.getId();
- // this.components.remove(component);
- // }
- //
- // }
- // for (Computer computer : computers) {
- // if (computer.getId() == computerId) {
- // for (Component component : computer.getComponents()) {
- // if (component.getClass().getSimpleName().equals(componentType)) {
- // computer.removeComponent(componentType);
- // componentId = component.getId();
- // components.remove(component);
- // break;
- // }
- // }
- // }
- //
- // }
- // "Successfully removed {component type} with id {component id}.".
- return null;//String.format(REMOVED_COMPONENT, componentType, componentId);
- }
- @Override
- public String addPeripheral(int computerId, int id, String peripheralType, String manufacturer,
- String model, double price, double overallPerformance, String connectionType) {
- notExistingComputerByIdThrow(computerId);
- existingPeripheralByIdThrow(id);
- Peripheral peripheral = generatePeripheral(computerId, id, peripheralType, manufacturer, model, price, overallPerformance, connectionType);
- existingComputer(computerId, computers).addPeripheral(peripheral);
- peripherals.add(peripheral);
- return String.format(ADDED_PERIPHERAL, peripheralType, id, computerId);
- }
- @Override
- public String removePeripheral(String peripheralType, int computerId) {
- for (Peripheral peripheral : peripherals) {
- if (peripheral.getClass().getSimpleName().equals(peripheralType)){
- peripherals.remove(peripheral);
- return String.format(REMOVED_PERIPHERAL, peripheralType, peripheral.getId()) ;
- }
- }
- // int peripheralId =0;
- // for (Computer computer : computers) {
- // if (computer.getId() == computerId) {
- // for (Peripheral peripheral : computer.getPeripherals()) {
- // if (peripheral.getClass().getSimpleName().equals(peripheralType)) {
- // computer.removePeripheral(peripheralType);
- // peripheralId = peripheral.getId();
- // peripherals.remove(peripheral);
- // }
- // }
- // }
- // }
- //
- // return null; // String.format(REMOVED_PERIPHERAL, peripheralType, peripheralId) ;
- throw new IllegalArgumentException(String.format(NOT_EXISTING_PERIPHERAL, peripheralType,
- existingComputer(computerId, computers).getClass().getSimpleName(), computerId));
- }
- // Functionality
- // Removes a computer, with the given id, from the collection of computers.
- // If it's successful, it returns toString method on the removed computer.
- @Override
- public String buyComputer(int id) {
- notExistingComputerByIdThrow(id);
- for (Computer computer : computers) {
- if (computer.getId() == id ){
- computers.remove(computer);
- return computer.toString();
- }
- }
- return null;
- }
- // Removes the computer with the highest overall performance and with a price, less or equal to the budget, from the collection of computers.
- // 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}."
- // If it's successful, it returns toString method on the removed computer.
- @Override
- public String BuyBestComputer(double budget) {
- return null;
- }
- @Override
- public String getComputerData(int id) {
- for (Computer computer : computers) {
- if(computer.getId()==id){
- return computer.toString();
- }
- }
- return null;
- }
- private boolean ComputerExistsById(int id, List<Computer> computers) {
- for (Computer computer : computers) {
- if (computer.getId() == id) {
- return true;
- }
- }
- return false;
- }
- private Computer existingComputer(int id, List<Computer> computers) {
- for (Computer computer : computers) {
- if (computer.getId() == id) {
- return computer;
- }
- }
- return null;
- }
- private void notExistingComputerByIdThrow(int computerId) {
- if (!ComputerExistsById(computerId, computers)) {
- throw new IllegalArgumentException(NOT_EXISTING_COMPUTER_ID);
- }
- }
- private void existingComponentByIdThrow(int id) {
- for (Component component : components) {
- if (component.getId() == id) {
- throw new IllegalArgumentException(EXISTING_COMPONENT_ID);
- }
- }
- }
- private Component generateComponent(int id, String componentType, String manufacturer, String model,
- double price, double overallPerformance, int generation) {
- if (componentType.equals("CentralProcessingUnit")) {
- return new CentralProcessingUnit(id, manufacturer, model, price, overallPerformance, generation);
- } else if (componentType.equals("Motherboard")) {
- return new Motherboard(id, manufacturer, model, price, overallPerformance, generation);
- } else if (componentType.equals("PowerSupply")) {
- return new PowerSupply(id, manufacturer, model, price, overallPerformance, generation);
- } else if (componentType.equals("RandomAccessMemory")) {
- return new RandomAccessMemory(id, manufacturer, model, price, overallPerformance, generation);
- } else if (componentType.equals("SolidStateDrive")) {
- return new SolidStateDrive(id, manufacturer, model, price, overallPerformance, generation);
- } else if (componentType.equals("VideoCard")) {
- return new VideoCard(id, manufacturer, model, price, overallPerformance, generation);
- }
- throw new IllegalArgumentException(INVALID_COMPONENT_TYPE);
- }
- private void existingPeripheralByIdThrow(int id) {
- for (Peripheral peripheral : peripherals) {
- if (peripheral.getId() == id) {
- throw new IllegalArgumentException(EXISTING_PERIPHERAL_ID);
- }
- }
- }
- private Peripheral generatePeripheral(int computerId, int id, String peripheralType, String manufacturer, String model, double price, double overallPerformance, String connectionType) {
- if (peripheralType.equals("Headset")) {
- return new Headset(id, manufacturer, model, price, overallPerformance, connectionType);
- // existingComputer(computerId, computers).addPeripheral(h);
- // peripherals.add(h);
- } else if (peripheralType.equals("Keyboard")) {
- return new Keyboard(id, manufacturer, model, price, overallPerformance, connectionType);
- // existingComputer(computerId, computers).addPeripheral(k);
- // peripherals.add(k);
- } else if (peripheralType.equals("Monitor")) {
- return new Monitor(id, manufacturer, model, price, overallPerformance, connectionType);
- // existingComputer(computerId, computers).addPeripheral(m);
- // peripherals.add(m);
- } else if (peripheralType.equals("Mouse")) {
- return new Mouse(id, manufacturer, model, price, overallPerformance, connectionType);
- // existingComputer(computerId, computers).addPeripheral(mouse);
- // peripherals.add(mouse);
- }
- throw new IllegalArgumentException(INVALID_PERIPHERAL_TYPE);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement