Source of AccountTest.java


  1: // Fig. 3.14: AccountTest.java
  2: // Create and manipulate an Account object.
  3: import java.util.Scanner;
  4: 
  5: public class AccountTest
  6: {
  7:    // main method begins execution of Java application
  8:    public static void main( String args[] ) 
  9:    {
 10:       Account account1 = new Account( 50.00 ); // create Account object
 11:       Account account2 = new Account( -7.53 ); // create Account object
 12: 
 13:       // display initial balance of each object
 14:       System.out.printf( "account1 balance: $%.2f\n", 
 15:          account1.getBalance() );
 16:       System.out.printf( "account2 balance: $%.2f\n\n", 
 17:          account2.getBalance() );
 18:       
 19:       // create Scanner to obtain input from command window
 20:       Scanner input = new Scanner( System.in );
 21:       double depositAmount; // deposit amount read from user
 22: 
 23:       System.out.print( "Enter deposit amount for account1: " ); // prompt
 24:       depositAmount = input.nextDouble(); // obtain user input
 25:       System.out.printf( "\nadding %.2f to account1 balance\n\n", 
 26:          depositAmount );
 27:       account1.credit( depositAmount ); // add to account1 balance
 28: 
 29:       // display balances
 30:       System.out.printf( "account1 balance: $%.2f\n", 
 31:          account1.getBalance() );
 32:       System.out.printf( "account2 balance: $%.2f\n\n", 
 33:          account2.getBalance() );
 34: 
 35:       System.out.print( "Enter deposit amount for account2: " ); // prompt
 36:       depositAmount = input.nextDouble(); // obtain user input
 37:       System.out.printf( "\nadding %.2f to account2 balance\n\n", 
 38:          depositAmount );
 39:       account2.credit( depositAmount ); // add to account2 balance
 40: 
 41:       // display balances
 42:       System.out.printf( "account1 balance: $%.2f\n", 
 43:          account1.getBalance() );
 44:       System.out.printf( "account2 balance: $%.2f\n", 
 45:          account2.getBalance() );
 46:    } // end main
 47: 
 48: } // end class AccountTest
 49: 
 50: 
 51: /**************************************************************************
 52:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 53:  * Pearson Education, Inc. All Rights Reserved.                           *
 54:  *                                                                        *
 55:  * DISCLAIMER: The authors and publisher of this book have used their     *
 56:  * best efforts in preparing the book. These efforts include the          *
 57:  * development, research, and testing of the theories and programs        *
 58:  * to determine their effectiveness. The authors and publisher make       *
 59:  * no warranty of any kind, expressed or implied, with regard to these    *
 60:  * programs or to the documentation contained in these books. The authors *
 61:  * and publisher shall not be liable in any event for incidental or       *
 62:  * consequential damages in connection with, or arising out of, the       *
 63:  * furnishing, performance, or use of these programs.                     *
 64:  *************************************************************************/