My flexiblesavingsaccount is supposed to add 1.2% interest each year and it compounds monthly. CD Savings account on the other hand adds interest at 4% per year and compounds quarterly. I've managed to get the program to add interest to the balance the first month, but after that it just stays the same. and on month zero, it says the balance is 0 when it needs to be 2000 for flexiblesavingsaccount and 3000 for CDSavingsaccount. Once the program goes through 12 months it is supposed to add up the balance of the two accounts. that part works i believe, it is just that the balance I get right now it wrong
Here is my driver class: import java.util.*;
public class SavingsAccountDriver1{
public static void main(String[] args){
FlexibleSavingsAccount1 saver1 = new FlexibleSavingsAccount1(10002,2000); //could be FlexibleSavingsAccount saver1
CDSavingsAccount1 saver2 = new CDSavingsAccount1(10003,3000);//CD SavingsAccount saver 2
saver1.annualInterestRate=.012;
saver2.annualInterestRate = .04;
int i;
System.out.println("Month\tAccount # Balance\tAccount # Balance");
System.out.println("-----\t--------- -------\t--------- -------");
System.out.print(" 0");
printFlex(saver1);
printCD(saver2);
System.out.println();
for(i=1;i<=12;i++)
{System.out.printf("%5d",i);
printIntFlex(saver1);
printIntCD(saver2);
System.out.println();
}
System.out.printf("Final balance of both accounts combined: %.2f\n",(saver1.getMonthlyInterest()+saver2.getMonthlyInterest()));
}
public static void printIntFlex(FlexibleSavingsAccount1 f){
getIntFlex(f);
printFlex(f);
}
public static void printIntCD(CDSavingsAccount1 c){
getIntCD(c);
printCD(c);
}
public static void printFlex(FlexibleSavingsAccount1 f){
System.out.printf("%12d%10.2f ",f.getAccount_Number(),f.getMonthlyInterest());
}
public static void printCD(CDSavingsAccount1 c){
System.out.printf("%12d%10.2f ",c.getAccount_Number(),c.getMonthlyInterest());
}
public static void getIntFlex(FlexibleSavingsAccount1 f){
f.addMonthlyInterest(f.getBalance());
}
public static void getIntCD(CDSavingsAccount1 c){
c.addMonthlyInterest(c.getBalance());
}
}
here is my SavingsAccount superclass: public abstract class SavingsAccount1 { private double balance; private final int ACCOUNT_NUMBER;
public SavingsAccount1(){
this.balance = 0.0;
this.ACCOUNT_NUMBER =0;
}
public SavingsAccount1(int ACCOUNT_NUMBER, double balance)
{
this.balance = balance;
this.ACCOUNT_NUMBER = ACCOUNT_NUMBER;
}
public abstract void addMonthlyInterest(double balance);
public double getBalance()
{ return balance;
}
public void setBalance(double balance){
this.balance = balance;
}
public int getAccount_Number()
{
return ACCOUNT_NUMBER;
}
public String toString(){
return String.format("%s", getAccount_Number());
}//end of toString
}
Here is my FlexibleSavingsAccount subsclass:
public class FlexibleSavingsAccount1 extends SavingsAccount1{
public static double annualInterestRate;
public static double b;
public FlexibleSavingsAccount1 (int ACCOUNT_NUMBER, double balance){
super(ACCOUNT_NUMBER, balance);
}//end of
@Override public void addMonthlyInterest(double balance){
b =balance +(balance * (annualInterestRate / 12));
this.b = b;
}
public double getMonthlyInterest(){
return b;
}//end of getMonthlyInterest
public String toString(){
return String.format("%s %s", super.toString(), getMonthlyInterest());
}//end of toString
}//end of FlexibleSavings Account
Here is my CDSavingsAccount subclass:
public class CDSavingsAccount1 extends SavingsAccount1{
public static double annualInterestRate;
public static double b;
public CDSavingsAccount1 (int ACCOUNT_NUMBER, double balance){
super(ACCOUNT_NUMBER, balance);
}//end of
@Override public void addMonthlyInterest(double balance){
b =balance +(balance * (annualInterestRate / 4));
this.b = b;
}
public double getMonthlyInterest(){
return b;
}//end of getMonthlyInterest
public String toString(){
return String.format("%s %s", super.toString(), getMonthlyInterest());
}//end of toString
}//end of CDSavings Account
Sorry for all the code, I've never messed with polymorphism or inheritance before and my teacher is really bad. I assume that is where my issue is. Any help you could give would be awesome.