/** * * BankAccount class defines the state and behavior of BankAccount Objects. * * The state ... name,balance * * The behaviors: * * Accessor methods : getName,getBalance. * Transformers (Mutators): withdraw, deposit. * Constructors : Used to initialize the object * */ // Author: Anthony Larrain import java.text.DecimalFormat; // Begin BankAccount Definition class BankAccount{ // Instance variables private String name; private double balance; public BankAccount(String N, double B){ name = N; if(B >= 0){ balance = B; }else{ balance = 0.0; System.out.println("Sorry balance must be non-negative, Balance is $0.00"); } } /* * The following methods are instance methods. * A method defined WITHOUT the keyword static * is an instance method. * */ // Accessor methods public String getName(){ return name; } public double getBalance(){ return balance; } // Other behaviors public void deposit(double amount){ if(amount >= 0){ balance += amount; } } public void withdraw(double amount){ if( balance >= amount ) { balance -= amount; }else{ System.out.println("Insufficient Funds...."); } } public String toString(){ DecimalFormat fmt = new DecimalFormat("0.00"); return name + '\n' + "Balance ... $" + fmt.format(balance); } }