-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathexercise6.js
59 lines (52 loc) · 1.37 KB
/
exercise6.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
/* BankAccount class */
function BankAccount(name, balance) {
this.name = name;
this.balance = balance;
this.deposit = deposit;
this.withdraw = withdraw;
this.log = []; // equivalent to saying new Array();
this.printLog = printLog;
}
function deposit(amount) {
var oldBalance = this.balance;
this.balance += amount;
// log transaction
this.log.push({
transaction: "deposit",
amount: amount,
date: new Date(),
oldBalance: oldBalance,
newBalance: this.balance,
success: 1 // we can always deposit
});
}
function withdraw(amount) {
var oldBalance = this.balance;
var success = 0;
if (this.balance >= amount) {
this.balance -= amount;
success = 1;
}
else {
console.log("Error: insufficient funds!");
}
// log transaction
this.log.push({
transaction: "withdraw",
amount: amount,
date: new Date(),
oldBalance: oldBalance,
newBalance: this.balance,
success: success
});
}
function printLog() {
for (var i = 0; i < this.log.length; i++) {
console.log(this.log[i].date
+ "\t" + this.log[i].transaction
+ "\t" + this.log[i].amount
+ "\t" + this.log[i].success
+ "\t" + this.log[i].oldBalance
+ "\t" + this.log[i].newBalance);
}
}