我现在完全失败了。我正在尝试开发一个程序,该程序将显示两个储蓄帐户的Months
,Account #
和Balance
并更新Balance
,因为帐户的利息是累计的:< / p>
为了做到这一点,我必须设计四个单独的类。 SavingsAccount
,SavingsAccountDriver
,FlexibleSavingsAccount
和CDSavingsAccount
。 SavingsAccount
是FlexibleSavingsAccount
和CDSavingsAccount
的父类。 SavingsAccountDriver
是主类。
在SavingsAccount
我有setAnnualInterestRate()
中调用的方法SavingsAccountDriver
。此方法设置每个帐户的利率。我遇到的问题是将此值传递给扩展类FlexibleSavingsAccount
和CDSavingsAccount
,以便我可以通过添加每个帐户的利率来更新余额。如果有人可以帮我解决这个问题,我将不胜感激。
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;
}
}
答案 0 :(得分:0)
您可能希望将getInterest添加为抽象方法,并将SavingsAccount声明为抽象类。那么你将拥有编译器已知的方法来调用,你将被迫在子类中提供正确的实现
答案 1 :(得分:0)
如果您要将getInterest()
和SavingsAccount
声明为saver1
类型,则应在父类 - saver2
中声明SavingsAccount
方法。
您展示的方式,该方法仅适用于声明为FlexibleSavingsAccount
的类。
由于您将其声明为SavingsAccount
,因此您只能访问该类&#39;方法
子类可以访问父类的方法,但不能反过来。
答案 2 :(得分:-1)
您已将实例化的储蓄帐户声明为SavingsAccount
类型。他们无法访问getInterest()
方法,因为它位于子级类FlexibleSavingsAccount
中。
您需要将它们实例化为您希望它们实际存在的实际储蓄帐户:
FlexibleSavingsAccount saver1 = new FlexibleSavingsAccount(10002, 2000); //create new FlexibleSavingsAccount object
现在saver1
将能够访问getInterest()
。
额外的想法:
将父SavingsAccount
编码为接口可能更好。然后,您将在此界面中声明您的getInterest()
方法,同时将方法内容的详细信息留给您的子类。
SavingsAccount:
public interface SavingsAccount {
public int getAccountNumber();
public double getBalance();
public void setAnnualInterestRate (double interestRate);
}
然后实例化您的帐户:
SavingsAccount saver1 = new FlexibleSavingsAccount(10002, 2000); //create new FlexibleSavingsAccount object
请注意,这有一个额外的好处,即您将实例编码为SavingsAccount
接口,这对于将来验证代码总是一个好主意。