-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaurav_oopsTask.html
More file actions
393 lines (335 loc) · 8.64 KB
/
saurav_oopsTask.html
File metadata and controls
393 lines (335 loc) · 8.64 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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<script>
class ProductModel {
prod_id;
prod_name;
constructor(prod_id, prod_name) {
this.prod_id = prod_id;
this.prod_name = prod_name;
}
}
class LocalStorage {
localStorage_setitem(list) {
localStorage.setItem("Product", JSON.stringify(list));
}
localStorage_getitem() {
if (localStorage.getItem("Product") == null) {
var list = [];
var pId = 1;
} else {
var list = JSON.parse(localStorage.getItem("Product"));
var pId = list[list.length - 1].prod_id + 1;
}
return [list, pId];
}
}
class IdSearch {
idSearch(index, list, key) {
//-------------------------------Id Search--------------------------------------
var flag = -1;
for (var i of list) {
if (index == i[key]) {
flag = i;
break;
}
}
return flag;
}
}
class ViewProduct {
viewProduct(list, start, output) {
//-------------------------------View Product--------------------------------------
output +=
"Product Id: " +
list[start].prod_id +
" - " +
"Product Name: " +
list[start].prod_name +
"\n";
if (start < list.length - 1) {
start++;
output = this.viewProduct(list, start, output);
}
return output;
}
}
class DeleteProduct extends ViewProduct {
deleteProduct(list) {
//------------------------Delete Product---------------------------------
alert(this.viewProduct(list, 0, ""));
var deleteId = parseInt(
prompt("Enter the Product id you want to Update: ")
);
var searchId = new IdSearch();
var flag = searchId.idSearch(deleteId, list, "prod_id");
if (flag == -1) {
alert("Invalid Product Id !!!");
} else {
list.splice(flag.prod_id - 1, 1);
var local = new LocalStorage();
local.localStorage_setitem(list);
alert("Product data deleted successfully !!!");
alert(this.viewProduct(list, 0, ""));
}
return list;
}
}
class DeleteData extends DeleteProduct {
deleteData(list) {
// ------------------------------Delete Function-----------------------------------
var deleteChoice = parseInt(
prompt("Which data you want to Delete: \n1. Product \n2. Exit")
);
switch (deleteChoice) {
case 1:
list = this.deleteProduct(list);
break;
case 2:
alert("Thank you!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.deleteData(list);
}
}
class UpdateProduct extends ViewProduct {
updateProduct(list) {
//-------------------------------Update Product--------------------------------------
var updateChoice = parseInt(
prompt("What do you want to Update? : \n1. Product Name")
);
switch (updateChoice) {
case 1:
//------------------------Update Product Name---------------------------------
alert(this.viewProduct(list, 0, ""));
var updateId = parseInt(
prompt("Enter the Product id you want to Update: ")
);
var searchId = new IdSearch();
var flag = searchId.idSearch(updateId, list, "prod_id");
if (flag == -1) {
alert("Invalid Product Id !!!");
} else {
flag.prod_name = prompt("Enter new Product Name: ");
var local = new LocalStorage();
local.localStorage_setitem(list);
alert("Product Name updated successfully !!!");
alert(this.viewProduct(list, 0, ""));
}
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
return list;
}
}
class UpdateData extends UpdateProduct {
updateData(list) {
// ------------------------------Update Function-----------------------------------
var updateChoice = parseInt(
prompt("Which data you want to Update: \n1. Product \n2. Exit")
);
switch (updateChoice) {
case 1:
list = this.updateProduct(list);
break;
case 2:
alert("Thank you!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.updateData(list);
}
}
class SearchProduct extends IdSearch {
searchProduct(list) {
//-------------------------------Search Product--------------------------------------
var searchChoice = parseInt(
prompt(
"Enter your Search choice: \n1. Search by Product Id \n2. Search by Product Name"
)
);
switch (searchChoice) {
case 1:
//-------------------------Search by Product Id---------------------------------
var searchId = parseInt(
prompt("Enter the Product id you want to search: ")
);
var flag = this.idSearch(searchId, list, "prod_id");
if (flag == -1) {
alert("Invalid Product Id !!!");
} else {
alert(
"Product Id: " +
flag.prod_id +
" - " +
"Product Name: " +
flag.prod_name +
"\n"
);
}
break;
case 2:
//------------------------Search by Product Name---------------------------------
var searchName = prompt(
"Enter the Product Name you want to search: "
);
var flag = -1;
for (var i of list) {
if (searchName == i.prod_name) {
flag = i;
break;
}
}
if (flag == -1) {
alert("Invalid Product Name !!!");
} else {
alert(
"Product Id: " +
flag.prod_id +
" - " +
"Product Name: " +
flag.prod_name +
"\n"
);
}
break;
default:
alert("Enter appropriate Search choice !!!");
break;
}
}
}
class SearchData extends SearchProduct {
searchData(list) {
// ------------------------------Search Function-----------------------------------
var searchChoice = parseInt(
prompt("What you want to Search: \n1. Product \n2. Exit")
);
switch (searchChoice) {
case 1:
this.searchProduct(list);
break;
case 2:
alert("Thank you!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.searchData(list);
}
}
class ViewData extends ViewProduct {
viewData(list) {
// ------------------------------View Function-----------------------------------
var viewChoice = parseInt(
prompt("What you want to View: \n1. Product List \n2. Exit")
);
switch (viewChoice) {
case 1:
alert(this.viewProduct(list, 0, ""));
break;
case 2:
alert("Thank you!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.viewData(list);
}
}
class InsertProduct extends ProductModel {
// constructor(prod_id, prod_name){
// super(prod_id, prod_name);
// }
insertProduct(list, id) {
//-------------------------------Insert Product--------------------------------------
var productName = prompt("Enter Product Name:");
var dummy = new ProductModel(id, productName);
list.push(dummy);
var local = new LocalStorage();
local.localStorage_setitem(list);
alert("Product added successfully !!!");
return list;
}
}
class InsertData extends InsertProduct {
insertData(list, id) {
// ------------------------------Insert Function-----------------------------------
var insertChoice = parseInt(
prompt("What you want to Insert: \n1. Product \n2. Exit")
);
switch (insertChoice) {
case 1:
list = this.insertProduct(list, id);
break;
case 2:
alert("Thank you!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.insertData(list, id);
}
}
class MainClass {
main(list, id) {
// -----------------------------------Main Function----------------------------
var choice = parseInt(
prompt(
"Enter your choice: \n1. Insert \n2. View \n3. Search \n4. Update \n5. Delete \n6. Exit"
)
);
switch (choice) {
case 1:
var dataInsert = new InsertData();
dataInsert.insertData(list, id++);
break;
case 2:
var dataView = new ViewData();
dataView.viewData(list);
break;
case 3:
var dataSearch = new SearchData();
dataSearch.searchData(list);
break;
case 4:
var dataUpdate = new UpdateData();
dataUpdate.updateData(list);
break;
case 5:
var dataDelete = new DeleteData();
dataDelete.deleteData(list);
break;
case 6:
alert("Thank you! for visiting our system...");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.main(list, id);
}
}
// The data is stored in this fashion "[ {_, _},{}";
// [ product]
// var db = [];
// localStorage.clear();
var local = new LocalStorage();
var db = local.localStorage_getitem();
var task = new MainClass();
task.main(db[0], db[1]);
</script>