-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bank Account Class
154 lines (140 loc) · 4.25 KB
/
Bank Account Class
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
import java.util.Comparator;
/**
* Keeps track of a Bank account information including the name (owner of the
* account) and the current balance
*
* @ElliottChoi
* @version December 2015
*/
public class BankAccount implements Comparable<BankAccount> {
// Constant Comparator object for comparing BankAccounts by balances
public static final Comparator<BankAccount> BALANCE_ORDER = new BalanceOrder();
private static double interestRate = 0;
private String name;
private double balance;
/**
* Constructs a new BankAccount object with the given name and balance
*
* @param newName
* the name of the account holder
* @param openBalance
* the opening balance for the new account
*/
public BankAccount(String newName, double openBalance) {
name = newName;
balance = openBalance;
}
// Display object using println, object's toString method is called to
// display the object as a String
// Without proper definition, inherit Object's toString method
/**
* Returns the BankAccount information as a String
*
* @returns the bank account name and current balance
*/
public String toString() {
return String.format("%-20s $%,10.2f", name, balance);
}
/**
* Allows the user to deposit money into the system
*
* @return the new bank account balance
*/
public double deposit(double value) {
return balance += value;
}
/**
* Allows the user to withdraw money from the system
*
* @return the new bank account balance
*/
public double withdraw(double value) {
return balance -= value;
}
/**
* Sets the current interest rate for all bank accounts
*
* @param newRate
* the new rate to set
*/
public static void setInterestRate(double newRate) {
interestRate = newRate;
}
/**
*
* @return balance plus the accrued interest for 1 month
*/
public double addInterest() {
return balance += (interestRate / (12 * 100)) * balance;
}
/**
*
* @param threshold
* @return boolean whether the value is above or below
*/
public boolean isBalanceOver(double threshold) {
return (balance > threshold ? true : false);
}
/**
* Checks to see if the given Object is a Bank account with the same name
* field as this bankAccount
*
* @param other:
* the object to compare this bankAccount
* @return true if the given object is a bankAccount with the same name as
* this bankAccount, else return false
*/
public boolean equals(Object other) {
//// Uses the "instanceof" operator to check if other
// is a reference to a BankAccount object
if (!(other instanceof BankAccount))
return false;
else {
//// We now know that other must refer to a BankAccount object so
//// we can cast it to a BankAccount reference. This step is
//// needed in order to be able to access the name field since
//// other.name would give an error
BankAccount otherAccount = (BankAccount) other;
return this.name.equals(otherAccount.name);
}
}
/**
* Compares this BankAccount to another BankAccount by comparing the name
* fields in each BankAccount
*
* @param other:
* the BankAccount to compare to this BankAccount
* @return a value < 0 if the name in this account comes alphabetically
* before the name in the other account, a value > 0, if this name
* comes after the other name and 0, if the names in the two
* accounts are the same
*/
public int compareTo(BankAccount other) {
return this.name.compareTo(other.name);
}
/**
** An inner Comparator class that compares two BankAccounts * by their
* balances
*/
private static class BalanceOrder implements Comparator<BankAccount> {
/**
* Compares the balances of two BankAccount objects
*
* @param first
* the first BankAccount to compare
* @param second
* the second BankAccount to compare
* @return a value < 0 if the first BankAccount has a lower balance, * a
* value < 0 if first BankAccount has a higher balance and * 0
* if the balances of the BankAccount are the same
*/
public int compare(BankAccount first, BankAccount second) {
if (first.balance < second.balance)
return -1;
else if (first.balance > second.balance)
return 1;
else
return 0;
}
}
}