/** * This class represents a bank account. * @author cs161 * @version sp18 */ public class BankAccount{ // Instance Variables private final double INTEREST_RATE = .014; private String ownersName; private String accountNumber; private double balance; private String password; /** * Initializes a bank account with given information and * zero balance. * @param name The account owner's name * @param pwd The account password * @param id The account identification number */ public BankAccount(String name, String pwd, String id){ ownersName = name; accountNumber = id; balance = 0.0; password = pwd; } /** * Withdraws a specified amount of money from the account * @param moneyOut The amount to be withdrawn * @return The updated balance */ public double withdraw(double moneyOut){ if(balance >= moneyOut){ balance = balance - moneyOut; } return balance; } /** * Deposits a specified amount of money into the account * @param moneyIn The amount the be deposited * @return The updated balance */ public double deposit(double moneyIn){ balance = balance + moneyIn; return balance; } /** * Returns the account id * @return The account id */ public String getAccountId(){ return accountNumber; } /** * Returns the current balance * @return The current balance */ public double checkBalance(){ return balance; } /** * Changes the password for the account * @param oldPwd The old password for the account * @param newPwd The new password for the account * @return true or false to indicate success or failure */ public boolean changePassword(String oldPwd, String newPwd){ // The owner must supply the old password in order to change // to the new password if(password.equals(oldPwd)){ password = newPwd; return true; } return false; } /** * Transfers money from this account to other account * @param Amount to be transfered * @param other The other account */ public void transfer(double amount, BankAccount other){ this.withdraw(amount); other.deposit(amount); } /** * Returns a string summarizing the state of the account * @return The state of the account */ public String toString(){ String stateOfAccount = "[name = " + ownersName; stateOfAccount += ", account number =" + accountNumber; stateOfAccount += ", balance = " + balance + "]"; return stateOfAccount; } }