简体   繁体   中英

Passing Variables to Extended Class and Calling Methods

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;  
        }
}

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM