-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.js
85 lines (76 loc) · 2.03 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
81
82
83
84
85
"use strict";
/**
* A Bank Account class
*
* Provides the basic functionality that every account should have
*/
class Account {
/**
* Constructor for creating a new Account object
*
* @param {number} number the number for this account
*/
constructor(number) {
this._number = number; // the account number
this._balance = 0.0;
}
/**
* Getter for the 'private' number field
*
* @returns {number} the account number
*/
getNumber() {
return this._number;
}
/**
* Accessor for the 'private' balance field
*
* @returns {number} balance for this account
*/
getBalance() {
return this._balance;
}
/**
* Method to add money into the account
*
* @param {number} amount money to be deposited into the account
* @returns {undefined}
* @throws {RangeError} when amount is less than or equal to zero
*/
deposit(amount) {
if (amount <= 0) {
throw new RangeError("Deposit amount has to be greater than zero");
}
this._balance += amount;
}
/**
* Method to take money out of the account
*
* @param {number} amount money to be taken out of the account
* @returns {undefined}
* @throws {RangeError} when amount is less than or equal to zero
* @throws {Error} when the account has insufficient funds (balance)
*/
withdraw(amount) {
if (amount <= 0) {
throw new RangeError("Withdraw amount has to be greater than zero");
}
if (amount > this._balance) {
throw Error("Insufficient funds");
}
this._balance -= amount;
}
/**
*
* @returns {string}
*/
endOfMonth(){
return ' ';
}
/**
* @returns {string} representation of this account
*/
toString() {
return "Account " + this._number + ": balance " + this._balance;
}
}