-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
223 lines (202 loc) · 7.56 KB
/
index.js
File metadata and controls
223 lines (202 loc) · 7.56 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
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
class Exhibit {
//This class holds information about Exhibits of animals to be featured at Zoos.
constructor(animal, population) {
this.animal = animal;
this.population = population;
//This constructor takes the name of the animal and the population of that animal as parameters
}
}
class Zoo {
//The Zoo class will hold Exhibits of animals, and each Zoo has its own name.
constructor(zooName) {
this.zooName = zooName;
this.exhibits = [];
//This constructor method sets the name of the Zoo to what was input by the user
//The array for exhibits is declared but not assigned a value yet, as it will be filled later by the user
}
addExhibit(exhibit) {
if (exhibit instanceof Exhibit) {
this.exhibits.push(exhibit);
} else {
throw new Error(
`You can only add an instance of Exhibit. Argument is not a exhibit: ${exhibit}`
);
//If the user tries to add something that isn't an Exhibit to a Zoo, then an error is thrown
}
}
}
class Menu {
constructor() {
this.Zoos = [];
this.selectedZoo = null;
//Menu objects will hold an array that contains all of the Zoo objects (which hold their own exhibits)
//selectedZoo is initialized as null because this program requires the use of a property which will be updated with user input.
//Later, selectedZoo will hold a Zoo object.
}
start() {
let selection = this.showMainMenuOptions();
//First showMainMenuOptions is called, causing a prompt to appear when the user runs this program
//The user's input in that prompt is assigned to the selection variable, which will be fed into a switch statement
//Based on the value of the selection variable, the program will decide what method of the Menu class to execute next.
while (selection != 0) {
switch (selection) {
case "1":
this.createZoo();
break;
case "2":
this.editZoo();
break;
case "3":
this.deleteZoo();
break;
case "4":
this.displayZoos();
break;
default:
selection = 0;
}
selection = this.showMainMenuOptions();
}
alert("Goodbye!");
//If the user inputs 0 at the main menu, the application will exit
}
showMainMenuOptions() {
return prompt(`
0) Exit
1) Create new Zoo
2) Edit Zoo
3) Delete Zoo
4) Display all Zoos
`);
//This method shows the user a prompt with a list of options available for this program
//showMainMenuOptions is called by start(), and whatever is input in this prompt will be returned to start() to select one of the options
}
displayZoos() {
let zooString = "";
for (let i = 0; i < this.Zoos.length; i++) {
zooString += i + ") " + this.Zoos[i].zooName + "\n";
if (this.Zoos[i].exhibits >= 1) {
zooString += "\t" + "Featuring these exhibits: " + "\n";
}
for (let j = 0; j < this.Zoos[i].exhibits.length; j++) {
zooString +=
"\t" +
j +
") " +
this.Zoos[i].exhibits[j].animal +
" with a population of " +
this.Zoos[i].exhibits[j].population +
"\n";
}
}
//These loops will compile a list of all Zoos stored in the menu application as well as the exhibits of those Zoos and then display them to the user in an alert
alert(zooString);
}
createZoo() {
let zooName = prompt("Enter name for new Zoo:");
this.Zoos.push(new Zoo(zooName));
//createZoo prompts the user to input a name for the new Zoo object they will be creating
}
deleteZoo() {
let listZoos = "";
for (let i = 0; i < this.Zoos.length; i++) {
listZoos += i + ") " + this.Zoos[i].zooName + "\n";
}
//This loop gathers a numbered list of the Zoos created by the user
let index = prompt(`
Existing Zoos:
${listZoos}
----
Please enter the index of the Zoo you wish to delete:
`);
if (index > -1 && index < this.Zoos.length) {
this.Zoos.splice(index, 1);
}
//Then after the list of existing Zoos has been compiled, the list is shown to the user within a prompt
//This prompt then asks the user to input the index of the Zoo they wish to delete
}
editZoo() {
//--
let listZoos = "";
for (let i = 0; i < this.Zoos.length; i++) {
listZoos += i + ") " + this.Zoos[i].zooName + "\n";
}
let index = prompt(`
Existing Zoos:
${listZoos}
----
Please enter the index of the Zoo you wish to edit details of:
`);
//This prompt shows the user all existing Zoos and asks them to input the index of the Zoo they want to edit details of
//--
if (index > -1 && index < this.Zoos.length) {
this.selectedZoo = this.Zoos[index];
//Set the selectedZoo property of the current Menu object to the particular Zoo object specified by the user's input
let description =
"Zoo Name: " +
this.selectedZoo.zooName +
"\n" +
"Has these animals: " +
"\n";
for (let i = 0; i < this.selectedZoo.exhibits.length; i++) {
description +=
i +
") " +
this.selectedZoo.exhibits[i].animal +
" with a population of " +
this.selectedZoo.exhibits[i].population +
"\n";
}
//Gather the exhibits featured at the user-specified Zoo and also the population of those exhibits
let selection = this.showZooMenuOptions(description);
//Pass all of the information regarding the Zoo selected by the user into the showZooMenuOptions method
switch (selection) {
case "1":
this.createExhibit();
break;
case "2":
this.deleteExhibit();
break;
}
}
}
showZooMenuOptions(ZooInfo) {
return prompt(`
0) back
1) create exhibit
2) delete exhibit
--------
${ZooInfo}
`);
//showZooMenuOptions displays a series of options the user can pick from followed by the information about the Zoo they selected earlier
}
createExhibit() {
let animal = prompt("Enter animal for new exhibit:");
let population = prompt(" Enter population for new exhibit:");
this.selectedZoo.addExhibit(new Exhibit(animal, population));
//createExhibit asks the user to input an animal name and the population of that animal to be featured as an exhibit for the Zoo they selected earlier
}
deleteExhibit() {
let listExhibits = "";
for (let i = 0; i < this.selectedZoo.exhibits.length; i++) {
listExhibits += i + ") " + this.selectedZoo.exhibits[i].animal + " - "+ this.selectedZoo.exhibits[i].population + "\n";
}
//Gather a list of exhibits that exist in the user's selected Zoo
let index = prompt(`Existing exhibits:
${listExhibits}
----
Please enter the index of the exhibit you wish to delete:"
`);
//Prompt the user with all existing exhibits of that Zoo along with instructions for the user to input an idex
//That index input by the user will indicate which exhibit to delete
//let index = prompt("Enter the index of the exhibit you wish to delete: ");
if (index > -1 && index < this.selectedZoo.exhibits.length) {
this.selectedZoo.exhibits.splice(index, 1);
}
//deleteExhibit asks the user to input the index of the exhibit they want to delete from the Zoo they selected earlier
}
}
let menu = new Menu();
//In order for this program to actually do something, an instance of the Menu object has to be created
menu.start();
//Then, with that new menu instance, start() is invoked, which will lead the user through a series of prompts to use the menu application