У меня есть класс под названием «Учетная запись»:
import java.util.Date;
public class Account {
public int id = 0; //Declare default id as 0
public double balance = 0; //Declare default balance as 0
public double annualInterestRate = 0; //Declare default annual interest rate as 0
public Date dateCreated = new Date(); //Declare date
//No argument constructor for Account
public Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//Constructor that accepts ID, Balance, and Annual Interest Rate
public Account(int newID, double newBalance, double newAnnualInterestRate) {
id = newID;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//Get ID
public int getId() {
return id;
}
//Set ID
public void setId(int id) {
this.id = id;
}
//Get Balance
public double getBalance() {
return balance;
}
//Set Balance
public void setBalance(double balance) {
this.balance = balance;
}
//Get Annual Interest Rate
public double getAnnualInterestRate() {
return annualInterestRate;
}
//Set Annual Interest Rate
public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}
//Get Date Created
public Date getDateCreated() {
return dateCreated;
}
//Withdraw method
double withdraw(double amount) {
return balance -= amount;
}
//Deposit method
double deposit(double amount) {
return balance += amount;
}
//Interest rate method
double getMonthlyInterestRate() {
return (balance * annualInterestRate) / 12;
}
} //End Account class
Затем я создал два разных подкласса «PreferredCustomer» и «CommercialCustomer». Эти два класса должны наследовать все методы (депозит, вывод, ежемесячная процентная ставка, а также все геттеры и сеттеры) основного класса «Учетная запись». Единственная разница с подклассами заключается в том, что они имеют заранее определенную процентную ставку.
public class PreferredCustomer extends Account {
public double annualInterestRate;
public PreferredCustomer() {
}
public PreferredCustomer(int id, double balance) {
super();
this.annualInterestRate = .04;
}
} //end PreferredCustomer Class
У меня есть ощущение, что то, как я сейчас настроил его, не является точным. При тестировании методы вывода и депозита работают, но, несмотря на ввод начального баланса в 20 000 долларов, он по-прежнему устанавливает начальный баланс в 0 долларов и не рассчитывает процентную ставку.
Я тестирую класс как таковой:
public class TestingAccountClass {
public static void main(String[] args) {
//Create accounts
CommercialCustomer myCommercialCustomerAccount = new CommercialCustomer(1124,
20000.00);
//Invoking deposit method from account class
myCommercialCustomerAccount.deposit(3000.00);
//Display account balance, monthly interest, and date created
System.out.println("\n\n----Commercial Account---");
System.out.println("Account Created On: "
+ myCommercialCustomerAccount.getDateCreated());
System.out.printf("Balance: $%.2f", myCommercialCustomerAccount.getBalance());
System.out.printf("\nMonthly Interest: $%.2f"
,myCommercialCustomerAccount.getMonthlyInterestRate());
При тестировании класса таким образом метод депозита работает, но ничего из класса аккаунта (кроме вывода) не работает. Любой совет будет принят во внимание. Спасибо!
PreferredCustomer
не должен определятьannualInterestRate
— он наследуетannualInterestRate
Пользователя. Кроме того,PreferredCustomer
должен звонитьsuper(id, balance, .04)
- person Zim-Zam O'Pootertoot   schedule 24.04.2013