-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
396 lines (360 loc) · 11.4 KB
/
script.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
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
394
395
396
// BankAccount class
class BankAccount {
constructor(owner, currency, pin, interestRate) {
this.owner = owner;
this.currency = currency;
this.pin = pin;
this._movements = [];
this._interestRate = interestRate;
this._approvedLoans = [];
}
// Static method to log a welcome message and the current date
static hello() {
BankAccount._logMessage(
`Welcome to our bank! Today's date is ${new Date().toLocaleString()}`
);
}
static _logMessage(message) {
document.getElementById("console-output").textContent += message + "\n";
}
// Method to add a deposit to _movements
deposit(val) {
if (val > 0) {
this._movements.push({ amount: val, date: new Date() });
BankAccount._logMessage(`Deposited: ${val} ${this.currency}`);
} else {
BankAccount._logMessage("Deposit amount must be positive.");
}
return this;
}
// Method to subtract a withdrawal from _movements
withdraw(val) {
if (val > 0 && this._getBalance() >= val) {
this._movements.push({ amount: -val, date: new Date() });
BankAccount._logMessage(`Withdrew: ${val} ${this.currency}`);
} else if (val <= 0) {
BankAccount._logMessage("Withdrawal amount must be positive.");
} else {
BankAccount._logMessage("Insufficient funds.");
}
return this;
}
// Method to request a loan
requestLoan(val) {
if (this._approveLoan(val)) {
this._approvedLoans.push(val);
this._movements.push({ amount: val, date: new Date() });
BankAccount._logMessage(`Loan approved: ${val} ${this.currency}`);
} else {
BankAccount._logMessage("Loan request denied.");
}
return this;
}
// Private method to approve a loan
_approveLoan(val) {
return val <= this._getBalance() * 10;
}
// Method to calculate interest on the balance
calculateInterest() {
const balance = this._getBalance();
if (balance > 0) {
const interest = balance * (this._interestRate / 100);
this._movements.push({ amount: interest, date: new Date() });
BankAccount._logMessage(`Interest added: ${interest} ${this.currency}`);
} else {
BankAccount._logMessage(
"No interest added because the balance is non-positive."
);
}
return this;
}
// Private method to get the current balance
_getBalance() {
return this._movements.reduce((acc, mov) => acc + mov.amount, 0);
}
// Method to provide account summary
getAccountSummary() {
const balance = this._getBalance();
const totalInterest = this._movements
.filter((mov) => mov.amount > 0 && this._interestRate > 0)
.reduce((acc, mov) => acc + (mov.amount * this._interestRate) / 100, 0);
BankAccount._logMessage(`Account Summary for ${this.owner}:`);
BankAccount._logMessage(`Currency: ${this.currency}`);
BankAccount._logMessage(
`Transactions: ${this._movements
.map((mov) => `${mov.amount} on ${mov.date.toLocaleString()}`)
.join(", ")}`
);
BankAccount._logMessage(`Current Balance: ${balance} ${this.currency}`);
BankAccount._logMessage(
`Total Interest Earned: ${totalInterest} ${this.currency}`
);
return this;
}
// Getter for movements
get movements() {
return this._movements;
}
}
// Prototype method to sort transactions by date
BankAccount.prototype.sortMovementsByDate = function () {
this._movements.sort((a, b) => a.date - b.date);
BankAccount._logMessage("Transactions sorted by date.");
return this;
};
// SavingsAccount class definition, extending BankAccount
class SavingsAccount extends BankAccount {
constructor(owner, currency, pin, interestRate, withdrawalLimit) {
super(owner, currency, pin, interestRate);
this.withdrawalLimit = withdrawalLimit;
}
// Override the withdraw method to enforce the withdrawal limit
withdraw(val) {
if (val > this.withdrawalLimit) {
BankAccount._logMessage(
`Withdrawal amount exceeds the limit of ${this.withdrawalLimit} ${this.currency}.`
);
} else {
super.withdraw(val);
}
return this;
}
}
// CheckingAccount class definition, extending BankAccount
class CheckingAccount extends BankAccount {
constructor(
owner,
currency,
pin,
interestRate,
overdraftLimit,
overdraftFee
) {
super(owner, currency, pin, interestRate);
this.overdraftLimit = overdraftLimit;
this.overdraftFee = overdraftFee;
}
// Override the withdraw method to allow overdraft
withdraw(val) {
const balance = this._getBalance();
if (balance - val < -this.overdraftLimit) {
BankAccount._logMessage(
`Withdrawal denied. Exceeds overdraft limit of ${this.overdraftLimit} ${this.currency}.`
);
} else {
super.withdraw(val);
if (this._getBalance() < 0) {
this._movements.push({
amount: -this.overdraftFee,
date: new Date(),
});
BankAccount._logMessage(
`Overdraft fee of ${this.overdraftFee} ${this.currency} applied.`
);
}
}
return this;
}
}
// BusinessAccount class definition, extending BankAccount
class BusinessAccount extends BankAccount {
constructor(owner, currency, pin, interestRate) {
super(owner, currency, pin, interestRate * 1.5);
}
// Method to process large transactions for business expenses
processLargeTransaction(val) {
if (val > 10000) {
BankAccount._logMessage(
`Processing large transaction: ${val} ${this.currency}`
);
this._movements.push({ amount: -val, date: new Date() });
} else {
BankAccount._logMessage(
"Transaction amount is too small to be processed as a large transaction."
);
}
return this;
}
// Method to generate a financial report
generateReport() {
const balance = this._getBalance();
const totalInterest = this._movements
.filter((mov) => mov.amount > 0 && this._interestRate > 0)
.reduce((acc, mov) => acc + (mov.amount * this._interestRate) / 100, 0);
const totalLoans = this._approvedLoans.reduce((acc, loan) => acc + loan, 0);
BankAccount._logMessage(`Financial Report for ${this.owner}:`);
BankAccount._logMessage(`Currency: ${this.currency}`);
BankAccount._logMessage(
`Transactions: ${this._movements
.map((mov) => `${mov.amount} on ${mov.date.toLocaleString()}`)
.join(", ")}`
);
BankAccount._logMessage(`Current Balance: ${balance} ${this.currency}`);
BankAccount._logMessage(
`Total Interest Earned: ${totalInterest} ${this.currency}`
);
BankAccount._logMessage(
`Total Loans Approved: ${totalLoans} ${this.currency}`
);
return this;
}
}
let currentAccount = null;
function createAccount() {
const accountType = document.getElementById("account-type").value;
const owner = document.getElementById("owner").value;
const currency = document.getElementById("currency").value;
const pin = document.getElementById("pin").value;
const interestRate =
parseFloat(document.getElementById("interest-rate").value) || 0;
const withdrawalLimit =
parseFloat(document.getElementById("withdrawal-limit").value) || 1000;
const overdraftLimit =
parseFloat(document.getElementById("overdraft-limit").value) || 500;
const overdraftFee =
parseFloat(document.getElementById("overdraft-fee").value) || 35;
let valid = true;
// Validate inputs
if (!/^[a-zA-Z\s]+$/.test(owner.trim())) {
document.getElementById("owner-error").textContent =
"Owner's name must contain only letters.";
valid = false;
} else {
document.getElementById("owner-error").textContent = "";
}
if (!/^\d{4}$/.test(pin)) {
document.getElementById("pin-error").textContent =
"PIN must be a 4-digit number.";
valid = false;
} else {
document.getElementById("pin-error").textContent = "";
}
if (isNaN(interestRate) || interestRate < 0) {
document.getElementById("interest-rate-error").textContent =
"Interest rate must be a positive number.";
valid = false;
} else {
document.getElementById("interest-rate-error").textContent = "";
}
if (isNaN(withdrawalLimit) || withdrawalLimit < 0) {
document.getElementById("withdrawal-limit-error").textContent =
"Withdrawal limit must be a positive number.";
valid = false;
} else {
document.getElementById("withdrawal-limit-error").textContent = "";
}
if (isNaN(overdraftLimit) || overdraftLimit < 0) {
document.getElementById("overdraft-limit-error").textContent =
"Overdraft limit must be a positive number.";
valid = false;
} else {
document.getElementById("overdraft-limit-error").textContent = "";
}
if (isNaN(overdraftFee) || overdraftFee < 0) {
document.getElementById("overdraft-fee-error").textContent =
"Overdraft fee must be a positive number.";
valid = false;
} else {
document.getElementById("overdraft-fee-error").textContent = "";
}
if (!valid) return;
switch (accountType) {
case "savings":
currentAccount = new SavingsAccount(
owner,
currency,
pin,
interestRate,
withdrawalLimit,
overdraftLimit,
overdraftFee
);
break;
case "checking":
currentAccount = new CheckingAccount(
owner,
currency,
pin,
interestRate,
withdrawalLimit,
overdraftLimit,
overdraftFee
);
break;
case "business":
currentAccount = new BusinessAccount(
owner,
currency,
pin,
interestRate,
withdrawalLimit,
overdraftLimit,
overdraftFee
);
break;
default:
BankAccount._logMessage("Invalid account type.");
return;
}
BankAccount._logMessage(`Account created: ${owner} (${accountType})`);
}
function performAction() {
if (!currentAccount) {
BankAccount._logMessage("No account created.");
return;
}
const action = document.getElementById("action").value;
const amount = parseFloat(document.getElementById("amount").value) || 0;
const pinAction = document.getElementById("pin-action").value;
let valid = true;
if (!/^\d{4}$/.test(pinAction)) {
document.getElementById("pin-action-error").textContent =
"PIN must be a 4-digit number.";
valid = false;
} else {
document.getElementById("pin-action-error").textContent = "";
}
if (currentAccount.pin !== pinAction) {
BankAccount._logMessage("Invalid PIN.");
valid = false;
}
if (!valid) return;
switch (action) {
case "deposit":
currentAccount.deposit(amount);
break;
case "withdraw":
currentAccount.withdraw(amount);
break;
case "request-loan":
currentAccount.requestLoan(amount);
break;
case "calculate-interest":
currentAccount.calculateInterest();
break;
case "process-large-transaction":
if (currentAccount instanceof BusinessAccount) {
currentAccount.processLargeTransaction(amount);
} else {
BankAccount._logMessage(
"Large transactions are only available for business accounts."
);
}
break;
case "generate-report":
if (currentAccount instanceof BusinessAccount) {
currentAccount.generateReport();
} else {
BankAccount._logMessage(
"Reports are only available for business accounts."
);
}
break;
default:
BankAccount._logMessage("Invalid action.");
return;
}
currentAccount.getAccountSummary();
currentAccount.sortMovementsByDate();
}
BankAccount.hello();