-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
181 lines (165 loc) · 4.01 KB
/
index.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
// import readline module for getting input from the console
const rl = readLine.createInterface({
input: process.stdin,
output: process.stdout,
});
const menuQ = () => {
return new Promise((resolve, reject) => {
try{
rl.question('your choice: ', (answer) => {
resolve(answer)
})
}
catch(error){
reject();
}
});
}
const milkQ = () => {
return new Promise((resolve, reject) => {
try{
rl.question('How many cups of milk to add? ', (answer) => {
resolve(answer)
})
}
catch(error){
reject();
}
});
}
const espressoQ = () => {
return new Promise((resolve, reject) => {
try{
rl.question('How many shots of espresso to add? ', (answer) => {
resolve(answer)
})
}
catch(error){
reject();
}
});
}
const peppermintQ = () => {
return new Promise((resolve, reject) => {
try{
rl.question('How many shots of peppermint to add? ', (answer) => {
resolve(answer)
})
}
catch(error){
reject();
}
});
}
// create parent class mocha
class Mocha {
milk: number;
shot: number;
chocolateType: string;
constructor(){
this.milk = 1;
this.shot = 1;
this.chocolateType = 'dark';
}
// list the ingredients of mocha
prepare(){
console.log('Your', this.chocolateType, 'Chocolate Mocha Ingredients:');
console.log(this.chocolateType, ' chocolate');
console.log('Cups of milk: ', this.milk);
console.log('Cups of espresso: ', this.shot, '\n\n');
}
}
// inherits from Mocha
class WhiteChocolateMocha extends Mocha {
chocolateType = 'White';
}
// inherits from Mocha
class DarkChocolateMocha extends Mocha {
chocolateType = 'Dark';
}
// inherits from Mocha
class PeppermintMocha extends Mocha {
// add peppermint property
peppermintSyrup: number;
constructor() {
// include super to pull in parent constructor
super();
this.peppermintSyrup = 1;
}
// Overrides Mocha prepare with additional statements
prepare() {
console.log('Your Peppermint Mocha Ingredients:');
console.log('Dark chocolate');
console.log('Cups of milk: ', this.milk);
console.log('Cups of espresso: ', this.shot);
console.log('Pumps of peppermint: ', this.peppermintSyrup, '\n\n');
}
}
// display menu and return selected menu item
const showMenu = async () => {
console.log(
'Select Mocha from menu: \n',
'1: Create White Chocolate Mocha \n',
'2: Create Dark Chocolate Mocha \n',
'3: Create Peppermint Mocha\n',
'0: Exit\n'
);
const qMenu = await menuQ();
return qMenu;
};
// User questions
const userOptions = async (
mochaObject: Mocha | PeppermintMocha
) => {
const milkPicked = await milkQ();
const milkChoice: number = parseInt(milkPicked);
const espPicked = await espressoQ();
const espChoice: number = parseInt(espPicked);
// If peppermint mocha
if (mochaObject instanceof PeppermintMocha) {
const pepPicked = ((await peppermintQ()) as unknown) as string;
const pepChoice: number = parseInt(pepPicked);
mochaObject.peppermintSyrup = pepChoice;
}
mochaObject.milk = milkChoice;
mochaObject.shot = espChoice;
mochaObject.prepare();
};
const main = () => {
let menuChoice = 0;
const buildMocha = async () => {
do {
const optionPicked = await showMenu();
menuChoice = parseInt(optionPicked);
switch (menuChoice) {
case 0: {
break;
}
case 1: {
const whiteMocha = new WhiteChocolateMocha();
await userOptions(whiteMocha);
break;
}
case 2: {
const darkMocha = new DarkChocolateMocha();
await userOptions(darkMocha);
break;
}
case 3: {
const peppermintMocha = new PeppermintMocha();
await userOptions(peppermintMocha);
break;
}
default: {
console.log('Option invalid, please choose from menu.');
break;
}
}
} while (menuChoice != 0);
// end readline process
rl.close();
};
buildMocha();
};
// launch the app
main();