2

I am at a complete loss right now. I am trying to develop a program that will display the Months, Account #, and Balance of two savings accounts and update the Balance as interest on the accounts is accrued:

  • For the first account 10,002 interest is accrued monthly with a yearly interest rate of 1.2%.
  • For the second account 10,003 interest is accrued quarterly with a yearly interest rat of 4%.

I have to design four individual classes in order to do this. SavingsAccount, SavingsAccountDriver, FlexibleSavingsAccount, and CDSavingsAccount. SavingsAccount is the parent class of both FlexibleSavingsAccount and CDSavingsAccount. SavingsAccountDriver is the Main class.

In SavingsAccount I have a method setAnnualInterestRate() that is called in SavingsAccountDriver. This method sets the interest rate for each account. The problem I am having is passing this value to the extended classes FlexibleSavingsAccount and CDSavingsAccount so that I can update the balance by adding the interest rate for each account. If anyone could please assist me on how this is done I would greatly appreciate it.

SavingsAccountDriver:

public class SavingsAccountDriver {
    public static void main (String[] args) {
        SavingsAccount saver1 = new SavingsAccount(10002, 2000); //create new SavingsAccount object
        SavingsAccount saver2 = new SavingsAccount(10003, 3000); //create new SavingsAccount object

        saver1.setAnnualInterestRate(.012); //sets AnnualInterestRate for 'saver1' object
        saver2.setAnnualInterestRate(.04); //sets AnnualInterestRate for 'saver2' object

        System.out.println("\nMonthly balances:\n");
        System.out.println("Month " + " Account# " + " Balance " + "      " + " Month " + " Account# " + " Balance ");
        System.out.println("----- " + " -------- " + " ------- " + "      " + " ----- " + " -------- " + " ------- ");


        System.out.println(saver1.getAccountNumber() + " / " + saver1.getBalance() + " / " + saver1.getInterest());
        System.out.println(saver2.getAccountNumber() + " / " + saver2.getBalance() + " / " + saver2.getInterest());

        /*for(int month = 0; month <= 12; month++) {
            switch(month) { // switch that outputs month, account number, and balance for both accounts (Some non-needed cases used to make output look cleaner)
                case 0:
                    System.out.println(month + "      " + saver1.getAccountNumber() + "     " + saver1.getBalance() + "         " + month + "      " + saver2.getAccountNumber() + "     " + saver2.getBalance());
                    break;
                case 4: 
                    saver1.addInterest();
                    //saver2.addInterest();

                    System.out.println(month + "      " + saver1.getAccountNumber() + "     " + saver1.getBalance() + "         " + month + "      " + saver2.getAccountNumber() + "     " + saver2.getBalance());
                    break;
                case 10:
                    saver1.addInterest();
                    //saver2.addInterest();

                    System.out.println(month + "     " + saver1.getAccountNumber() + "     " + saver1.getBalance() + "        " + month + "     " + saver2.getAccountNumber() + "     " + saver2.getBalance());
                    break;
                case 11:
                    saver1.addInterest();
                    //saver2.addInterest();

                    System.out.println(month + "     " + saver1.getAccountNumber() + "     " + saver1.getBalance() + "        " + month + "     " + saver2.getAccountNumber() + "     " + saver2.getBalance());
                    break;
                case 12:
                    saver1.addInterest();
                    //saver2.addInterest();

                    double totalBalance = saver1.getBalance() + saver2.getBalance();
                    System.out.println(month + "     " + saver1.getAccountNumber() + "     " + saver1.getBalance() + "        " + month + "     " + saver2.getAccountNumber() + "     " + saver2.getBalance());
                    break;
                default:
                    saver1.addInterest();
                    //saver2.addInterest();

                    System.out.println(month + "      " + saver1.getAccountNumber() + "     " + saver1.getBalance() + "        " + month + "      " + saver2.getAccountNumber() + "     " + saver2.getBalance());
                    break;
            }
        }*/
    }
}

SavingsAccount:

public class SavingsAccount {
    // variables specific to SavingsAccount class
    public double annualInterestRate;
    private final int ACCOUNT_NUMBER;
    public double balance;

    //constructor with account number and balance parameters
    public SavingsAccount(int account_number, double balance) {
        this.ACCOUNT_NUMBER = account_number;
        this.balance = balance;
    }

    //returns account number
    public int getAccountNumber() {
        return this.ACCOUNT_NUMBER;
    }

    //returns balance
    public double getBalance() {
        return this.balance;
    }

    //sets interest rate
    public void setAnnualInterestRate (double interestRate) {
        this.annualInterestRate = interestRate;
    }
}

FlexibleSavingsAccount:

public class FlexibleSavingsAccount extends SavingsAccount{

