-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (127 loc) · 5.01 KB
/
index.js
File metadata and controls
146 lines (127 loc) · 5.01 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
142
143
144
145
146
const readline = require('readline');
const Controller = require('./controller/Controller');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const prompt = (query) => new Promise((resolve) => rl.question(query, resolve));
async function createVendingMachineHandler(controller) {
const id = Number(await prompt(' enter id: '));
controller.createNewVendingMachine(id);
console.log(' Vending machine created successfully.');
}
async function createProductHandler(controller) {
const name = await prompt(' enter name: ');
const price = Number(await prompt(' enter price: '));
controller.createNewProduct(name, price);
console.log(' Product created successfully.');
}
async function selectVendingMachineHandler(controller) {
const id = Number(await prompt(' enter id: '));
controller.selectVendingMachine(id);
console.log(' Vending machine selected successfully.');
}
async function addNewShelfToVendingMachineHandler(controller) {
const number = Number(await prompt(' enter shelf number: '));
const productName = await prompt(' enter product name: ');
const count = Number(await prompt(' enter product count: '));
controller.addNewShelfToVendingMachine(number, productName, count);
console.log(' New shelf added to selected vending machine successfully.');
}
async function chargeVendingMachineShelfHandler(controller) {
const number = Number(await prompt(' enter shelf number: '));
const count = Number(await prompt(' enter product count: '));
controller.chargeVendingMachineShelf(number, count);
console.log(' Charge selected vending machine shelf successfully.');
}
function getExistingProductsHandler(controller) {
const existingProducts = controller.getExistingProducts();
Object.entries(existingProducts).forEach(([shelfNumber, productShelf]) => {
console.log(' (shelf number:)', shelfNumber, '(product name:)', productShelf.product.getName(),
'(product price:)', productShelf.product.getPrice(), '(product count:)', productShelf.count);
});
}
async function insertCoinHandler(controller) {
const count = Number(await prompt(' enter coin count: '));
controller.insertCoinToVendingMachine(count);
console.log(' insert coin to selected vending machine successfully.');
}
async function buyProductHandler(controller) {
const number = Number(await prompt(' enter shelf number: '));
const count = Number(await prompt(' enter buy count: '));
const buyResult = controller.buyProduct(number, count);
console.log(' buy product successfully. here the result:');
console.log(' buy', buyResult.count, 'number of', buyResult.productName,
'and remain', buyResult.remainedCoin, 'of your coin.');
console.log(' please give your product and remain coin.');
}
function presetHandler(controller) {
controller.createNewVendingMachine(1);
controller.createNewVendingMachine(2);
controller.createNewVendingMachine(3);
controller.createNewProduct('soda', 2);
controller.createNewProduct('coffee', 3);
controller.selectVendingMachine(1);
controller.addNewShelfToVendingMachine(1, 'soda', 5);
controller.addNewShelfToVendingMachine(2, 'coffee', 0);
controller.selectVendingMachine(2);
controller.addNewShelfToVendingMachine(1, 'soda', 0);
controller.addNewShelfToVendingMachine(2, 'coffee', 10);
controller.selectVendingMachine(3);
controller.addNewShelfToVendingMachine(3, 'soda', 6);
controller.addNewShelfToVendingMachine(4, 'coffee', 7);
console.log(' create 3 vending machine and 2 product for quick test :)');
}
async function main() {
console.log('<<<Start vending machine system>>>');
const controller = new Controller();
while(true) {
const command = await prompt('Please enter your command: ');
try {
switch (command) {
case 'create vending machine':
await createVendingMachineHandler(controller);
break;
case 'create product':
await createProductHandler(controller);
break;
case 'select vending machine':
await selectVendingMachineHandler(controller);
break;
case 'add shelf':
await addNewShelfToVendingMachineHandler(controller);
break;
case 'charge shelf':
await chargeVendingMachineShelfHandler(controller);
break;
case 'get products':
getExistingProductsHandler(controller);
break;
case 'insert coin':
await insertCoinHandler(controller);
break;
case 'buy':
await buyProductHandler(controller);
break;
case 'preset':
presetHandler(controller);
break;
case 'close':
rl.close();
return;
default:
console.log('wrong command! please try again.');
}
} catch (error) {
console.log(error.message);
}
}
}
main().catch((error) => {
console.log(error);
setTimeout(() => { process.exit(1); }, 3000);
});
rl.on('close', () => {
console.log('Have a good day :)');
setTimeout(() => { process.exit(1); }, 3000);
});