diff options
Diffstat (limited to 'src/inventory.lisp')
| -rw-r--r-- | src/inventory.lisp | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/src/inventory.lisp b/src/inventory.lisp index 657585e..e122364 100644 --- a/src/inventory.lisp +++ b/src/inventory.lisp @@ -34,22 +34,30 @@ (setq items (remove item items)) (setq filled (- filled (cdr item))))) -(defmethod update-item ((inv inventory) item qty old-item old-qty) +(defmethod update-item ((inv inventory) item qty old-item) (with-slots (filled items) inv - (delete-item inv (cons old-item old-qty)) - (let ((total-quantity (+ qty old-qty))) - (push (cons item total-quantity) items) - (setq filled (+ filled total-quantity))))) + (delete-item inv old-item) + (let ((total-quantity (+ qty (cdr old-item))) + (item-name (commodities:name item)) + (item-price (commodities:price item)) + (old-qty (cdr old-item)) + (old-item-price (commodities:price (car old-item)))) + (let ((new-item (commodities:new-commodity item-name + (/ (+ (* qty item-price) (* old-qty old-item-price)) + total-quantity)))) + (push (cons + new-item + total-quantity) items) + (setq filled (+ filled total-quantity)))))) (defmethod add-item ((inv inventory) item quantity) ;; add <quantity> of <item> to inventory (when (> quantity 0) (with-slots (size filled items) inv - (when (> size (+ filled quantity)) + (when (>= size (+ filled quantity)) (let ((old-item (find-item inv (commodities:name item)))) (if old-item - (update-item inv item quantity - (car old-item) (cdr old-item)) + (update-item inv item quantity old-item) (prog1 (setq filled (+ filled quantity)) (push (cons item quantity) items)))))))) |
