-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (55 loc) · 1.74 KB
/
index.js
File metadata and controls
75 lines (55 loc) · 1.74 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
class BankAccount {
constructor(accountNumber, owner) {
this.accountNumber = accountNumber;
this.owner = owner;
this.transactions = [];
}
deposit(amt){
if (amt < 0) {
return;
} else {
let newDeposit = new Transaction(amt);
this.transactions.push(newDeposit);
}
}
charge(amt, payee) {
if (this.balance() < amt * -1) {
return "Insufficent funds, purchase invalidated";
} else {
let charge = new Transaction(amt, payee);
this.transactions.push(charge);
}
}
balance(){
let balance = 0;
this.transactions.forEach((transaction) => {
balance = balance + transaction.amount;
})
return balance;
}
}
class Transaction {
constructor(amount, payee){
this.date = new Date();
this.amount = amount;
this.payee = payee;
}
}
let account = new BankAccount("1234", "John Smith");
console.log(account)
// balance on new accounts
account.balance() // 0
account.deposit(100) //
console.log("My balance after first deposit", account.balance()) // 100
account.deposit(-100) // 100
console.log("Cannot do a negative deposit", account.balance());
// can charge to a vendor
account.charge(-50, "Target");
console.log("My balance after groceries at Target", account.balance()) // 50
// cannot overcharge
account.charge(-1000, "Diamond Shop")
console.log("Cannont overcharge", account.balance()) // 50
// can do refunds
account.charge(20, "Target");
console.log("I got a $20 refund for part of my groceries", account.balance()) // 70
console.log("My number of transactions is", account.transactions.length) // 3