/** * BankAccount Class represent a very simplified bank account with a name and balance. */ public class BankAccount { private String name = ""; private double balance = 0D; /** * Constructs a new bank account object with a given name. * @param name is the name for the bank account object. */ public BankAccount(String name) { this.name = name; } private BankAccount() { } /** * The name of the current bank account object. * @return the name of the bank account object. */ public String getName() { return name; } /** * Changes the name of the current bank account object to a given name. * @param name is the new account name for the bank account object. */ public void setName(String name) { this.name = name; } /** * The balance of the current bank account object. * @return the balance of the bank account object. */ public double getBalance() { return balance; } /** * Changes the balance of the current bank account object to a given balance. * @param balance is the new account balance for the bank account object. */ public void setBalance(double balance) { this.balance = balance; } /** * Withdraws a given amount of money from the bank account. * @param amount is the amount of money which will be withdrawn from the bank account. */ public void withdraw(double amount) { this.balance = this.balance - amount; } /** * Deposits a given amount of money to the bank account. * @param amount is the amount of money which will be deposited to the bank account. */ public void deposit(double amount) { this.balance = this.balance + amount; } /** * String representation of the object. * @return a string representation that includes the name and balance of the bank account object. */ @Override public String toString() { return "BankAccount{" + "name='" + name + '\'' + ", balance=" + balance + '}'; } /** * Checks to see if two objects are equal. * @param o is the other object that is going to be examined for equality with the current bank account object. * @return true if the other object is equal to the current bank account object. */ @Override public boolean equals(Object o) { //check the basics if (null == o) return false; if (this == o) return true; if (this.getClass() != o.getClass()) return false; if (!(o instanceof BankAccount)) return false; BankAccount that = (BankAccount) o; if (Double.compare(that.getBalance(), getBalance()) != 0) return false; return getName().equals(that.getName()); } /** * Calculates a hash code for the current bank account object. * @return a hash code value for the current bank account object. */ @Override public int hashCode() { int result; long temp; result = getName().hashCode(); temp = Double.doubleToLongBits(getBalance()); result = 31 * result + (int) (temp ^ (temp >>> 32)); return result; } }