        public FlexibleSavingsAccount(int account_number, double balance) {
            super(account_number, balance);
        }

        //returns interest
        public double getInterest() {
            return annualInterestRate;  
        }
}
t0mppa
  • 3,983
  • 5
  • 37
  • 48
Brandon
  • 49
  • 1
  • 11
  • I don't think I understand. By extending a class, you should have access to the members of the parent class. Are you getting an error? – Jonathon Anderson Sep 03 '14 at 19:23
  • Yes. If you look at the last little bit of code i posted the getInterest() method should return the annualInterestRate of an object. In this case I have two objects of type SavingsAccount ('saver1' and 'saver2'). When I run the code I get the following error: cannot find symbol (referring to the section in the Main class saver1.getInterest() and saver2.getInterest(). – Brandon Sep 03 '14 at 19:25
  • 1
    You won't be able to run `getInterest()` from either of your `SavingsAccount` objects. `SavingsAccount` doesn't have `getInterest()` defined. If you want to use that method you have to initialize the object as `FlexibleSavingsAccount` or `CDSavingsAccount` – Jonathon Anderson Sep 03 '14 at 19:26
  • Why is this? Do I have to declare two more objects of type FlexibleSavingsAccount? I thought I would be able to use the SavingsAccount objects since the other classes are extended from this class. – Brandon Sep 03 '14 at 19:28
  • I think you should read this http://docs.oracle.com/javase/tutorial/java/IandI/subclasses.html – Jonathon Anderson Sep 03 '14 at 19:30
  • you can use reference of SavingsAccount to point them. they will have their own implementation of calculating interest. – Adi Sep 03 '14 at 19:30
  • 3
    By declaring it as a `SavingsAccount`, all you (the compiler) know is that it is at least a `SavingsAccount` and `SavingsAccount` does not have a `getInterest()` method. It can't make any other assumptions. – Sotirios Delimanolis Sep 03 '14 at 19:30
  • `getInterest()` should be defined in `SavingsAccount` – GriffeyDog Sep 03 '14 at 19:31
  • may be [this](http://stackoverflow.com/questions/25651066/why-use-type-substitution/25651192?noredirect=1#comment40082048_25651192) will help you. – Adi Sep 03 '14 at 19:31
  • Ok thank you all for the info. I will try it. I am new to Java if you cant tell haha. Thanks again. – Brandon Sep 03 '14 at 19:32
  • You can declare getInterest() in SavingsAccount class, and then override it in the other classes – vgarzom Sep 03 '14 at 19:57

3 Answers3

0

You may want to add getInterest as an abstract method and declare SavingsAccount as abstract class. then you will have the method known by the compiler to call and you will be forced in the subclasses to provide the proper implementation

LhasaDad
  • 1,786
  • 1
  • 12
  • 19
-1

Your getInterest() method should be declared in your parent class - SavingsAccount- if you want to declare saver1 and saver2 as being of type SavingsAccount.

The way that you show, the method will only be available to classes declared as FlexibleSavingsAccount.

Since you declared them as SavingsAccount you only have access to that class' methods.

Child classes can access the methods of the parent but not the other way around.

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
LuisF
  • 564
  • 1
  • 7
  • 18
-1

You have declared your instantiated savings accounts as type SavingsAccount. They will not have access to the method getInterest() since it is in the child class FlexibleSavingsAccount.

You need to instantiate them as the actual savings account that you wish them to actually be:

FlexibleSavingsAccount saver1 = new FlexibleSavingsAccount(10002, 2000); //create new FlexibleSavingsAccount object

Now saver1 will be able to access getInterest().

Extra idea:

What might be nicer is to code the parent SavingsAccount as an interface. You would then declare your getInterest() method in this interface, whilst leaving the details of what goes in the method to your children classes.

SavingsAccount:

public interface SavingsAccount {
    public int getAccountNumber();
    public double getBalance();
    public void setAnnualInterestRate (double interestRate);
 }

Then instantiate your accounts:

SavingsAccount saver1 = new FlexibleSavingsAccount(10002, 2000); //create new FlexibleSavingsAccount object

Note this has the added benefit that you declare your instances coded to the SavingsAccount interface which is always a nice idea for future proofing your code.

Mega Bean
  • 11
  • 3
  • NOt sure what you mean by future proofing the code? making an interface here looses the shared behavior that can be contained in a base class and forces the author to write the code twice with twice the maintenance if it needs to change and 2 times to enter something incorrectly? – LhasaDad Sep 03 '14 at 22:32
  • Yep true an abstract base class instead of interface would handle this. I would probably do both an interface and an abstract base class. – Mega Bean Sep 04 '14 at 11:54