-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathwallet.js
47 lines (40 loc) · 850 Bytes
/
wallet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function Coin(value) {
this.value = value
Object.freeze(this)
}
Coin.prototype.equals = function(other) {
if(!(other instanceof Coin)) {
return false
}
return this.value === other.value
}
function Wallet(coins) {
this.money = coins
}
Wallet.prototype.pay = function (coin) {
for(var i = 0; i < this.money.length; i++) {
if (this.money[i].equals(coin)) {
this.money.splice(i, 1)
return true
}
}
return false
}
function Orc(wallet) {
this.wallet = wallet
this.inventory = []
}
Orc.prototype.buy = function (thing, price) {
var priceToPay = new Coin(price)
if (this.wallet.pay(priceToPay)) {
this.inventory.unshift(thing)
return true
}
return false
}
var ten = new Coin(10)
var wallet = new Wallet([ten])
var orc = new Orc(wallet)
console.log(orc)
orc.buy("axe", 10)
console.log(orc)