-
Notifications
You must be signed in to change notification settings - Fork 0
/
BankAccount.java
329 lines (303 loc) · 9.77 KB
/
BankAccount.java
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package Section4;
import java.text.DecimalFormat;
import java.util.Scanner;
/**
* Complete Java Masterclass - Section 4: OOP (1)
*
* Constructors - Part 1: Challenge
*
* This class will create a bank account object
* whereby the user will be able to both
* deposit and withdraw funds.
*
* Assuming that there is enough money
* in the bank account for withdrawals in particular.
*
* Update: A SCANNER has been added in the version
* to enable user-input and therefore allow the user
* to enter account details as well as conduct transactions.
* Therefore aiming to better resemble the program to
* that of an ATM machine.
*
* Update: Constructor chaining enabled for all three constructors
* in this class.
*
* @author Ben Silveston, Tim Buchalka
*/
public class BankAccount {
private String accountNumber;
private double balance;
private double givenAmount;
private String accountName;
private String email;
private String phoneNumber;
// Enable currency formatting to two decimal places:
private final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#0.00");
// Scanner to be used for user-input in the program:
private static final Scanner SCANNER = new Scanner(System.in);
/**
* Basic constructor
*
* The account balance is only defined
* and given a default value of £100.00.
*
* All customer details are defined on request at
* run-time.
*/
public BankAccount() {
this(100.00);
}
/**
* Constructor
*
* Provides default details for a new current
* account if these are not entered manually
*
* Also allows the balance to be defined a new value
* other than the default value of £100.00.
*
* @param balance The current account's balance
*/
public BankAccount(double balance) {
this("John Smith",
"54651652",
"01654-456572",
"johnsmith@gmail.com");
this.balance = balance;
}
/**
* Constructor
*
* Allows customer details to be defined here
* instead of the default values, as defined in the
* previous constructor.
*
* @param accountName The customer's registered account name
* @param accountNumber The customer's account number
* @param phoneNumber The customer's phone number
* @param email The customer's email address
*/
public BankAccount(String accountName, String accountNumber, String phoneNumber, String email) {
this.accountName = accountName;
this.accountNumber = accountNumber;
this.phoneNumber = phoneNumber;
this.email = email;
}
/**
* Set the new account number
* for the customer
*
* @param accountNumber The new account number
*/
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
/**
* Get the account number for
* the customer
*
* @return String
*/
public String getAccountNumber() {
return this.accountNumber;
}
/**
* Set the new balance
* for the customer
*
* @param balance The new account balance
*/
public void setBalance(double balance) {
this.balance = balance;
}
/**
* Get the customer's balance
*
* @return The current account balance
*/
public double getBalance() {
return this.balance;
}
/**
* Set the given amount to be
* deposited or withdrawn
*
* @param givenAmount The given amount
*/
public void setGivenAmount(double givenAmount) {
this.givenAmount = givenAmount;
}
/**
* Get the given amount to be
* deposited or withdrawn
*
* @return The current given amount
*/
public double getGivenAmount() {
return givenAmount;
}
/**
* Set the new account name
* for the customer
*
* @param accountName The new account name
*/
public void setAccountName(String accountName) {
this.accountName = accountName;
}
/**
* Get the customer's account name
*
* @return The current account name
*/
public String getAccountName() {
return this.accountName;
}
/**
* Set the new email address
* for the customer
*
* @param email The new email address
*/
public void setEmail(String email) {
this.email = email;
}
/**
* Get the email-address
* of the customer
*
* @return The current email-address
*/
public String getEmail() {
return this.email;
}
/**
* Set the phone number
* for the customer
*
* @param phoneNumber The new phone number
*/
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
/**
* Get the customer's
* phone number
*
* @return The current phone number
*/
public String getPhoneNumber() {
return this.phoneNumber;
}
/**
* Deposit a given amount
* of money
*
* @param amount The amount of money to deposit
*/
public void depositFunds(double amount) {
this.balance += amount;
System.out.println("The account " + this.accountNumber + " under the name of " + this.accountName +
" has just deposited £" + DECIMAL_FORMAT.format(amount));
System.out.println("The total account balance is now £" + DECIMAL_FORMAT.format(getBalance()));
}
/**
* Withdraw a given amount
* of money
*
* @param amount The amount of money to withdraw
*/
public void withdrawFunds(double amount) {
if(this.balance <= 0 || amount > this.balance) {
System.out.println("Unable to withdraw money, due to insufficient funds. Transaction failed.");
}
else {
this.balance -= amount;
System.out.println("The account " + this.accountNumber + " under the name of " + this.accountName +
" has just withdrawn £" + DECIMAL_FORMAT.format(amount));
System.out.println("The total account balance is now £" + DECIMAL_FORMAT.format(getBalance()));
}
}
/**
* Enter the account details
*/
public void enterAccountDetails() {
System.out.println("Please enter the following details - " +
"(account name, account number, phone number and email address): ");
this.accountName = SCANNER.nextLine();
this.setAccountName(this.accountName);
this.accountNumber = SCANNER.nextLine();
this.setAccountNumber(this.accountNumber);
this.phoneNumber = SCANNER.nextLine();
this.setPhoneNumber(this.phoneNumber);
this.email = SCANNER.nextLine();
this.setEmail(this.email);
}
/**
* Print account details
*/
private void printAccountDetails() {
System.out.println("Account name: " + this.getAccountName() + "\n" +
"Current account balance: £" + DECIMAL_FORMAT.format(this.getBalance()) + "\n" +
"Phone number: (" + this.getPhoneNumber() + ")" + "\n" +
"Email address: " + this.getEmail());
}
/**
* Enable the transaction whether
* that would a deposit or withdrawal
*/
public void enableTransaction() {
System.out.print("Do you wish to deposit or withdraw funds (Press 'd' / 'w')? ");
String answer = SCANNER.next();
while(SCANNER.hasNextLine()) {
if(answer.equals("d") || answer.equals("D")) {
System.out.print("Please enter the amount to deposit (£): ");
double amountToDeposit = SCANNER.nextDouble();
System.out.print("You are about to deposit £" + DECIMAL_FORMAT.format(amountToDeposit) +
", do you want to proceed? (Please type 'y' or 'n'): ");
String depositAnswer = SCANNER.next();
if(depositAnswer.equals("y") || depositAnswer.equals("Y")) {
this.depositFunds(amountToDeposit);
break;
}
else if(depositAnswer.equals("n") || depositAnswer.equals("N")) {
disableTransaction();
break;
}
else {
System.out.print("Please enter an answer (Y / N): ");
}
}
else if(answer.equals("w") || answer.equals("W")) {
System.out.print("Please enter the amount to withdraw (£): ");
double amountToWithdraw = SCANNER.nextDouble();
System.out.print("You are about to withdraw £" + DECIMAL_FORMAT.format(amountToWithdraw) +
", do you want to proceed? (Please type 'y' or 'n'): ");
String withdrawAnswer = SCANNER.next();
if(withdrawAnswer.equals("y") || withdrawAnswer.equals("Y")) {
this.withdrawFunds(amountToWithdraw);
break;
}
else if(answer.equals("n") || answer.equals("N")) {
disableTransaction();
break;
}
else {
System.out.print("Sorry, cannot recognise answer. Please enter an appropriate answer (Y / N): ");
}
}
else {
System.out.print("Sorry, cannot recognise answer. Please enter an appropriate answer (D / W): ");
}
}
SCANNER.close();
System.out.println("\nPlease find updated details below: ");
printAccountDetails();
}
/**
* Disable a transaction, based on the request of the user
*/
private void disableTransaction() {
System.out.println("Transaction has been disabled.");
}
}