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