0

I could use assistance in figuring out why when I run my SpecialSavingsDriver, my interest and deposits numbers are off in the output:

Saver 1
New Monthly Savings Balance After Interest: 27958.33
Saver 2
New Monthly Savings Balance After Interest: 4933.33
Saver 1
New Monthly Savings Balance After Interest: 49240.28
Saver 2
New Monthly Savings Balance After Interest: 6431.11

I do have it adding interest correctly in my SavingsAccountClass. My withdraw method in the SpecialSavings class does not seem to work either. Any assistance would be greatly appreaciated. Below is the outline of what I am try to accomplish:

Part 1
Create a class SavingsAccount. Use a static class variable to store the annualInterestRate for each of the savers. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit. Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the balance by annualInterestRate divided by 12; this interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a driver program to test the class SavingsAccount. Instantiate two different savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for each of the savers. Then set the annualInterestRate to 5% and calculate the next months interest and print the new balances for each of the savers.

Part 2
Write another class SpecialSavings that extends SavingsAccount to pay interest of 10% on accounts that have balances that exceed 10K. Also provided methods to deposit and take money out of savings account. Write a driver program to test the class SpecialSavings. Instantiate two different savingsAccount objects, saver1 and saver2, with balances of $2000.00 and $3000.00, respectively. Make a few deposits and withdrawals and show balance and interest earned for each account.

Thank You Again!

public class SavingsAccountDriver {

    public static void main(String[] args) {
        SavingsAccount saver1 = new SavingsAccount();
        SavingsAccount saver2 = new SavingsAccount();
        
        saver1.setsavingsBalance(2000);
        saver2.setsavingsBalance(3000);
        
        SavingsAccount.modifyInterestRate(0.04);
        
        saver1.calculateMonthlyInterest();
        saver2.calculateMonthlyInterest();
        
        System.out.printf("\nSaver 1\nNew Monthly Savings Balance After Interest: %.2f",saver1.getsavingsBalance());
        System.out.printf("\nSaver 2\nNew Monthly Savings Balance After Interest: %.2f",saver2.getsavingsBalance());
        
        SavingsAccount.modifyInterestRate(0.05);
        
        saver1.calculateMonthlyInterest();
        saver2.calculateMonthlyInterest();

        System.out.printf("\nSaver 1\nNew Monthly Savings Balance After Interest: %.2f",saver1.getsavingsBalance());
        System.out.printf("\nSaver 2\nNew Monthly Savings Balance After Interest: %.2f",saver2.getsavingsBalance());
    }
public class SavingsAccount {
    
    public static double annualInterestRate;
    public double savingsBalance;
    
    /*
     * Constructor
     * */
    public SavingsAccount() {
        annualInterestRate = 0;
        savingsBalance = 0;
    }
    /*
     * Overloaded
     * */
    public SavingsAccount(double savingsBalance) {
        this.savingsBalance = savingsBalance;
    }
    /*
     * Calculates Monthly Interest from savings balance and interest rate
     * */
    public void calculateMonthlyInterest() {
        savingsBalance += savingsBalance*annualInterestRate/12;
    }
    /*
     * Interest Rate modifier
     * */
    public static void modifyInterestRate(double newInterestRate) {
        annualInterestRate = newInterestRate;
    }
    /*
     * Returns new Savings Balance
     * */
    public double getsavingsBalance() {
        return savingsBalance;
    }   
    /*
     * Sets new savings balance after interest
     * */
    public void setsavingsBalance(double savingsBalance) {
        this.savingsBalance = savingsBalance;
    }

    /*
     * Returns Interest Rate
     * */
    public double getannualInterestRate() {
        return annualInterestRate;
    }
}
public class SpecialSavings extends SavingsAccount {
    
    public SpecialSavings() {
        annualInterestRate = 0;
        savingsBalance = 0;
    }   
    
    public SpecialSavings(double savingsBalance) {
        super(savingsBalance);
    }
    
    public void calculateMonthlyInterest() {
        if(getsavingsBalance()>10000) {
            modifyInterestRate(10);
        }else {
            modifyInterestRate(4);
        }
        super.calculateMonthlyInterest();
    }
    
    public void deposit(double amount) {
        savingsBalance+=amount;
    }
    
    public void withdraw(double amount) {
        savingsBalance-=amount;
    }
    
}
public class SpecialSavingsDriver {

    public static void main(String[] args) {
        SpecialSavings saver1 = new SpecialSavings();
        SpecialSavings saver2 = new SpecialSavings();
        
        saver1.setsavingsBalance(2000);
        saver2.setsavingsBalance(3000);
        
        SavingsAccount.modifyInterestRate(0.04);
        
        saver1.deposit(13000);
        saver1.deposit(250);
        saver2.deposit(350);
        saver2.deposit(350);
        
        saver1.calculateMonthlyInterest();
        saver2.calculateMonthlyInterest();
        
        System.out.printf("\nSaver 1\nNew Monthly Savings Balance After Interest: %.2f",saver1.getsavingsBalance());
        System.out.printf("\nSaver 2\nNew Monthly Savings Balance After Interest: %.2f",saver2.getsavingsBalance());
        
        saver1.withdraw(1000);
        saver1.withdraw(100);
        saver2.withdraw(100);
        saver2.withdraw(10);
        
        saver1.calculateMonthlyInterest();
        saver2.calculateMonthlyInterest();
        
        System.out.printf("\nSaver 1\nNew Monthly Savings Balance After Interest: %.2f",saver1.getsavingsBalance());
        System.out.printf("\nSaver 2\nNew Monthly Savings Balance After Interest: %.2f",saver2.getsavingsBalance());

    }

}
Andreas
  • 154,647
  • 11
  • 152
  • 247
Bowtiez
  • 9
  • 2
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas May 26 '21 at 04:56
  • [How to create a **Minimal**, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Andreas May 26 '21 at 04:57

1 Answers1

0

I feel issue is with this line.

savingsBalance*annualInterestRate/12;

It is going to execute like this:-

(savingsBalance*annualInterestRate)/12;
= (multipliedValue)/12;
= result

I hope you dont want this, to fix this you should use brackets

savingsBalance*(annualInterestRate/12);