In: Computer Science
Exercise 4: Perform the following on the Account class and test it in the AccountArrayTest: In the AccountArrayTest class, add a static method called getAverageBalance (Account accounts[]) that returns the average balance of all accounts. Use the enhanced for loop. In the main method get and print the average accounts balance. Add an instance method called deposit() that takes a double amount as parameter and if it is positive it adds it to the current balance. If the amount is less than or equal to zero it does nothing. Add a static method called addInterest(Account accounts[]) that calculates the interest for each account in the accounts array (based on the interestRate value), and then deposits it into the relevant account in that array. (Hint: interest = interestRate * balance/100). Use the above methods to apply the addInterest() for all the accounts in your AccountArrayTest. Print the accountNo and balance after the update.
/****************************************Account.java**********************/
// TODO: Auto-generated Javadoc
/**
* The Class Account.
*/
public class Account {
/** The owner. */
private String owner;
/** The account number. */
private String accountNumber;
/** The balance. */
private double balance;
/** The interest rate. */
private double interestRate;
/**
* Instantiates a new account.
*
* @param owner the owner
* @param accountNumber the account number
* @param balance the balance
*/
public Account(String owner, String accountNumber,
double balance) {
super();
this.owner = owner;
this.accountNumber =
accountNumber;
this.balance = balance;
}
/**
* Gets the owner.
*
* @return the owner
*/
public String getOwner() {
return owner;
}
/**
* Sets the owner.
*
* @param owner the new owner
*/
public void setOwner(String owner) {
this.owner = owner;
}
/**
* Gets the interest rate.
*
* @return the interest rate
*/
public double getInterestRate() {
return interestRate;
}
/**
* Sets the interest rate.
*
* @param interestRate the new interest rate
*/
public void setInterestRate(double interestRate)
{
this.interestRate =
interestRate;
}
/**
* Gets the account number.
*
* @return the account number
*/
public String getAccountNumber() {
return accountNumber;
}
/**
* Sets the account number.
*
* @param accountNumber the new account number
*/
public void setAccountNumber(String accountNumber)
{
this.accountNumber =
accountNumber;
}
/**
* Gets the balance.
*
* @return the balance
*/
public double getBalance() {
return balance;
}
/**
* Sets the balance.
*
* @param balance the new balance
*/
public void setBalance(double balance) {
this.balance = balance;
}
/**
* Deposit.
*
* @param amount the amount
*/
public void deposit(double amount) {
this.balance += amount;
}
/**
* Withdraw.
*
* @param amount the amount
*/
public void withdraw(double amount) {
if (balance > amount) {
this.balance
-= amount;
}
}
/**
* Adds the interest.
*/
public void addInterest() {
this.balance += balance *
interestRate / 100;
}
}
/**********************************AccountArrayTest.java**********************/
/**
* The Class AccountArrayTest.
*/
public class AccountArrayTest {
/**
* Gets the average balance.
*
* @param accounts the accounts
* @return the average balance
*/
public static double getAverageBalance(Account[]
accounts) {
double totalBalance = 0;
for (Account account : accounts)
{
totalBalance
+= account.getBalance();
}
return totalBalance / accounts.length;
}
/**
* Adds the interest.
*
* @param accounts the accounts
*/
public static void addInterest(Account[] accounts)
{
for (Account account : accounts) {
account.addInterest();
}
}
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
Account[] accounts = new Account[3];
accounts[0] = new
Account("Virat", "12478252", 20000);
accounts[1] = new Account("Kohli",
"12478252", 20000);
accounts[2] = new Account("MS",
"12478252", 20000);
accounts[0].setInterestRate(10);
accounts[1].setInterestRate(9);
accounts[2].setInterestRate(12);
System.out.println("Average
Balance: " + getAverageBalance(accounts));
addInterest(accounts);
for (Account account : accounts) {
System.out.println(
"Account number: " +
account.getAccountNumber() + " has balance: $" +
account.getBalance());
}
}
}
/*****************************output******************************/
Average Balance: 20000.0
Account number: 12478252 has balance: $22000.0
Account number: 12478252 has balance: $21800.0
Account number: 12478252 has balance: $22400.0
Please let me know if you have any doubt or modify the answer, Thanks :)