This is a plate, requirement for our semifinals.

Enhance the addInterest method of the SavingsAccount class to compute the interest on the minimum
balance since the last call to addInterest.
Hint: You need to modify the withdraw method as well, and you need to add an instance field to
remember the minimum balance.

AccountTester.java

public class AccountTester{
public static void main(String[] args){
SavingsAccount momsSavings = new SavingsAccount(0.5);
CheckingAccount harrysChecking = new CheckingAccount(100);
momsSavings.deposit(10000);
momsSavings.transfer(2000,harrysChecking);
harrysChecking.withdraw(1500);
harrysChecking.withdraw(80);
momsSavings.transfer(1000,harrysChecking);
harrysChecking.withdraw(400);
// Simulate end of month
momsSavings.addInterest();
harrysChecking.deductFees();
System.out.println("Mom's savings balance: " + momsSavings.getBalance());
System.out.println("Expected: 7035");
System.out.println("Harry's checking balance: " + harrysChecking.getBalance());
System.out.println("Expected: 1116");
}
}

BankAccount.java

//A bank account has a balance that can be changed by deposits and withdrawals.
public class BankAccount{
private double balance;
//Constructs a bank account with a zero balance.
public BankAccount(){
balance = 0;
}
//Constructs a bank account with a given balance.
//@param initialBalance the initial balance
public BankAccount(double initialBalance){
balance = initialBalance;
}
//Deposits money into the bank account.
//@param amount the amount to deposit
public void deposit(double amount){
balance = balance + amount;
}
//Withdraws money from the bank account.
//@param amount the amount to withdraw
public void withdraw(double amount){
balance = balance - amount;
}
//Gets the current balance of the bank account.
//@return the current balance
public double getBalance()
{
return balance;
}
//Transfers money from the bank account to another account.
//@param amount the amount to transfer
//@param other the other account
public void transfer(double amount, BankAccount other){
withdraw(amount);
other.deposit(amount);
}
}

CheckingAccount.java

//A checking account that charges transaction fees.
public class CheckingAccount extends BankAccount{
private int transactionCount;
private static final int FREE_TRANSACTIONS = 3;
private static final double TRANSACTION_FEE = 2.0;
//Constructs a checking account with a given balance.
//@param initialBalance the initial balance
public CheckingAccount(double initialBalance){
// Construct superclass
super(initialBalance);
// Initialize transaction count
transactionCount = 0;
}
public void deposit(double amount){
transactionCount++;
// Now add amount to balance
super.deposit(amount);
}
public void withdraw(double amount){
transactionCount++;
//Now subtract amount from balance
super.withdraw(amount);
}
//Deducts the accumulated fees and resets the transaction count.
public void deductFees(){
if (transactionCount > FREE_TRANSACTIONS){
double fees = TRANSACTION_FEE * (transactionCount - FREE_TRANSACTIONS);
super.withdraw(fees);
}
transactionCount = 0
}
}

SavingsAccount.java

//An account that earns interest at a fixed rate.
public class SavingsAccount extends BankAccount{
private double interestRate;
//Constructs a bank account with a given interest rate.
//@param rate the interest rate
public SavingsAccount(double rate){
interestRate = rate;
}
//Adds the earned interest to the account balance.
public void addInterest(){
double interest = getBalance() *interestRate / 100;
deposit(interest);
}
}

Recommended Answers

All 3 Replies

I ... don't really see a question in there? did you forget to post it, or is this just a snippet you want to share with the community?

I ... don't really see a question in there? did you forget to post it, or is this just a snippet you want to share with the community?

I dunno how to modify the SavingsAccount -__-

well ... put your amount (or so) as an instance variable, and add setters and getters to obtain or change the value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.