Create a Bank object In a loop repeat 5 times Using a Scanne

Create a Bank object In a loop, repeat 5 times: Using a Scanner, prompt the user for the balance of an Account object. Call the addAccount method of the Bank to add an Account object to the Array using the balance entered by the user. You can call either of the two addAccount methods of the Bank class. At the end of the loop, print the Bank object. Using the methods of the Bank class, print the following in the format shown below: The total balance of all accounts in the Bank using the getTotalBalance() method of the Bank class The Account with the highest balance in the array of Accounts using the getMaxBalance() method of the Bank class.

Solution

/*

FileName :Bank.java

*/

import java.util.Scanner;
import java.util.Arrays;


public class Bank {
Account [] account=new Account[5];
  
public void addAccount(Account ac,int i ){
account[i]=ac;
}
  
public static void main(String[] args) {
Bank bank =new Bank();
Scanner sc = new Scanner(System.in);
for(int i=0;i<5;i++){
Double balance = sc.nextDouble();
bank.addAccount(new Account(balance),i);
}
System.out.println(bank);//if you want to print object properties so Override toString method
System.out.println(\"Total Balance:\"+bank.getTotalBalance());
System.out.println(\"Total Balance:\"+bank.getMaxBlance());
  
}
public double getTotalBalance(){
double sum=0;
for(int i=0;i<account.length;i++){
sum+=account[i].balance;
}
return sum;
}
  
public double getMaxBlance(){
Arrays.sort(account, new AccountComparator());
return account[account.length-1].balance;
}
  
}
class Account
// implements Comparable<Account>
{
double balance;

public Account(double balance) {
this.balance = balance;
}
  
}

class AccountComparator implements java.util.Comparator<Account>
{

  

@Override
public int compare(Account o1, Account o2) {
return new Double((o1).balance).compareTo(new Double((o2).balance));
}
  
}

 Create a Bank object In a loop, repeat 5 times: Using a Scanner, prompt the user for the balance of an Account object. Call the addAccount method of the Bank t
 Create a Bank object In a loop, repeat 5 times: Using a Scanner, prompt the user for the balance of an Account object. Call the addAccount method of the Bank t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site