diff --git a/Bank Account Class b/Bank Account Class new file mode 100644 index 0000000..59fe356 --- /dev/null +++ b/Bank Account Class @@ -0,0 +1,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 { + // Constant Comparator object for comparing BankAccounts by balances + public static final Comparator 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 { + /** + * 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; + } + } + +}