-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvending.js
More file actions
141 lines (133 loc) · 4.26 KB
/
vending.js
File metadata and controls
141 lines (133 loc) · 4.26 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
class Vending {
constructor() {
this.balance = 0.0;
this.coinLookup = {
"nickle": {
value: .05,
count: 10 //1 - Last Test Scenario
},
"dime": {
value: .1,
count: 10 //0 - Last Test Scenario
},
"quarter": {
value: .25,
count: 10 //10 - Last Test Scenario
}
};
this.coinReverseLookupArray = [
{
coinValue: 25,
coinName: "quarter",
coin: this.coinLookup.quarter
},
{
coinValue: 10,
coinName: "dime",
coin: this.coinLookup.dime
},
{
coinValue: 5,
coinName: "nickle",
coin: this.coinLookup.nickle
}
]
this.productLookUp = {
"1": {
name: "cola",
price: 1.00,
inventory: 5
},
"2": {
name: "chips",
price: .5,
inventory: 5
},
"3": {
name: "candy",
price: .65,
inventory: 5
},
"4": {
name: "chocolate",
price: .20,
inventory: 0
}
};
this.rejectedCoins = [];
this.displayText = '';
}
getDisplay = () => {
let currentDisplay = this.displayText;
if (currentDisplay.length === 0) {
currentDisplay = this.balance === 0.0 ? "INSERT COINS" : ("balance: " + this.balance.toFixed(2));
}
this.displayText = '';
return currentDisplay;
}
insertCoin = (coinName) => {
const identifiedCoin = this.coinLookup[coinName];
if (!identifiedCoin) {
this.rejectedCoins.push(coinName);
} else {
this.balance += identifiedCoin.value;
this.coinLookup[coinName].count += 1;
}
}
getChange = () => {
return this.rejectedCoins;
}
makeChange = (remainingBalance) => {
const initialRejectedCoins = [...this.rejectedCoins];
remainingBalance = remainingBalance * 100
while (remainingBalance > 0) {
for (let i = 0; i < this.coinReverseLookupArray.length; i++) {
const currentCoin = this.coinReverseLookupArray[i];
if (currentCoin.coinValue <= remainingBalance) {
this.rejectedCoins.push(currentCoin.coinName);
//for EXACT CHANGE ONLY scenario - Last TestCase
if (currentCoin.coin.count - 1 < 1) {
this.resetState(initialRejectedCoins);
return false;
}
currentCoin.coin.count -= 1;
remainingBalance -= currentCoin.coinValue;
break;
}
}
}
return true;
}
resetState = (initialRejectedCoins) => {
this.rejectedCoins = initialRejectedCoins;
}
selectProduct = (productNumber) => {
let selectedProduct = this.productLookUp[parseInt(productNumber)];
if (this.balance < selectedProduct.price) {
this.displayText = "PRICE $" + selectedProduct.price.toFixed(2);
}
else {
if (this.balance > selectedProduct.price) {
if (selectedProduct.inventory === 0) {
this.displayText = "SOLD OUT";
return;
}
let remainingBalance = (((this.balance * 100) - (selectedProduct.price * 100)) / 100);
let makeChangeRes = this.makeChange(remainingBalance);
if (!makeChangeRes) {
this.displayText = "EXACT CHANGE ONLY";
return;
}
}
selectedProduct.inventory -= 1;
this.balance = 0;
this.displayText = "THANK YOU";
}
}
returnCoins = () => {
let remainingBalance = this.balance;
this.makeChange(remainingBalance);
this.balance = 0;
}
}
module.exports = Vending;