-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
294 lines (293 loc) · 9.19 KB
/
main.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
var Inventory = {};
console.clear();
main();
function main(){
console.log('\n---------------------TRUEPILL PHARMACY---------------------');
console.log('\n1. Add Medication\n2. Show Medications\n3. Update Inventory\n4. Show Inventory\n5. Make a Sale\n6. Exit\n')
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Please enter your choice: ',input => {
input = parseInt(input);
readline.close();
switch(input){
case 1:
console.clear();
addMedication();
break;
case 2:
console.clear();
showMedication();
break;
case 3:
console.clear();
updateInventory();
break;
case 4:
console.clear();
displayInventory();
break;
case 5:
console.clear();
makeSale();
break;
case 6:
console.clear();
console.log('THANK YOU FOR USING TRUEPILL PHARMACY!');
break;
default:
console.clear();
console.log('Invalid Input. Please Try Again.');
main();
break;
}
});
}
function addMedication(){
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter Name of Medicine (Enter \'exit\' for Main Menu): ',medicine => {
if(checkExist(medicine)){
console.log('Medicine Already Exists!');
readline.close();
main();
}
else{
if (stringCheck(medicine) == 0){
readline.close();
addMedication();
}
else if (stringCheck(medicine) == 1){
console.clear();
readline.close();
main();
}
else{
Inventory[medicine] = {
'strength': 0,
'pack_size':0,
'total_packs':0
}
console.clear();
console.log('MEDICATION ADDED SUCCESSFULLY!\n');
readline.close();
main();
}
}
});
}
function stringCheck(str){
if(str === '' || !isNaN(str)){
console.log('The Entry Must Contain String Characters. Please Try Again.');
return 0;
}
else if (str.toLowerCase() == 'exit'){
return 1;
}
}
function checkExist(medicine){
for(item in Inventory){
if(medicine.toLowerCase() == item.toLowerCase()){
return true;
}
}
return false;
}
function showMedication(){
console.clear();
console.log('\nMEDICINE NAME');
console.log('------------------------');
var keys = Object.keys(Inventory);
for(i=0;i<keys.length;i++){
console.log(keys[i]);
}
console.log('------------------------');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('\nPress Enter For Main Menu',inp =>{
console.clear();
readline.close();
main();
});
}
function printMedicineInfo(medicine){
console.log(Inventory[medicine]['strength'].toString() + 'mg\t\t' + Inventory[medicine]['pack_size'] + '\t\t' + Inventory[medicine]['total_packs'] + '\t\t' + medicine);
}
function updateInventory(){
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter Name of Medicine (Enter \'exit\' For Main Menu):',medicine => {
if(stringCheck(medicine) == 0){
readline.close();
updateInventory();
}
else if(stringCheck(medicine) == 1){
console.clear();
readline.close();
main();
}
else{
if(checkExist(medicine)){
console.log('\nSTRENGTH\tPACK SIZE\tTOTAL PACKS\tMEDICINE');
console.log('------------------------------------------------------------------')
printMedicineInfo(medicine);
console.log();
readline.close();
updateMenu(medicine);
}
else{
readline.close();
console.log('The Medicine Does Not Exist.');
updateInventory();
}
}
});
}
function updateMenu(medicine){
console.log('1. Update Strength\n2. Update Pack Size\n3. Add Packs\n4. Main Menu\n')
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter your choice: ', choice =>{
readline.close();
if(choice == 1){
readline.close();
setData(medicine,'strength');
}
else if(choice == 2){
readline.close();
setData(medicine,'pack_size');
}
else if(choice == 3){
readline.close();
setData(medicine,'total_packs');
}
else if(choice == 4){
console.clear();
readline.close();
main();
}
else{
readline.close();
console.log('You did not choose appropriate option. Please try again.');
updateMenu(medicine);
}
});
}
function setData(medicine,property){
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter ' + property + ' for ' + medicine + ': ', value =>{
if(isNaN(value) || value == ''){
readline.close();
console.log('The input should be a number. Please try again.');
setData(medicine,property);
}
else{
readline.close();
if(property == 'total_packs'){
Inventory[medicine]['total_packs'] += Number(value);
}
else{
Inventory[medicine][property] = Number(value);
}
console.clear();
console.log('\nSTRENGTH\tPACK SIZE\tTOTAL PACKS\tMEDICINE');
console.log('------------------------------------------------------------------')
printMedicineInfo(medicine);
console.log();
updateMenu(medicine);
}
});
}
function makeSale(){
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter the name of medicine (\'exit\' for Main Menu): ', input => {
if(stringCheck(input) == 0){
readline.close();
makeSale();
}
else if(stringCheck(input) == 1){
console.clear();
readline.close();
main();
}
else{
if(checkExist(input)){
readline.close();
salePacks(input);
}
else{
readline.close();
console.log('Medicine Does Not Exist. Please Try Again');
makeSale();
}
}
})
}
function salePacks(medicine){
console.log('\nSTRENGTH\tPACK SIZE\tTOTAL PACKS\tMEDICINE');
console.log('------------------------------------------------------------------')
printMedicineInfo(medicine);
console.log();
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Enter the number of unit: ', input => {
if(!Number.isInteger(Number(input))){
readline.close();
console.log('Please enter a valid integer.')
salePacks(medicine);
}
else if(input == 0){
console.clear();
readline.close();
console.log('Operation Cancelled!');
main();
}
else if (input > Inventory[medicine]['total_packs']){
readline.close();
console.log('Sufficient Packs Not Available. Please Try Again or Enter O to cancel the operation');
salePacks(medicine);
}
else{
readline.close();
Inventory[medicine]['total_packs'] -= input;
console.clear();
console.log('SALE SUCCESSFUL!');
main();
}
})
}
function displayInventory(){
console.clear();
console.log('\nSTRENGTH\tPACK SIZE\tTOTAL PACKS\tMEDICINE');
console.log('--------------------------------------------------------------------------');
for(med in Inventory){
printMedicineInfo(med);
}
console.log('--------------------------------------------------------------------------');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('\nPress Enter For Main Menu',inp =>{
readline.close();
console.clear();
main();
});
}