diff options
| author | El-BG-1970 <elouangros@hotmail.com> | 2022-07-29 17:03:43 +0200 |
|---|---|---|
| committer | El-BG-1970 <elouangros@hotmail.com> | 2022-07-29 17:03:43 +0200 |
| commit | 1077a12a0156f2c24083067f760a1afda91f99e9 (patch) | |
| tree | 89ce4d76c65a7901fe18c10632d2fcb343637bc4 | |
| parent | 080878db58f67845b382f19857f731cfb9794865 (diff) | |
| download | transacc-1077a12a0156f2c24083067f760a1afda91f99e9.tar.gz | |
split add-item in add-item and update-item in inventory
| -rw-r--r-- | src/inventory.lisp | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/src/inventory.lisp b/src/inventory.lisp index 429f666..657585e 100644 --- a/src/inventory.lisp +++ b/src/inventory.lisp @@ -34,22 +34,24 @@ (setq items (remove item items)) (setq filled (- filled (cdr item))))) +(defmethod update-item ((inv inventory) item qty old-item old-qty) + (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))))) + (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)) - (let* ((old-item - (find-item inv (commodities:name item))) - (qt - (if old-item - (progn - (delete-item inv old-item) - (+ quantity (cdr old-item))) - quantity))) - (prog1 (setq filled (+ filled qt)) - (push (cons item qt) - items))))))) + (let ((old-item (find-item inv (commodities:name item)))) + (if old-item + (update-item inv item quantity + (car old-item) (cdr old-item)) + (prog1 (setq filled (+ filled quantity)) + (push (cons item quantity) items)))))))) (defmethod remove-item ((inv inventory) name quantity) ;; remove <quantity> of <item> from inventory |
