package com.evanbyrne.vending_machine_kata.inventory;

import java.util.SortedMap;
import java.util.TreeMap;

public class VolatileInventoryService implements IInventoryService {

    private final TreeMap<String, InventoryProduct> inventory;

    public VolatileInventoryService() {
        this.inventory = new TreeMap<String, InventoryProduct>();
    }

    public InventoryProduct getProduct(final String key) {
        return this.inventory.get(key);
    }

    public void setProduct(final String key, final InventoryProduct inventoryProduct) {
        this.inventory.put(key, inventoryProduct);
    }

    public SortedMap<String, InventoryProduct> getAllProducts() {
        return this.inventory;
    }

    public InventoryProduct vendProduct(final String key) {
        final InventoryProduct inventoryProduct = this.getProduct(key);

        if(inventoryProduct != null) {
            final int newStock = inventoryProduct.getStock() - 1;

            if(newStock >= 0) {
                final InventoryProduct newInventoryProduct = new InventoryProduct(inventoryProduct.getName(), newStock, inventoryProduct.getCents());
                this.setProduct(key, newInventoryProduct);
                return newInventoryProduct;
            }

            this.inventory.remove(key);
        }

        return null;
    }

}

@@ -948,45 +948,8 @@
  1;%0A
-%0A            if(newStock %3E= 0) %7B%0A    
     
@@ -1089,16 +1089,48 @@
 ents());
+%0A%0A            if(newStock %3E 0) %7B
 %0A       
@@ -1197,38 +1197,59 @@
     
-    return newInventoryProduct
+%7D else %7B%0A                this.inventory.remove(key)
 ;%0A  
@@ -1277,34 +1277,34 @@
     
-this.inventory.remove(key)
+return newInventoryProduct
 ;%0A  

Fix product inventory off by one error [closes #2]