-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccountProgram.java
More file actions
144 lines (115 loc) · 3.77 KB
/
BankAccountProgram.java
File metadata and controls
144 lines (115 loc) · 3.77 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
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
import java.util.Date;
public class BankAccountProgram {
public static class Account {
private int id;
private double balance;
private static double annualInterestRate;
private Date dateCreated;
public Account() {
dateCreated = new Date();
}
public Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
dateCreated = new Date();
}
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
id = newId;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public static void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", balance=" + balance +
", dateCreated=" + dateCreated +
'}';
}
}
public static class SavingsAccount extends Account {
public SavingsAccount() {
super();
}
public SavingsAccount(int newId, double newBalance) {
super(newId, newBalance);
}
@Override
public void withdraw(double amount) {
if (getBalance() - amount >= 0) {
super.withdraw(amount);
} else {
System.out.println("Error: Insufficient funds for withdrawal from savings account");
}
}
@Override
public String toString() {
return "Savings" + super.toString();
}
}
public static class CheckingAccount extends Account {
private double overdraftLimit;
public CheckingAccount() {
super();
overdraftLimit = 0;
}
public CheckingAccount(int newId, double newBalance, double overdraftLimit) {
super(newId, newBalance);
this.overdraftLimit = overdraftLimit;
}
@Override
public void withdraw(double amount) {
if (getBalance() - amount >= -overdraftLimit) {
super.withdraw(amount);
} else {
System.out.println("Error: Overdraft limit exceeded for checking account");
}
}
@Override
public String toString() {
return "Checking" + super.toString() + ", overdraftLimit=" + overdraftLimit + '}';
}
}
public static void main(String[] args) {
Account acc = new Account(1, 1000);
CheckingAccount checkAcc = new CheckingAccount(2, 2000, 500);
SavingsAccount saveAcc = new SavingsAccount(3, 3000);
System.out.println(acc.toString());
acc.deposit(500);
acc.withdraw(100);
System.out.println(acc.toString());
System.out.println(checkAcc.toString());
checkAcc.deposit(500);
checkAcc.withdraw(2600);
System.out.println(checkAcc.toString());
System.out.println(saveAcc.toString());
saveAcc.deposit(500);
saveAcc.withdraw(3500);
System.out.println(saveAcc.toString());
}
}