Objectives Practice developing overriding methods. Requirements Modify InterestB
ID: 3561734 • Letter: O
Question
Objectives
Practice developing overriding methods.
Requirements
Modify InterestBearingAccount to use the minimum monthly balance for computing interest. Currently the balance at the end of the month is used. This modification can be carried out with these changes tointerestBearingAccount:
The file TestMinMonthlyBalance.java is a test program for all of this.
package cs2302.accounts;
/**
* This kind of account earns interest each month based
* on the ending balance in the account.
*
* Actually, the interest earned would be based on the minimum
* balance during the month.
* We won't deal with that now, but may return to the issue later.
*
* @author Ben Setzer
*
*/
public class InterestBearingAccount extends Account {
private double annualInterestRate;
public InterestBearingAccount(String accountId) {
super(accountId);
}
public InterestBearingAccount(Person owner, String accountId, double balance,
double annualInterestRate ) {
super(owner, accountId, balance);
setAnnualInterestRate(annualInterestRate);
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate) {
if(annualInterestRate >= 0) {
this.annualInterestRate = annualInterestRate;
} else {
// do nothing, but should throw an exception
}
}
/**
* Add the interest earned this month.
*/
public void addMonthlyInterest() {
deposit(getBalance()*annualInterestRate/12);
}
@Override
public void endOfMonthProcess() {
addMonthlyInterest();
}
}
Explanation / Answer
Bearing account class
class BearingAccount
{
private double accountBalance;
private double annualInterestRate;
private double lastAmountOfInterestEarned;
public BearingAccount(double balance, double interestRate)
{
accountBalance = balance;
annualInterestRate = interestRate;
lastAmountOfInterestEarned = 0.0;
}
public void withdraw(double withdrawAmount)
{
accountBalance -= withdrawAmount;
}
public void deposit(double depositAmount)
{
accountBalance += depositAmount;
}
public void addInterest()
{
// Get the monthly interest rate.
double monthlyInterestRate = annualInterestRate / 12;
// Calculate the last amount of interest earned.
lastAmountOfInterestEarned = monthlyInterestRate * accountBalance;
// Add the interest to the balance.
accountBalance += lastAmountOfInterestEarned;
}
public double getAccountBalance()
{
return accountBalance;
}
public double getAnnualInterestRate()
{
return annualInterestRate;
}
public double getLastAmountOfInterestEarned()
{
return lastAmountOfInterestEarned;
}
}
Main program
public static void main(String args[])
{
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Ask user to enter starting balance
System.out.print("How much money is in the account?: ");
double startingBalance = keyboard.nextDouble();
// Ask user for annual interest rate
System.out.print("Enter the annual interest rate:");
double annualInterestRate = keyboard.nextDouble();
// Create class
BearingAccountClass bearingAccountClass = new BearingAccountClass();
BearingAccount bearingAccount = bearingAccountClass.new BearingAccount(startingBalance, annualInterestRate);
// Ask how long account was opened
System.out.print("How long has the account been opened? ");
double months = keyboard.nextInt();
double montlyDeposit;
double monthlyWithdrawl;
double interestEarned = 0.0;
double totalDeposits = 0;
double totalWithdrawn = 0;
// For each month as user to enter information
for (int i = 1; i <= months; i++) {
// Get deposits for month
System.out.print("Enter amount deposited for month: " + i + ": ");
montlyDeposit = keyboard.nextDouble();
totalDeposits += montlyDeposit;
// Add deposits bearing account
bearingAccount.deposit(montlyDeposit);
// Get withdrawals for month
System.out.print("Enter amount withdrawn for " + i + ": ");
monthlyWithdrawl = keyboard.nextDouble();
totalWithdrawn += monthlyWithdrawl;
// Subtract the withdrawals
bearingAccount.withdraw(monthlyWithdrawl);
// Add the monthly interest
bearingAccount.addInterest();
// Accumulate the amount of interest earned.
interestEarned += bearingAccount.getLastAmountOfInterestEarned();
}
// close keyboard
keyboard.close();
// Create a DecimalFormat object for formatting output.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
// Display the totals and the balance.
System.out.println("Total deposited: $" + dollar.format(totalDeposits));
System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
System.out.println("Interest earned: $" + dollar.format(interestEarned));
System.out.println("Ending balance: $"
+ dollar.format(bearingAccount.getAccountBalance()));
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.