Consider the class BankAccount, for use in a bank?s account record system. publi
ID: 665712 • Letter: C
Question
Consider the class BankAccount, for use in a bank?s account record system. public class BankAccount { private String accName; II the account holder?s name, e.g. Bill Gates private int balance; public String getAccName() { return accName; public int getBalance { return balance; II constructor and other details omitted Write an efficient method public String findHighest(BankAccount accList) that returns the name of the account holder of the account with the highest balance in accList. findHighest should throw an exception if accList is empty or null. You may assume that the records are sorted by their account names.Explanation / Answer
public String findHighest(BankAccount[] accList)
{
int maxBalance=accList[0].getBalance();
int flag=-1;
for(int i=0;i<accList.length;i++) //search for account with highest balance
{
if(accList[i].getBalance()>maxBalance){
maxBalance=accList[i].getBalance();
flag=i;
}
}
if(accList.length==0)
try {
throw new nullException(); //if null throws exception
} catch (nullException e) {
e.printStackTrace();
}
return accList[flag].getAccName(); //return accName with Highest balance
}
class nullException extends Exception
{
String msg;
nullException()
{
msg = new String("Record is empty");
}
}