diff options
| author | El-BG-1970 <elouangros@hotmail.com> | 2022-07-29 19:51:45 +0200 |
|---|---|---|
| committer | El-BG-1970 <elouangros@hotmail.com> | 2022-07-29 19:51:45 +0200 |
| commit | 4352604d3349098317a29006b1665bc457463e66 (patch) | |
| tree | 96f2aeb4622f4e678006f32cde5e4ccbc2fe7152 /src | |
| parent | bf5a092fb649dd503b0265f9c2a67f2f45978ffb (diff) | |
| download | transacc-4352604d3349098317a29006b1665bc457463e66.tar.gz | |
solved errors that happened during tests
Diffstat (limited to 'src')
| -rw-r--r-- | src/commodities.lisp | 12 | ||||
| -rw-r--r-- | src/inventory.lisp | 24 |
2 files changed, 27 insertions, 9 deletions
diff --git a/src/commodities.lisp b/src/commodities.lisp index 1302a98..6f8c65d 100644 --- a/src/commodities.lisp +++ b/src/commodities.lisp @@ -2,7 +2,8 @@ (:use :cl) (:export :new-commodity :name - :price)) + :price + :commodity-eq)) (in-package :commodities) @@ -17,3 +18,12 @@ (make-instance 'commodity :name name :price price))) + +(defmethod print-object ((object commodity) stream) + (print-unreadable-object (object stream :type t) + (with-slots (name price) object + (format stream "~a worth ~a" name price)))) + +(defmethod commodity-eq ((c1 commodity) (c2 commodity)) + (and (equal (name c1) (name c2)) + (= (price c1) (price c2)))) 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)))))))) |
