-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathBankAccount.java
More file actions
194 lines (162 loc) · 5.49 KB
/
BankAccount.java
File metadata and controls
194 lines (162 loc) · 5.49 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package armstrong.alexandra;
import java.io.*;
import static armstrong.alexandra.Status.*;
import static armstrong.alexandra.AccountType.*;
import static armstrong.alexandra.Overdraft.*;
/**
* Created by alexandraarmstrong on 1/17/17.
*/
public class BankAccount {
private final static int ROUTERNUMBER = 1234567;
private static int accountCounter = 0;
private AccountType accountType;
private long accountNumber;
private double balance = 0;
private String accountHolderName;
private double interestRate = 1.00d;
private Status status = CLOSED;
private Overdraft overdraft = DISABLED;
//private File record;
BankAccount(AccountType accountType, String accountHolderName){
this.accountType = accountType;
this.accountHolderName = accountHolderName;
accountCounter++;
accountNumber = Integer.valueOf(String.valueOf(ROUTERNUMBER) + String.valueOf(accountCounter));
status = OPEN;
}
BankAccount(AccountType accountType, String accountHolderName, Overdraft overdraft){
this(accountType, accountHolderName);
this.overdraft = overdraft;
}
BankAccount(AccountType accountType, String accountHolderName, double interestRate){
this(accountType, accountHolderName);
this.interestRate = interestRate;
}
BankAccount(AccountType accountType, String accountHolderName, double interestRate, Overdraft overdraft){
this(accountType, accountHolderName, overdraft);
this.interestRate = interestRate;
}
public AccountType getAccountType(){
return accountType;
}
public long getAccountNumber(){
return accountNumber;
}
public Double getBalance(){
if (status == FROZEN) {
return null;
} else {
return balance;
}
}
public String getAccountHolderName(){
return accountHolderName;
}
public double getInterestRate(){
return interestRate;
}
public Status getStatus(){
return status;
}
public Overdraft getOverdraft() {
return overdraft;
}
public void setOverdraft(Overdraft overdraft){
this.overdraft = overdraft;
}
public void setInterestRate(double interestRate){
this.interestRate = interestRate;
writeToFileChangeInterestRate();
}
protected void setStatus(Status status){
this.status = status;
writeToFileChangeStatus();
}
public String changeBalance(double amount) {
if (status == OPEN) {
if (overdraft == ENABLED) {
if (amount > 0 || amount > getBalance()) {
balance += amount;
writeToFileChangeBalance();
return "Balance adjusted";
} else {
return "Insufficient Funds";
}
} else {
balance += amount;
writeToFileChangeBalance();
return "Balance adjusted";
}
} else {
return "Balance inaccessible";
}
}
public String transferMoneyToOtherAccount(BankAccount otherAccount, double amount) {
if (accountHolderName.equalsIgnoreCase(otherAccount.accountHolderName)) {
if (getBalance() > amount || overdraft != DISABLED) {
changeBalance(-1 * amount);
otherAccount.changeBalance(amount);
return "Transfer Successful";
} else {
return "Insufficient funds";
}
} else {
return "Permission Denied";
}
}
public void setAccountHolderName(String name){
if(status != CLOSED){
this.accountHolderName = name;
writeToFileChangeName();
}
}
public String closeAccount(){
if(getBalance() == 0d){
setStatus(CLOSED);
return "Account Closed";
} else {
return "Please withdraw funds";
}
}
public String changeFreezeStatus(){
if (status == FROZEN) {
setStatus(OPEN);
return "Account unfrozen";
} else {
setStatus(FROZEN);
return "Account frozen";
}
}
private void writeToFileChangeBalance() {
try {
PrintStream print = new PrintStream(new BufferedOutputStream(new FileOutputStream("BankAccount.txt", true)));
print.println("Balance Changed to " + balance + ".");
print.close();
} catch (IOException e) {
}
}
private void writeToFileChangeStatus(){
try {
PrintStream print = new PrintStream(new BufferedOutputStream(new FileOutputStream("BankAccount.txt", true)));
print.println("Status changed to " + status + ".");
print.close();
} catch (IOException e) {
}
}
private void writeToFileChangeName(){
try {
PrintStream print = new PrintStream(new BufferedOutputStream(new FileOutputStream("BankAccount.txt", true)));
print.println("Name changed to " + accountHolderName + ".");
print.close();
} catch (IOException e) {
}
}
private void writeToFileChangeInterestRate(){
try {
PrintStream print = new PrintStream(new BufferedOutputStream(new FileOutputStream("BankAccount.txt", true)));
print.println("Interest rate changed to " + interestRate + ".");
print.close();
} catch (IOException e) {
}
}
}