we assume that balances must be greater than or equal to zero. The calls to method System.out.printf in lines 13–16 output the account names and balances, which are obtained by calling each Account’s getName and getBalance methods.
Inputting and outputting floating-point numbers with Account objects in java
// Fig. 3.9: AccountTest.java
// Inputting and outputting floating-point numbers with Account objects.
import java.util.Scanner;
public class AccountTest
{
public static void main(String[] args)
{
// display initial balance of each object
System.out.printf("%s balance: $ %n",
account1.getName(), );
System.out.printf("%s balance: $ %n%n",
account2.getName(), );
// create a Scanner to obtain input from the command window
Scanner input = new Scanner(System.in);
System.out.print("Enter deposit amount for account1: "); // prompt
System.out.printf("%nadding to account1 balance%n%n",
depositAmount);
// display balances
System.out.printf("%s balance: $ %n",
account1.getName(), );
System.out.printf("%s balance: $ %n%n",
account2.getName(), );
System.out.print("Enter deposit amount for account2: "); // prompt
System.out.printf("%nadding to account2 balance%n%n",
depositAmount);
// display balances
System.out.printf("%s balance: $ %n",
account1.getName(), );
System.out.printf("%s balance: $ %n%n",
account2.getName(), );
} // end main
} // end class AccountTest
Output of Program
Jane Green balance: $50.00
John Blue balance: $0.00
Enter deposit amount for account1: 25.53
adding 25.53 to account1 balance
Jane Green balance: $75.53
John Blue balance: $0.00
Enter deposit amount for account2: 123.45
adding 123.45 to account2 balance
Jane Green balance: $75.53
John Blue balance: $123.45