In: Computer Science
Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to be withdrawn than what is available in the account, nor should you allow for negative deposits. Finally, there should be a function that adds interest to the balance (interest accrual) at the current interest rate. This function should have a parameter indicating how many months’ worth of interest are to be added (for example, 6 indicate the account should have 6 months’ added). Interest is accrued from an annual rate. The month is representative of 1/12 that amount. Each month should be calculated and added to the total separately.
For example:
1 month accrued at 5% APR is Balance = ((Balance * 0.05)/12) + Balance;
For 3 months the calculations you repeat the statement above 3 times.
Account.java
public class Account {
private double balance, interestRate;
public Account()
{
this.balance = 0;
this.interestRate = 0;
}
public Account(double balance, double interestRate) {
this.balance = balance;
this.interestRate = interestRate;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public double getInterestRate() {
return interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
public void deposit(double amt)
{
this.balance += amt;
}
public void withdraw(double amt)
{
if(amt >= this.balance)
System.out.println("Withdrawal not possible due to insufficient
funds!");
else
this.balance -= amt;
}
public void addInterest(int months)
{
for(int i = 0; i < months; i++)
{
this.balance += (this.balance * (this.interestRate / 100) /
12);
}
}
@Override
public String toString()
{
return ("Interest rate: " + String.format("%.2f",
this.interestRate) + " %, "
+ "Balance: $" + String.format("%.2f", this.balance));
}
}
AccountTester.java (Main class)
public class AccountTester {
public static void main(String[]args)
{
Account account = new Account(1000, 5);
System.out.println("Account Details:\n----------------\n" +
account.toString() + "\n");
System.out.println("OPERATION: Deposit...");
account.deposit(3800);
System.out.println("Account Details:\n----------------\n" +
account.toString() + "\n");
System.out.println("OPERATION: Withdrawal...");
account.withdraw(5800);
System.out.println("Account Details:\n----------------\n" +
account.toString() + "\n");
System.out.println("OPERATION: Withdrawal...");
account.withdraw(1300);
System.out.println("Account Details:\n----------------\n" +
account.toString() + "\n");
System.out.println("OPERATION: Add Interest...");
account.addInterest(7);
System.out.println("Account Details:\n----------------\n" +
account.toString() + "\n");
}
}
****************************************************************** SCREENSHOT *********************************************************