//An instance of this class represents a basic bank account that permits // transactions with any non-negative transaction amount. // //Unique account numbers are generated by an internal process whenever a // new account is created // //Any attempted transaction with a negative amount results in aborting // the transaction // //PUBLIC INTERFACE // // Constructors: // 1) BasicBankAccount() // Creates a new account with 0.0 balance, and unique account number // 2) BasicBankAccount(double amount) // Creates a new account with a given non-negative initial balance // and a unique account number // Accessors: // 1) String getNumber() // Returns the number of this BasicBankAccount // 2) double getBalance() // Returna the balance of this BasicBankAccount // Mutators: // 1) void credit(double amount) // Increases the balance of an account by a given non-negative amount // 2) void debit(double amount) // Decreases the balance of an account by a given non-negative amount // The account balance is permitted to go negative // Other Methods: // 1) String toString() // Returns a printable description of the state of an account // // Norman Pestaina COP 2210 Summer 2016 // public class BasicBankAccount { //Instance (State) Variables private final String number; //Account number private double balance; //Current balance //Parameterless Constructor: // Creates a new BasicBankAccount with zero balance public BasicBankAccount() { this.number = newNumber(); this.balance = 0.0; } //Initializing Constructor: // Creates a new BasicBankAccount with a given balance (must be >= 0.0) public BasicBankAccount(double amount) { validate(amount); this.number = newNumber(); this.balance = amount; } //Accessor: // Returns the number of this BasicBankAccount public String getNumber() { return this.number; } //Accessor: // Returns the balance of this BasicBankAccount public double getBalance() { return this.balance; } //Mutator: // Increase the balance of this BasicBankAccount by a given amount public void credit(double amount) { validate(amount); this.balance = this.balance + amount; } //Mutator: // Decrease the balance of this BasicBankAccount by a given amount public void debit(double amount) { validate(amount); this.balance = this.balance - amount; } //Returns a printable image of this BasicBankAccount public String toString() { return "Account #" + this.number + " Balance: $" + this.balance; } //********************************************************************* //********************************************************************* //Class Variables and Methods private static int seedNumber = 7000000; //Base account number //Returns a new unique account # private static String newNumber() { seedNumber = seedNumber + 1; String number = "" + seedNumber; return number.substring(0, 1) + "-" + number.substring(1, 4) + "-" + number.substring(4); } //Validates the amount of any BasicBankAccount creation or transaction //Throws 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); } }