@Override public boolean save(final Product... products) { for (Product p : products) { if (p == null) { throw new IllegalArgumentException( String.format("Input " + "product cannot be null: %s.", p)); } if (productList.size() == 0) { p.setId(id++); productList.add(p); } else { for (Product d : productList) { if (p.getName().equals(d.getName())) { String errorMessage = String.format( "Product with such" + " name already exists: %s. ID - %d", d.getName(), d.getId()); throw new IllegalArgumentException(errorMessage); } } p.setId(id++); productList.add(p); } LOGGER.info(String.format("*** %s has been saved. ID - %d", p.getName(), p.getId())); } return true; }
@Override public Set<Product> getAllSortedByName() { LOGGER.info(String.format("%n*** Getting all products from the list " + "ordered by name ***")); Set<Product> nameSortedProductList = new TreeSet<Product>(new NameSorterComparator()); nameSortedProductList.addAll(productList); return nameSortedProductList; }
@Override public Product getById(final Integer id) { if (id == null || id < 1) { throw new IllegalArgumentException( "ID cannot be null and should " + "be greater than 0. Your ID: " + id); } for (Product p : productList) { if (p.getId().equals(id)) { LOGGER.info( String.format("*** Getting product with ID %d from " + "the list of products ***", id)); return p; } else { throw new IllegalArgumentException("There is no product with " + "such ID: " + id); } } return null; }
@Override public boolean update(final Product... products) { for (Product p : products) { if (p == null) { throw new IllegalArgumentException("Input product cannot be " + "null. Your product: " + p); } for (Product d : productList) { if (p.getId().equals(d.getId())) { LOGGER.info(String.format("*** Updating %s ***", p.getName())); d.setName(p.getName()); d.setDescription(p.getDescription()); d.setPrice(p.getPrice()); } } } return false; }
@Override public Product getByName(final String name) { if (name == null || name.equals("")) { throw new IllegalArgumentException( "Input name cannot be null or " + "blank. Your name: " + name); } boolean isInList = false; for (Product p : productList) { if (p.getName().equals(name)) { LOGGER.info(String.format("*** Getting %s ***", p.getName())); return p; } } if (!isInList) { throw new IllegalArgumentException("There is no product with " + "such name: " + name); } return null; }
@Override public boolean remove(final Product... products) { for (Product p : products) { if (p == null) { throw new IllegalArgumentException("Input product cannot be " + "null. Your product: " + p); } if (productList.contains(p)) { LOGGER.info( String.format( "*** %s has been removed from the " + "list of products ***", p.getName())); productList.remove(p); } else { throw new IllegalArgumentException( "There is no product with " + "such name: " + p.getName()); } } return true; }
@Override public Set<Product> getAllSortedById() { LOGGER.info(String.format("%n*** Getting all products from the list " + "ordered by ID ***")); return productList; }
@Override public boolean removeAll() { LOGGER.info("*** Removing all products from the list of products ***"); productList.clear(); return true; }