Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. public class ProductCollection implements ProductCollectionInterface {
  2.  
  3. private Map<Integer, ProductInterface> productMap;
  4.  
  5. public ProductCollection() {
  6. productMap = new HashMap<Integer, ProductInterface>();
  7. }
  8.  
  9. public void addProduct(ProductInterface newProduct) {
  10. productMap.put(new Integer(newProduct.getProductId()), newProduct);
  11. }
  12.  
  13. public ProductInterface getProductById(int productId) {
  14. if (productMap.containsKey(productId)) {
  15. return productMap.get(productId);
  16. } else {
  17. return null;
  18. }
  19. }
  20.  
  21. public boolean isEmpty() {
  22. if (productMap.isEmpty()) {
  23. return true;
  24. } else {
  25. return false;
  26. }
  27. }
  28. // /** Minskar antalet av en vald varutyp med ett.
  29. // * @param identifikationen för den aktuella varutypen.
  30. // @throws om antalet inte kan minskas, d.v.s. antalet redan är noll */
  31.  
  32. public void reduceNumberOfItemsInStockByOne(int productId) throws
  33. DispenserCantReduceItemsException {
  34. ProductInterface product = getProductById(productId);
  35. int numbersInStock = product.getNumbersInStock();
  36. if (numbersInStock > 0) {
  37. product.setNumbersInStock(numbersInStock--);
  38. } else {
  39. throw new DispenserCantReduceItemsException();
  40. }
  41. }
  42.  
  43. public int size() {
  44. return productMap.size();
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement