3 Replies Latest reply: Jul 5, 2007 12:46 AM by 807600 RSS

    toString methods

    807600
      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
       */
      public class Transactions {
          
          public static void 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;
      public class Account {
          private final double RATE = 0.035;
          private long acctNumber;
          private double balance;
          private String name;
          
          /** Creates a new instance of Account */
          public Account(String owner, long account, double initial) {
              name = owner;
              acctNumber = account;
              balance = initial;
          }
          public double deposit(double amount){
              balance = balance+amount;
              return balance;
          }
          public double withdraw(double amount, double fee){
              balance = balance-amount-fee;
              return balance;
          }
          public double addInterest(){
              balance+=(balance * RATE);
              return balance;
          }
          public double getBalance(){
              return balance;
          }
          public String toString(){
              NumberFormat fmt = NumberFormat.getCurrencyInstance();
              return acctNumber+"\t"+name+"\t"+fmt.format(balance);
          }
          
      }