Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.63 KB | None | 0 0
  1.     /**
  2.      * This class contains a method that finds the richest account in an array
  3.      *
  4.      * @author Akash Lagad
  5.      */
  6.     class Lab10Exercise3
  7.     {
  8.             public static void main(String[] args)
  9.             {
  10.                     testNormal();
  11.                     testEmpty();
  12.             }
  13.      
  14.      
  15.             /**
  16.              * Perform a basic test in the scenario with an array with many accounts
  17.              */
  18.             public static void testNormal()
  19.             {
  20.                     // Make an array of accounts
  21.                     Account[] accounts = makeAccounts();
  22.      
  23.                     // Use the method which finds the richest account,
  24.                     // and print results to screen
  25.                     testMethodAndPrint(accounts);
  26.             }
  27.      
  28.      
  29.             /**
  30.              * Perform a basic test in the scenario with an empty array
  31.              */
  32.             public static void testEmpty()
  33.             {
  34.                     // Make an array of length 0;
  35.                     // This is an empty array containing no accounts
  36.                     Account[] accounts = new Account[0];
  37.      
  38.                     // Use the method which finds the richest account,
  39.                     // and print results to screen
  40.                     testMethodAndPrint(accounts);
  41.             }
  42.      
  43.      
  44.             /**
  45.              * Returns an array of accounts
  46.              */
  47.             public static Account[] makeAccounts()
  48.             {
  49.                     // Make 6 Account objects belonging to 6 different people
  50.                     Account a1 = new Account("Adam");
  51.                     Account a2 = new Account("Betty");
  52.                     Account a3 = new Account("Charlie");
  53.                     Account a4 = new Account("Diana");
  54.                     Account a5 = new Account("Ed");
  55.                     Account a6 = new Account("Fiona");
  56.      
  57.                     // Add some money into each account
  58.                     a1.deposit(200);
  59.                     a2.deposit(900);
  60.                     a3.deposit(400);
  61.                     a4.deposit(100);
  62.                     a5.deposit(1000);
  63.                     a6.deposit(400);
  64.      
  65.                     // Make an array to store these Account objects
  66.                     Account[] accounts = new Account[6];
  67.      
  68.                     // Put the accounts into the array
  69.                     accounts[0] = a1;
  70.                     accounts[1] = a2;
  71.                     accounts[2] = a3;
  72.                     accounts[3] = a4;
  73.                     accounts[4] = a5;
  74.                     accounts[5] = a6;
  75.      
  76.                     // Return the array of accounts
  77.                     return accounts;
  78.             }
  79.      
  80.      
  81.             /**
  82.              * Call the findRichestAccount method and then print out the details
  83.              * of the returned Account
  84.              */
  85.             public static void testMethodAndPrint(Account[] accounts)
  86.             {
  87.                     // Use the method which finds the richest account
  88.                     Account richestAccount = findRichestAccount(accounts);
  89.                     if(richestAccount!=null)
  90.                     {
  91.                     System.out.println ("Owner is " + richestAccount.getOwner());
  92.                     System.out.println ("Balance is " + richestAccount.getBalance());
  93.                     }
  94.                     else
  95.                     {
  96.                     System.out.println("There is no richest account because the array was empty");
  97.                     }
  98.             }
  99.      
  100.             /**
  101.              * Returns the account with the greatest balance;
  102.              * if there are no accounts in the given array, then null is returned;
  103.              * if there are more than 1 richest account, then any one of these richest accounts may be returned
  104.              */
  105.             public static Account findRichestAccount(Account[] accounts)
  106.             {
  107.                     double currentLargest = 0;
  108.                     Account currentLargestAccount = null;
  109.                     if (accounts.length == 0)
  110.                     {
  111.                             return null;
  112.                     }
  113.                     for (int i = 0; i < accounts.length; i++)
  114.                     {
  115.                             if (accounts[i].getBalance() > currentLargest)
  116.                             {
  117.                                     currentLargest = accounts[i].getBalance();
  118.                                     currentLargestAccount = accounts[i];
  119.                             }
  120.                     }
  121.                     return currentLargestAccount;
  122.             }
  123.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement