-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendingMachine.js
64 lines (56 loc) · 1.39 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
const Person = require('./person').default
const person = new Person()
export default class VendingMachine {
constructor() {
// status can be ["idle", "credited", "vending"]
this.state = {
status: "idle",
credits: 0,
change: 0,
selection: {
A1: [1,1,1,1,1,1,1,1,1,1],
A2: [1,1,1,1,1,1,1,1,1,1],
A3: [1,1,1,1,1,1,1,1,1,1],
B1: [1,1,1,1,1,1,1,1,1,1],
B2: [1,1,1,1,1,1,1,1,1,1],
B3: [1,1,1,1,1,1,1,1,1,1],
C1: [1,1,1,1,1,1,1,1,1,1],
C2: [1,1,1,1,1,1,1,1,1,1],
C3: [1,1,1,1,1,1,1,1,1,1]
},
price: 75,
qty: 0,
msg: ''
};
};
insertCredit(user, credit) {
person.insertMoney(credit)
this.state.credits = credit;
this.state.status = 'credited'
};
checkSelection(choice) {
return Object.keys(this.state.selection).find(choice => choice = 'A1')? true : false
};
checkQuantity(choice) {
this.state.selection[choice]
};
checkPrice() {
if(this.state.credits >= this.state.price) {
this.state.change = this.state.credits - this.state.price;
this.state.msg = 'Change'
} else {
this.state.msg = 'Please add more credits'
};
};
vend(choice) {
this.state.selection[choice].shift()
};
giveChange() {
if (this.state.change) {
person.receiveChange(this.state.change)
}
};
reset() {
this.constructor()
};
}