diff options
| author | El-BG-1970 <elouangros@hotmail.com> | 2022-07-31 17:30:05 +0200 |
|---|---|---|
| committer | El-BG-1970 <elouangros@hotmail.com> | 2022-07-31 17:30:05 +0200 |
| commit | 3dc8dc250a5cd4089f43a4ff2ac9e9593fa93203 (patch) | |
| tree | ea02343e6a9897595790b69d87ded3a0f90e3d39 | |
| parent | 4352604d3349098317a29006b1665bc457463e66 (diff) | |
| download | transacc-3dc8dc250a5cd4089f43a4ff2ac9e9593fa93203.tar.gz | |
buy-item and some player tests
| -rw-r--r-- | src/player.lisp | 19 | ||||
| -rw-r--r-- | tests/test.lisp | 33 |
2 files changed, 50 insertions, 2 deletions
diff --git a/src/player.lisp b/src/player.lisp index bff62aa..d91c32a 100644 --- a/src/player.lisp +++ b/src/player.lisp @@ -1,7 +1,10 @@ (defpackage player (:use :cl) (:export :init-player - :name)) + :name + :cash + :debt + :buy-item)) (in-package :player) @@ -19,7 +22,7 @@ :accessor clout) (stash :initform 0 :accessor stash) - (stock :initform (make-instance 'inventory :size 100) + (stock :initform (inventory:new-inventory 100) :accessor stock))) (defun init-player (name &optional (cash 2000) (debt 5000)) @@ -27,3 +30,15 @@ :name name :cash cash :debt debt)) + +(defmethod buy-item ((p player) item quantity) + (with-slots (stock cash) p + (let ((cost (* quantity + (commodities:price item)))) + (when (>= cash cost) + (progn + (inventory:add-item stock item quantity) + (setq cash (- cash cost)) + t))))) + + diff --git a/tests/test.lisp b/tests/test.lisp index a040852..a98e0db 100644 --- a/tests/test.lisp +++ b/tests/test.lisp @@ -91,3 +91,36 @@ (car (inventory::find-item i "apple"))))))) ;; creating inventory adding + + +;;; Player tests +(def-suite* player-suite + :description "Test object and methods for player" + :in transacc-suite) + +;; testing buy-item +(test creating-player + (let ((p (player:init-player "Joze")) + (p2 (player:init-player "Mehmout" 555 8976))) + (is (equal (type-of p) 'player::player)) + (is (equal (player:name p) "Joze")) + (is (= (player:cash p) 2000)) + (is (= (player:debt p) 5000)) + (is (equal (type-of p2) 'player::player)) + (is (equal (player:name p2) "Mehmout")) + (is (= (player:cash p2) 555)) + (is (= (player:debt p2) 8976)))) + +(test buy-item + (let ((p (player:init-player "Joze")) + (c (commodities:new-commodity "apple" 5)) + (c2 (commodities:new-commodity) "apple" 11) + (truck (commodities:new-commodity "truck" 100000))) + (is (player:buy-item p c 10)) + (is (not (player:buy-item p kar 1))) + (is (= 10 + (inventory:filled (player::stock p)))) + (is (player:buy-item p c2 10)) + (is (= 20 + (inventory:filled (player::stock p)))) + )) |
