JAVA   13

BankTester.java

Guest on 22nd September 2021 03:53:08 PM

  1. /*
  2.    File: BankTester.java (interactive input version)
  3.    
  4.    A test class for the Bank class.  Reads data for any number of bank accounts
  5.    until eof, from the keyboard
  6.    
  7.    After reading the data for each account, a BankAccount object is created
  8.    and added to the bank
  9. */
  10.  
  11. import GregBank.BankAccount;
  12. import GregBank.Bank;
  13. import java.io.IOException;
  14. import java.util.Scanner ;
  15. import javax.swing.JOptionPane ;
  16.  
  17. /**
  18.    A test class for the Bank class.
  19. */
  20. public class BankTester
  21. {
  22.    public static void main(String[] args) throws IOException
  23.    {
  24.       // create Bank object
  25.       Bank firstBankOfJava = new Bank() ;
  26.      
  27.       // "priming" read
  28.      
  29.       String input = JOptionPane.showInputDialog
  30.               ("Enter first account number and initial balance" +
  31.                "\n(separated by spaces)  (Cancel to quit)") ;
  32.              
  33.       while ( input != null )   // while not eof
  34.       {
  35.          Scanner scan = new Scanner(input) ;
  36.          String accountNumber = scan.next() ;
  37.          double balance = scan.nextDouble() ;
  38.          // create BankAccount object
  39.          BankAccount next = new BankAccount(accountNumber, balance) ;  
  40.          // add to list  
  41.          firstBankOfJava.addAccount( next ) ;
  42.          input = JOptionPane.showInputDialog
  43.               ("Enter next account number and initial balance" +
  44.                "\n(separated by spaces)  (Cancel to quit)") ;
  45.       }
  46.       // here at eof - list has been populated      
  47.  
  48.       // print the list of accounts
  49.       firstBankOfJava.printList() ;
  50.      
  51.       // get total on deposit, all accounts
  52.       System.out.println( "\nTotal on deposit in all accounts is $" +
  53.                           + firstBankOfJava.getTotalBalance() ) ;
  54.            
  55.       // find number of accounts with balance of at least specified amount
  56.      
  57.       input = JOptionPane.showInputDialog
  58.                      ("Find number of accounts with what minimum balance?" ) ;
  59.       double threshold = Double.parseDouble(input) ;
  60.       int count = firstBankOfJava.count(threshold) ;
  61.       System.out.println("\nFound " + count + " accounts with balance of "
  62.                          + "at least $" + threshold) ;
  63.      
  64.       // find account with greatest balance
  65.  
  66.       BankAccount max = firstBankOfJava.getMaximum() ;
  67.       if ( max != null )  // there are accounts
  68.          System.out.println( "\nAccount with number " + max.getAccountNumber()
  69.                              + " has the largest balance ($" +
  70.                              + max.getBalance() + ")" ) ;
  71.       else
  72.          System.out.println( "\nSorry, no accounts!" ) ;
  73.      
  74.       // search the list for some specific accounts
  75.      
  76.       String accountNumber = JOptionPane.showInputDialog
  77.                     ("Enter account number to search for (or Cancel to quit)") ;
  78.       while ( accountNumber != null )                   // while not eof
  79.       {
  80.          BankAccount target = firstBankOfJava.find(accountNumber) ;
  81.          if (target == null)
  82.             System.out.println("\nNo account with number " + accountNumber) ;
  83.          else
  84.             System.out.println("\nAccount with number " + accountNumber
  85.                                 + " has balance of $" + target.getBalance() ) ;
  86.          accountNumber = JOptionPane.showInputDialog
  87.            ("Enter next account number to search for (or Cancel to quit)") ;
  88.       }
  89.       System.exit(0) ;
  90.    }
  91. }
  92.  
  93.  
  94. /*  sample output
  95.  
  96.  Account Number     Account Balance
  97.  ==============     ===============
  98.         1111111     $     15,000.00
  99.         2222222     $     20,000.00
  100.         3333333     $     12,500.00
  101.         4444444     $     37,000.00
  102.  
  103. Total on deposit in all accounts is $84500.0
  104.  
  105. Found 3 accounts with balance of at least $15000.0
  106.  
  107. Account with number 4444444 has the largest balance ($37000.0)
  108.  
  109. Account with number 2222222 has balance of $20000.0
  110.  
  111. No account with number 9999999
  112.  
  113. */

Raw Paste


Login or Register to edit or fork this paste. It's free.