-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendingMachine.js
128 lines (121 loc) · 2.92 KB
/
vendingMachine.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
let wallet, slot, display
const drinks = [
{ name: 'Cola', price: 50 },
{ name: 'Spritz', price: 50 },
{ name: 'Water', price: 50 }
]
class Wallet {
constructor(id) {
this.coins = []
this.el = document.querySelector(`#${id} img`)
this.el.draggable = true
this.el.ondragstart = this.spendCoin.bind(this)
this.el.ondragend = this.removeCoin.bind(this)
this.showNext()
}
addMoney(coins) {
this.coins.push(...coins)
this.showNext()
}
spendCoin(ev) {
const coin = this.coins[0]
if (coin) {
ev.dataTransfer.setData(
'application/x-coin-payment',
JSON.stringify({ coin })
)
} else {
console.log('Wallet empty')
ev.preventDefault()
}
}
removeCoin(ev) {
if (ev.dataTransfer.dropEffect !== 'none') {
console.log('Spending', this.coins[0])
this.coins.shift()
this.showNext()
}
}
showNext() {
const coin = this.coins[0]
this.el.nextElementSibling.innerText = coin
? 'Next coin: ' + coin
: 'Wallet empty'
}
}
class DisplayPanel {
constructor(id) {
this.value = 0
this.el = document.getElementById(id)
this.update()
}
setValue(val) {
this.value = val
this.update()
}
update() {
this.el.innerText = (this.value / 100).toFixed(2)
}
}
class CoinSlot {
constructor(id) {
this.coins = 0
const el = document.getElementById(id)
el.ondragover = (ev) => ev.preventDefault()
el.ondrop = this.receiveCoin.bind(this)
}
receiveCoin(ev) {
const data = ev.dataTransfer.getData('application/x-coin-payment')
const { coin } = JSON.parse(data)
if (coin) {
this.coins += coin
display.setValue(this.coins)
}
}
pay(amount) {
if (this.coins < amount) return false
this.coins -= amount
display.setValue(this.coins)
return true
}
}
class Hopper {
constructor(drink, count) {
this.drink = drink
this.count = count
}
dispenseDrink(name) {
if (this.count < 1) console.log('Hopper empty:', name)
else if (!slot.pay(this.drink.price)) console.log('Insufficient coins')
else {
this.count--
console.log('Drink dispensed:', this.drink.name)
return true
}
return false
}
}
class DrinkButton {
constructor(bn, hopper) {
this.name = hopper.drink.name
this.hopper = hopper
const image = new Image()
bn.className = 'drink-button'
bn.title = hopper.drink.name
bn.onclick = () => this.hopper.dispenseDrink()
image.src = 'images/soda-575433_640.png'
image.alt = this.name
bn.appendChild(image)
}
}
wallet = new Wallet('wallet')
wallet.addMoney([10, 10, 25, 25, 50, 50, 25, 10])
display = new DisplayPanel('display-panel')
slot = new CoinSlot('coin-slot')
const buttons = document.getElementById('drink-buttons')
for (d of drinks) {
const hopper = new Hopper(d, 5)
const bn = document.createElement('button')
new DrinkButton(bn, hopper)
buttons.appendChild(bn)
}