-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaurav_classObjectTask.html
More file actions
321 lines (281 loc) · 6.86 KB
/
saurav_classObjectTask.html
File metadata and controls
321 lines (281 loc) · 6.86 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
<script>
class UserObject {
id;
name;
password;
fullName;
accountNumber;
accountBalance;
status;
constructor(
id,
name,
password,
fullName,
accountNumber,
accountBalance,
status
) {
this.id = id;
this.name = name;
this.password = password;
this.fullName = fullName;
this.accountNumber = accountNumber;
this.accountBalance = accountBalance;
this.status = status;
}
}
class MainClass {
numReg = /^\d+$/;
id = 101;
adminList = [
{ id: 1, name: "saurav", password: 123456, status: 1 },
{ id: 2, name: "keshav", password: 123456, status: 1 },
];
checklen(inp, len) {
//-------------------------------Check Length--------------------------------------
var inputString = prompt(inp);
if (inputString.length < len) {
inputString = this.checklen(inp, len);
}
return inputString;
}
checkNum(inp) {
//-------------------------------Check Number--------------------------------------
var num = prompt(inp);
if (!num.match(this.numReg)) {
num = this.checkNum(inp);
}
return parseInt(num);
}
login(list) {
var loginFlag = 0;
var loginName = this.checklen("Enter your Name: ", 3);
// ----------------yaha per for loop ko recursion meh kese convert kere...?--------------
for (var l of list) {
if (loginName == l.name) {
loginFlag = 1;
var loginPassword = this.checklen("Enter your Password: ", 6);
if (loginPassword == l.password) {
loginFlag = 2;
if (l.status == 1) {
loginFlag = l;
alert("Login Successfull, Welcome :)");
}
}
}
}
return loginFlag;
}
userOperation(userObject) {
//-------------------------------User Operation--------------------------------------
var userChoice = this.checkNum(
"What's your user role ? \n1. WithDraw \n2. Deposit \n3. Show Balance \n4. Exit"
);
switch (userChoice) {
case 1:
//-------------------------WithDraw------------------------------
var withDrawAmount = this.checkNum("Enter WithDraw Amount: ");
if (userObject.accountBalance < withDrawAmount) {
alert("Insufficient Balance!");
} else {
userObject.accountBalance -= withDrawAmount;
alert("WithDrawed Successfully, \nAmount has been debited!");
}
break;
case 2:
//---------------------------Deposit------------------------------
var depositAmount = this.checkNum("Enter Deposit Amount: ");
userObject.accountBalance += depositAmount;
alert("Deposited Successfully, \nAmount has been credited!");
break;
case 3:
//-------------------------Show Balance------------------------------
alert(
"Account Number= " +
userObject.accountNumber +
"\nAccount Balance= " +
userObject.accountBalance
);
break;
case 4:
alert("Thank You!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.userOperation(userObject);
}
user(list) {
//--------------------------------User---------------------------------------
var flag = this.login(list);
if (flag == 0) {
alert("Invalid User Name !!!");
} else if (flag == 1) {
alert("Wrong Password !!!");
} else if (flag == 2) {
alert("Sorry!!! You're not been approved yet!!");
} else {
this.userOperation(flag);
}
}
viewData(list) {
//-------------------------------View--------------------------------------
var outputString = "";
for (var v of list) {
outputString +=
"User Id: " +
v.id +
" - " +
"User Name: " +
v.name +
" - " +
"Full Name: " +
v.fullName +
"\n.Account Number: " +
v.accountNumber +
" - " +
"Account Balance: " +
v.accountBalance +
" - " +
"Status: " +
v.status +
"\n\n";
}
alert(outputString);
// --------------for console the data------------
for (var k of list) {
console.log(
k.id +
" - " +
k.name +
" - " +
k.password +
" - " +
k.fullName +
" - " +
k.accountNumber +
" - " +
k.accountBalance +
" - " +
k.status
);
}
}
manageUser(list) {
//-----------------------Manage User---------------------
this.viewData(list);
var flag = -1;
var approvalId = this.checkNum(
"Which user you want to approve ? \n(Please enter the User Id): "
);
for (var i of list) {
if (approvalId == i.id && i.status == 0) {
flag = 0;
i.status = 1;
var result = "";
for (var j = 0; j < 16; j++) {
result += Math.floor(Math.random() * 10);
}
i.accountNumber = result;
alert("User Approved. Thank You!");
break;
} else if (approvalId == i.id && i.status == 1) {
flag = 1;
alert("User has been already Approved. Thank You!");
}
}
if (flag == -1) {
alert("Invalid User Id, Please enter a valid User Id!!");
}
}
adminOperation(list) {
var adminChoice = this.checkNum(
"What's your user role ? \n1. Manage User \n2. Exit"
);
switch (adminChoice) {
case 1:
this.manageUser(list);
break;
case 2:
alert("Thank you!");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.adminOperation(list);
}
admin(list) {
//---------------------------Admin---------------------------------------
var adminFlag = this.login(this.adminList);
if (adminFlag == 0) {
alert("Invalid Admin Name !!!");
} else if (adminFlag == 1) {
alert("Invalid Admin Password !!!");
} else {
this.adminOperation(list);
}
}
visitor(list) {
//------------------------------Visitor--------------------------------------
var name = this.checklen(
"Enter the username: \nEnter atleast 3 chars",
3
);
var password = this.checklen(
"Enter the password: \nEnter atleast 6 chars",
6
);
var fullName = this.checklen(
"Enter the full name: \nEnter atleast 5 chars",
5
);
var status = 0;
var accountNumber = "0";
var accountBalance = 0;
var dummy = new UserObject(
this.id++,
name,
password,
fullName,
accountNumber,
accountBalance,
status
);
list.push(dummy);
alert("User data added successfully !!!");
return list;
}
main(list) {
var choice = this.checkNum(
"What's your user role ? \n1. Visitor \n2. Admin \n3. User \n4. Exit"
);
switch (choice) {
case 1:
list = this.visitor(list);
break;
case 2:
this.admin(list);
break;
case 3:
this.user(list);
break;
case 4:
alert("Thank you! for visiting our system...");
return;
break;
default:
alert("Please select appropriate choice. Thank You!");
break;
}
this.main(list);
}
}
var userData = [];
var task = new MainClass();
task.main(userData);
</script>