-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBankAccount.java
More file actions
87 lines (76 loc) · 2.33 KB
/
BankAccount.java
File metadata and controls
87 lines (76 loc) · 2.33 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
/**
* A class to represent a single bank account.
*/
public class BankAccount {
private double balance;
private int accountNumber;
/*
* Constructor
* @param initialBalance the initial balance of the BankAccount
* @param accountNumber The account number to associate with this
* BankAccount. Must be a 5 digit integer.
*/
public BankAccount(double initialBalance, int accountNumber) {
if (initialBalance < 0) {
throw new IllegalArgumentException(
"Accounts with a negative balance cannot be created!");
}
if (accountNumber < 9999 || accountNumber > 100000){
throw new IllegalArgumentException(
"Account number needs to be 5 digit number!");
}
balance = initialBalance;
this.accountNumber = accountNumber;
}
/**
* Deposits money into the BankAccount
*
* @param amount the amount to deposit
*/
public void deposit(double amount) {
if (amount < 0) {
throw new IllegalArgumentException(
"Don't deposit negative amounts!");
}
balance = balance + amount;
}
/**
* Withdraws money from the BankAccount
*
* @param amount the amount to withdraw
*/
public void withdraw(double amount) throws InsufficientFundsException {
if (amount < 0) {
throw new IllegalArgumentException(
"Don't withdraw a negative amount!");
}
if (amount > balance) {
throw new InsufficientFundsException(
"You can't withdraw more than you have!");
}
balance = balance - amount;
}
/**
* Gets the current balance of the BankAccount
*
* @return the current balance
*/
public double getBalance() {
return balance;
}
/**
* Gets the account number of the BankAccount
* @return the account number
*/
public int getAccountNumber() {
return accountNumber;
}
/**
* Returns a string representation this BankAccount in
* the following format, e.g. 12345 $100.52 where 12345
* is the account number, and 100.52 is the balance.
*/
public String toString () {
return "" + accountNumber + " $" + balance;
}
}