-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount.js
80 lines (70 loc) · 1.88 KB
/
account.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
/* eslint-disable no-underscore-dangle */
class Account {
constructor(id, name, balance) {
this.id = id;
this._name = name;
this._balance = balance;
}
get name() {
return this._name;
}
get balance() {
return this._balance;
}
set name(value) {
const comparingValue = value;
if (typeof comparingValue !== 'string') {
throw new Error('Neo: you better go with letters');
} else if (comparingValue.trim() === '' || comparingValue.length < 2) {
throw new Error("Neo: ha ha ha that's so funny!");
}
this._name = value.trim();
}
set balance(value) {
const comparingValue = value;
if (typeof comparingValue !== 'number') {
throw new Error('Are you serious? Neo neeeds numbers');
}
if (comparingValue < 10) {
throw new Error('Stop fooling me!\nNeo knows that you have more');
}
this._balance = value;
}
credit(amount) {
if (amount < 0) {
throw new Error('Not enough money');
}
this.balance += amount;
}
debit(amount) {
if (amount < this._balance) {
this._balance -= amount;
}
throw new Error(
'Our client is very Poor! \nWhat about at the beginning of the next month?'
);
}
static identifyAccounts(accoutFirst, accountSecond) {
// ANCHOR my version
/* for (const key of Object.keys(accoutFirst)) {
if (Object.keys(accountSecond).includes(key)) {
if (accoutFirst[key] === accountSecond[key]) {
return true;
}
}
}
return false; */
return JSON.stringify(accoutFirst) === JSON.stringify(accountSecond);
}
toString() {
if (this._balance <= 100) {
return `${this._name}'s balnace is ${this._balance}.\nPlease help to find some extra funds`;
}
return `${this._name}'s balnace is ${this._balance}.\nP.S You can borrow some money`;
}
transferTo(acc2, amount) {
this.debit(amount);
acc2.credit(amount);
return this.balance;
}
}