toString methods
I am sorry for posting all this code. I am wondering if someone would be gracious to answer a question regarding the toString() method. Where and how is it called? Is it implicitly called somewhere? Can someone explain this to me please?
/*
* Transactions.java
*
* Created on July 4, 2007, 10:18 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author pberardi
*/
publicclass Transactions{
publicstaticvoid main(String[] args){
Account acct1 =new Account("Ted Murphy",72354, 102.56);
Account acct2 =new Account("Jane Smith", 69713, 40.00);
Account acct3 =new Account("Edward Demsey", 93757, 759.32);
acct1.deposit(25.85);
double smithBalance = acct2.deposit(500.00);
System.out.println("Smith balance after depost:"+smithBalance);
System.out.println("Smith balance after withdrawal:"+acct2.withdraw(430.75,1.50));
acct1.addInterest();
acct2.addInterest();
acct3.addInterest();
System.out.println();
System.out.println(acct1);
System.out.println(acct2);
System.out.println(acct3);
}
}
/*
* Account.java
*
* Created on July 4, 2007, 10:23 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author pberardi
*/
import java.text.NumberFormat;
publicclass Account{
privatefinaldouble RATE = 0.035;
privatelong acctNumber;
privatedouble balance;
private String name;
/** Creates a new instance of Account */
public Account(String owner,long account,double initial){
name = owner;
acctNumber = account;
balance = initial;
}
publicdouble deposit(double amount){
balance = balance+amount;
return balance;
}
publicdouble withdraw(double amount,double fee){
balance = balance-amount-fee;
return balance;
}
publicdouble addInterest(){
balance+=(balance * RATE);
return balance;
}
publicdouble getBalance(){
return balance;
}
public String toString(){
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return acctNumber+"\t"+name+"\t"+fmt.format(balance);
}
}

