Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ProductCollection implements ProductCollectionInterface {
- private Map<Integer, ProductInterface> productMap;
- public ProductCollection() {
- productMap = new HashMap<Integer, ProductInterface>();
- }
- public void addProduct(ProductInterface newProduct) {
- productMap.put(new Integer(newProduct.getProductId()), newProduct);
- }
- public ProductInterface getProductById(int productId) {
- if (productMap.containsKey(productId)) {
- return productMap.get(productId);
- } else {
- return null;
- }
- }
- public boolean isEmpty() {
- if (productMap.isEmpty()) {
- return true;
- } else {
- return false;
- }
- }
- // /** Minskar antalet av en vald varutyp med ett.
- // * @param identifikationen för den aktuella varutypen.
- // @throws om antalet inte kan minskas, d.v.s. antalet redan är noll */
- public void reduceNumberOfItemsInStockByOne(int productId) throws
- DispenserCantReduceItemsException {
- ProductInterface product = getProductById(productId);
- int numbersInStock = product.getNumbersInStock();
- if (numbersInStock > 0) {
- product.setNumbersInStock(numbersInStock--);
- } else {
- throw new DispenserCantReduceItemsException();
- }
- }
- public int size() {
- return productMap.size();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement