Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * This class contains a method that finds the richest account in an array
- *
- * @author Akash Lagad
- */
- class Lab10Exercise3
- {
- public static void main(String[] args)
- {
- testNormal();
- testEmpty();
- }
- /**
- * Perform a basic test in the scenario with an array with many accounts
- */
- public static void testNormal()
- {
- // Make an array of accounts
- Account[] accounts = makeAccounts();
- // Use the method which finds the richest account,
- // and print results to screen
- testMethodAndPrint(accounts);
- }
- /**
- * Perform a basic test in the scenario with an empty array
- */
- public static void testEmpty()
- {
- // Make an array of length 0;
- // This is an empty array containing no accounts
- Account[] accounts = new Account[0];
- // Use the method which finds the richest account,
- // and print results to screen
- testMethodAndPrint(accounts);
- }
- /**
- * Returns an array of accounts
- */
- public static Account[] makeAccounts()
- {
- // Make 6 Account objects belonging to 6 different people
- Account a1 = new Account("Adam");
- Account a2 = new Account("Betty");
- Account a3 = new Account("Charlie");
- Account a4 = new Account("Diana");
- Account a5 = new Account("Ed");
- Account a6 = new Account("Fiona");
- // Add some money into each account
- a1.deposit(200);
- a2.deposit(900);
- a3.deposit(400);
- a4.deposit(100);
- a5.deposit(1000);
- a6.deposit(400);
- // Make an array to store these Account objects
- Account[] accounts = new Account[6];
- // Put the accounts into the array
- accounts[0] = a1;
- accounts[1] = a2;
- accounts[2] = a3;
- accounts[3] = a4;
- accounts[4] = a5;
- accounts[5] = a6;
- // Return the array of accounts
- return accounts;
- }
- /**
- * Call the findRichestAccount method and then print out the details
- * of the returned Account
- */
- public static void testMethodAndPrint(Account[] accounts)
- {
- // Use the method which finds the richest account
- Account richestAccount = findRichestAccount(accounts);
- if(richestAccount!=null)
- {
- System.out.println ("Owner is " + richestAccount.getOwner());
- System.out.println ("Balance is " + richestAccount.getBalance());
- }
- else
- {
- System.out.println("There is no richest account because the array was empty");
- }
- }
- /**
- * Returns the account with the greatest balance;
- * if there are no accounts in the given array, then null is returned;
- * if there are more than 1 richest account, then any one of these richest accounts may be returned
- */
- public static Account findRichestAccount(Account[] accounts)
- {
- double currentLargest = 0;
- Account currentLargestAccount = null;
- if (accounts.length == 0)
- {
- return null;
- }
- for (int i = 0; i < accounts.length; i++)
- {
- if (accounts[i].getBalance() > currentLargest)
- {
- currentLargest = accounts[i].getBalance();
- currentLargestAccount = accounts[i];
- }
- }
- return currentLargestAccount;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement