Question: Hey, I have a problem with my code and here is what is wrong... CODE: public abstract class BankAccount { protected double balance; protected int
Hey, I have a problem with my code and here is what is wrong... CODE:
public abstract class BankAccount
{
protected double balance;
protected int numDeposits;
protected int numWithdrawals;
protected double interestRate;
protected double monthlyServiceCharge;
public BankAccount(double bal, double intRate, double mon)
{
balance = bal;
interestRate = intRate;
monthlyServiceCharge = mon;
}
public void deposit(double amount)
{
balance += amount;
numDeposits++;
}
public void withdraw(double amount) {
balance -= amount;
numWithdrawals++;
}
protected void calcInterest()
{
double monthlyInterestRate = interestRate / 12;
double monthlyInterest = balance * monthlyInterestRate;
balance += monthlyInterest;
}
public void monthlyProcess()
{
balance -= monthlyServiceCharge;
calcInterest();
numDeposits = 0;
numWithdrawals = 0;
}
public void setMonthlyServiceCharges(double amount)
{
this.monthlyServiceCharge += amount;
}
public double getBalance()
{
return balance;
}
public int getNumDeposits()
{
return numDeposits;
}
public int getNumWithdrawals()
{
return numWithdrawals;
}
public double getInterestRate()
{
return interestRate;
}
public double getMonthlyServiceCharges()
{
return monthlyServiceCharge;
}
}
public class SavingsAccount extends BankAccount
{
public SavingsAccount(double bal, double intRate, double mon)
{
super(bal, intRate, mon);
}
public boolean isActive()
{
return balance >= 50;
}
public void withdraw(double amount)
{
if (isActive() == false)
{
throw new IllegalStateException("This account is inactive.");
}
super.withdraw(amount);
}
public void monthlyProcess()
{
int costlyWithdrawals = numWithdrawals - 5;
if (costlyWithdrawals > 0)
{
monthlyServiceCharge += costlyWithdrawals * 0.5;
}
super.monthlyProcess();
}
}
import java.text.DecimalFormat;
public class SavingsDemo
{
public static void main(String[] args)
{
DecimalFormat dollar = new DecimalFormat("#,##0.00");
SavingsAccount savings =
new SavingsAccount(100.0, 0.03, 2.50);
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
savings.deposit(25.00);
savings.deposit(10.00);
savings.deposit(35.00);
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
savings.withdraw(100.00);
savings.withdraw(50.00);
savings.withdraw(10.00);
savings.withdraw(1.00);
savings.withdraw(1.00);
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
System.out.println();
savings.monthlyProcess();
System.out.println("Balance: $" +
dollar.format(savings.getBalance()));
System.out.println("Number of deposits: " +
savings.getNumDeposits());
System.out.println("Number of withdrawals: " +
savings.getNumWithdrawals());
}
}
CURRENT OUTPUT:
Balance: $100.00 Number of deposits: 0 Number of withdrawals: 0
Balance: $170.00 Number of deposits: 3 Number of withdrawals: 0
DESIRED OUTPUT:
Balance: $100.00
Number of deposits: 0
Number of withdrawals: 0
Balance: $170.00
Number of deposits: 3
Number of withdrawals: 0
Balance: $20.00
Number of deposits: 3
Number of withdrawals: 2
Balance: $17.54
Number of deposits: 0
Number of withdrawals: 0
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
