0

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.

cding001
  • 7
  • 3
  • Any way we can get you to tidy up your code a bit so that it's easier to follow? Also, you're using an odd mixture of double and float to do things... try to stick with BigDecimal for monetary transactions so that you don't lose precision. Some of your problems may be due to mixing and matching of data types. – MarsAtomic Oct 27 '14 at 20:25
  • I just tried to fix it, if its still hard to follow I'm sorry I'm not entirely sure what im doing – cding001 Oct 27 '14 at 21:13
  • Don't worry, I already resolved your problems, I'm just editing an answer. Hopefully you will understand and it will be helpful. – Iootu Oct 27 '14 at 21:23
  • Don't thank me yet, this is gonna take a while, I guess... – Iootu Oct 27 '14 at 21:42
  • Well i believe in you! Plus hopefully, i don't think i was too far off, i think something is wrong with the balance variable/method or the addMonthlyInterest method – cding001 Oct 27 '14 at 21:57
  • Yes, you weren't. However, I will be trying to explain the inheritance subject and the problems you had. So the explain is kinda long. – Iootu Oct 27 '14 at 22:23
  • Ok cool, that would be really helpful! – cding001 Oct 27 '14 at 22:36
  • Did you ever figure out an answer? – cding001 Oct 28 '14 at 00:03

1 Answers1

0

InSavingsAccount1 change the constructor to allow an annualInterestRate parameter to be given. Create a getter for it and it's done.

Regarding theFlexibleSavingsAccount1class (Due to the similarities with theCDSavingsAccount1class, everything I will mention applies to both classes):

When you extend a class you inherit all theattributes and methodsof the parent class so there is no need to defineannualInterestRateattribute in the child class. You see, this is precisely why we use inheritance. 1- is so we don't repite ourselves, and keep typing the same code more than once. 2- it allows us to force a common signature/behaviour. A class that extends from an abstract class must either provide an implementation to the abstract methods or must itself be abstract, adding one more layer of abstraction.

This variable b was supossely to hold the value of the monthlyInterest but you seem never to actually use it in your design. ps: If you were to keep it, you would have to rename it to something more meaningful then justb.

So, you should eliminate both of the attributes because you never use them.

In theaddMonthlyInterestmethod you are calculating the value of the monthlyInterest and assigning to b and then you are assigning b to itself. This is obviously wrong, you need to change the actual balance of the account.

public void addMonthlyInterest(double balance) {
  setBalance(balance + (balance * (annualInterestRate / 12)));
}

Pretty self-explanatory, I set a new value for the balance which is the result of the sum of the monthlyInterest with the current balance.

    FlexibleSavingsAccount1 saver1 = new FlexibleSavingsAccount1(10002, 2000, .012);
    CDSavingsAccount1 saver2 = new CDSavingsAccount1(10003, 3000, .04);                                                                     
    System.out.println("Month\tAccount #   Balance\tAccount #   Balance");
    System.out.println("-----\t---------   -------\t---------   -------");
    System.out.print("    0");
    print(saver1);
    print(saver2);
    System.out.println();
    for (int i = 1; i <= 12; i++) {
        System.out.printf("%5d", i);
        printInt(saver1);
        printInt(saver2);
        System.out.println();
    }
    System.out.printf("Final balance of both accounts combined: %.2f\n",
            (saver1.getBalance() + saver2.getBalance()));
}

Both FlexibleSavingsAccount1 and CDSavingsAccount1 are a type of SavingsAccount1 you and so you can eliminate several duplicated methods that you had by changing the method signature to accept a SavingsAccount1 as argument instead.

public static void printInt(SavingsAccount1 f) {
    getInt(f);
    print(f);
}
public static void print(SavingsAccount1 f) {
    System.out.printf("%12d%10.2f ", f.getAccount_Number(),
            f.getBalance());
}
public static void getInt(SavingsAccount1 f) {
    f.addMonthlyInterest(f.getBalance());
}

The methodgetInt(SavingsAccoutn1 f) is a wonderful example what polymorphism is all about. You will now call this method using both child objects and both of them will addMonthlyInterest according to their own unique implementation defined in their classes. That is the definition of polymorphism: two related but different objects getting the same order but acting upon it differently.

You should rename this methods to something more explanatory.

Finally, a word about your class naming convention, namely the fact that all your classes end with a 1 attached, such as SavingsAccount1 to it's end makes me think that you not fully grasped the concept of a class. A class is a blueprint, it enables us to create many objects of the same type, as such it's name shouldn't seem to be "tied" to any particular instance/object.

NOTE: I agree with MarsAtomic in that you should stick with BigDecimal class for monetary transactions in order to not lose precision but as you state this is some kinda of assignment, I will take in assumption that the BigDecimal is beyond the scope of what your teacher taught you so I just used double.

Community
  • 1
  • 1
Iootu
  • 344
  • 1
  • 6