/** * A simple bank account. (Version 2 with constructors.) * * @author Hal Perkins * @version July 10, 2001 */ public class BankAccount { String accountName; // account owner's name int accountNumber; // account number double balance; // current account balance in dollars /** * Construct new BankAccount with empty name, and balance and account * number of 0. */ public BankAccount() { this.accountName = ""; this.accountNumber = 0; this.balance = 0.0; } /** * Construct new BankAccount with given name and account number, * and with a balance of 0.0. * @param accountName Account name * @param accountNumber Account Number */ public BankAccount(String accountName, int accountNumber) { this.accountName = accountName; this.accountNumber = accountNumber; this.balance = 0.0; } /** * Construct new BankAccount with given name, account number * and initial balance. * @param accountName Account name * @param accountNumber Account Number * @param initialBalance initial balance in Dollars */ public BankAccount(String accountName, int accountNumber, double initialBalance) { this.accountName = accountName; this.accountNumber = accountNumber; this.balance = initialBalance; } /** Set account owner's name * @param newAccountName the new name of this account */ public void setAccountName(String newAccountName) { this.accountName = newAccountName; } /** Set account number * @param number new number for this account */ public void setAccountNumber(int number) { this.accountNumber = number; } /** Deposit money in this account * @param amount amount of money to deposit in dollars */ public void deposit(double amount) { this.balance = this.balance + amount; } /** Get account balance. * @return Current account balance */ public double getBalance() { return this.balance; } /** Withdraw money from account. Only allow user to withdraw up to the * current account balance. * @param request amount requested * @return amount actually withdrawn */ public double withdraw(double request) { double amountWithdrawn; if (this.balance >= request) { amountWithdrawn = request; this.balance = this.balance - request; } else { amountWithdrawn = this.balance; this.balance = 0.0; } return amountWithdrawn; } }