//An instance of this class represents a basic bank account that permits // the usual transactions with any non-negative transaction amount: // deposit(), withdraw(), transfer() // public class BankAccount { //Instance (State) Variables private String accountNumber; //Account Number private double currentBalance;//Current Balance //Parameterized Constructor: // Create a new BankAccount with a given initial balance // and a unique (automatically generated) account number public BankAccount(double initialBalance) { this.accountNumber = newAccountNumber(); this.credit(initialBalance); } //Parameterless Constructor: // Create a new BankAccount with 0.0 initial balance public BankAccount() { this(0.0); } //Accessor: // Return the account number of this BankAccount public String getAccountNumber() { return this.accountNumber; } //Accessor: // Returns the current balance of this BankAccount public double getCurrentBalance() { return this.currentBalance; } //Mutator: // Add a given amount to the current balance of this BankAccount public void deposit(double amount) { this.credit(amount); } //Mutator: // Reduce the current balance of this BankAccount by a given amount public void withdraw(double amount) { if (amount > this.currentBalance) throw new RuntimeException("Insufficient Funds"); this.debit(amount); } //Mutator: // Transfer a given amount to this BankAccount from another BankAccount public void transfer(double amount, BankAccount source) { source.withdraw(amount); this.deposit(amount); } //Override: // Return a printable image of the state of this BankAccount public String toString() { return this.getClass().getSimpleName() + " #" + this.accountNumber + " Balance: $" + this.currentBalance; } //Override: // Return true iff this BankAccount is identical to another BankAccount public boolean equals(Object other) { if ( other == null ) return false; if ( this.getClass() != other.getClass() ) return false; BankAccount that = (BankAccount)other; return this.accountNumber.equals(that.accountNumber) && this.currentBalance == that.currentBalance; } // Return an exact replica of this BankAccount // **** Will override inherited clone() method later **** public BankAccount copy() { BankAccount twin = new BankAccount(this.currentBalance); twin.accountNumber = this.accountNumber; this.seedNumber--; return twin; } /* //Override: // Return a copy of this BankAccount public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException cnse) { throw new RuntimeException("Clone: " + cnse.getMessage()); } } */ //********************************************************************* //********************************************************************* //Class Variables and Methods private static int seedNumber = 7000000; //Base account accountNumber //Constructor Helper: // Return a new unique account # private static String newAccountNumber() { seedNumber = seedNumber + 1; String accountNumber = "" + seedNumber; return accountNumber.substring(0, 1) + "-" + accountNumber.substring(1, 4) + "-" + accountNumber.substring(4); } //Helper: // Increase the balance of this BankAccount by a given amount private void credit(double amount) { validate(amount); this.currentBalance += amount; } //Helper: // Decrease the balance of this BankAccount by a given amount private void debit(double amount) { validate(amount); this.currentBalance -= amount; } //Helper: // Validate the amount of any BankAccount creation or transaction // Throw a RuntimeException if the amount is negative (< 0.0) private static void validate(double amount) { if (amount < 0.0) throw new RuntimeException("Invalid amount $" + amount); } }