Made the machine handle unexpected coins.
This commit is contained in:
parent
694230405c
commit
dfa8ccd0ac
|
@ -1,9 +1,15 @@
|
||||||
(ns vending.core
|
(ns vending.core
|
||||||
(:use clojure.set))
|
(:use clojure.set))
|
||||||
|
|
||||||
|
(defn member?
|
||||||
|
"true if seq contains elm"
|
||||||
|
[seq elm]
|
||||||
|
(some #(= elm %) seq))
|
||||||
|
|
||||||
|
|
||||||
(defn make-default-machine [] { :stock {:caramel-wafer 5 :teacake 5 :snowball 5}
|
(defn make-default-machine
|
||||||
|
"Initialise a machine in a known state"
|
||||||
|
[] { :stock {:caramel-wafer 5 :teacake 5 :snowball 5}
|
||||||
:coins {:merk 1 :plack 4 :bawbee 4 :bodle 4}
|
:coins {:merk 1 :plack 4 :bawbee 4 :bodle 4}
|
||||||
:tendered nil
|
:tendered nil
|
||||||
:message ""
|
:message ""
|
||||||
|
@ -39,7 +45,10 @@
|
||||||
|
|
||||||
(defn add-coin [machine coin]
|
(defn add-coin [machine coin]
|
||||||
"Add this coin to this machine."
|
"Add this coin to this machine."
|
||||||
(message-machine (assoc (dissoc machine :tendered) :tendered (cons coin (:tendered machine))) (str "Added a " coin)))
|
(if
|
||||||
|
(member? (keys coin-values) coin)
|
||||||
|
(message-machine (assoc (dissoc machine :tendered) :tendered (cons coin (:tendered machine))) (str "Added a " coin))
|
||||||
|
(message-machine (assoc (dissoc machine :change) :change (cons coin (:change machine))) (str "Sorry, this machine doesn't accept " coin "s"))))
|
||||||
|
|
||||||
(defn add-coins [machine coins]
|
(defn add-coins [machine coins]
|
||||||
"Add these coins to this machine"
|
"Add these coins to this machine"
|
||||||
|
|
|
@ -3,12 +3,6 @@
|
||||||
ring.mock.request
|
ring.mock.request
|
||||||
vending.core))
|
vending.core))
|
||||||
|
|
||||||
(defn member?
|
|
||||||
"true if seq contains elm"
|
|
||||||
[seq elm]
|
|
||||||
(some #(= elm %) seq))
|
|
||||||
|
|
||||||
|
|
||||||
(deftest test-vending
|
(deftest test-vending
|
||||||
(testing
|
(testing
|
||||||
"Test adding coins to machine"
|
"Test adding coins to machine"
|
||||||
|
|
Loading…
Reference in a new issue