帐户类定义为为银行帐户建模。帐户具有属性帐号,余额,年利率和创建日期,以及存入和取出资金的方法。
现在创建两个子类来检查和保存帐户。支票账户有透支限额(比如1000美元,收取25美元的费用),但储蓄账户不能透支。
编写一个创建Account,SavingsAccount和CheckingAccount对象的测试程序,并调用它们的toString()方法。
以上是说明,以下是我的代码。我无法弄清楚如何将子类调用到主Account类中。我也想知道如何应用toString()方法,因为我也无法得到它。我还在我的代码中保留了大部分注释,我在尝试不同的想法。
/*
//Calls both subclasses to the main. As well as a few other variables.
SavingsAccount savings = new SavingsAccount();
CheckingAccount checking = new CheckingAccount();
Account account;
double balance = 0.0;
double withdrawal = 0.0;
double deposit = 0.0;
System.out.println(checking);
CheckingAccount.getwithdrawal(10.50);
System.out.println(savings);
SavingsAccount.deposit(5.0);
System.out.println(account);
}
}
*/
package account;
public class Assignment5 {
SavingsAccount savings = new SavingsAccount();
CheckingAccount checking = new CheckingAccount();
Account account;
public static void main (String[] args) {
Account account = new Account(1122, 20000);
/* System.out.print("After Creation, " );
Print (account.getBalance());
Account.setAnnualInterestRate(4.5);
System.out.print("After Withdrawal of $2,500, " );
account.withdraw(2500);
Print (account.getBalance());
System.out.print("After Deposit of $3,000, " );
account.deposit(3000);
Print (account.getBalance());
System.out.println("Balance is " + account.getBalance());
System.out.println("Monthly interest is " +
account.getMonthlyInterest());
System.out.println("This account was created at " +
account.getDateCreated()); */
}
// Extra Print Method
public static void Print (double x){
System.out.printf("The current balance is "+" $ "+"%4.2f"+"%n",x);
}
}
class Account {
private int id;
double balance;
private static double annualInterestRate;
private java.util.Date dateCreated;
public Account() {
dateCreated = new java.util.Date();
}
public Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
dateCreated = new java.util.Date();
}
public int getId() {
return this.id;
}
public double getBalance() {
return balance;
}
public static double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
id = newId;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public static void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
public double getMonthlyInterest() {
return balance * (annualInterestRate / 1200);
}
public java.util.Date getDateCreated() {
return dateCreated;
}
public void withdraw(double amount) {
balance -= amount;
}
public void deposit(double amount) {
balance += amount;
}
}
package account;
public class CheckingAccount extends Account {
double overDraft = -1000;
public void checking(double i) {
//initializes double variable balance as 0.0
double balance = 0.0;
if (balance - i < overDraft){
System.out.println("Failure: Can't overdraft more than $1,000. A $25 +"
+ "overdraft fee will be issued to your account. ");
balance = balance - 25; }
else
balance = balance - i;
}
}
package account;
public class SavingsAccount extends Account{
double overdraftLimit = 0;
public void withdraw (double w) {
if (balance - w < overdraftLimit)
System.out.println("Insufficient Funds");
else
balance = balance - w;
}
}
答案 0 :(得分:0)
我不确定你通过调用子类是什么意思,但是toString方法的签名是
public String toString(){
}
由于默认情况下Java中的所有内容都是虚拟的,因此只需在子类中使用该方法来覆盖它。根据我的猜测,似乎问题是你想要调用toString方法中的各种方法并返回所需的所有信息的完整字符串。
另一方面,要调用超类方法,即使子类在Java中重写它,语法也是
super.function()
答案 1 :(得分:0)
首先,您需要为储蓄帐户和支票帐户创建默认构造函数。然后你需要创建一个重载的构造函数(假设你将唯一的额外值传递给子类)
public savingsAccount() {
super();
}
//overloaded constructor
public savingsAccount(int newId, double newBalance, int anotherVariable ) {
super(newID, newBalance);
this.anotherVariable = anotherVariable; //this is unique to this specific class
}
对于toString(),您需要每个子类都有自己的。
String toString(){
String x = "Savings: " + totalSavings + ". And the fee's assessed are: " + fees";
return x;
}
这样您就可以拨打savingsAccount.toString();
来打印信息。
答案 2 :(得分:0)
您需要覆盖每个类的默认toString方法 例如,在Account类
中 @Override
public String toString()
{
String str = "Id: "+ getId() +
"\nBalance: " + getBalance()+
"\nAnnual Interest Rate: " + getAnnualInterestRate() +
"\n Monthly Interest: " + getMonthlyInterest() +
"\n Created on: " + getDateCreated();
return str;
}
然后,在主类中创建Account对象
public static void main(String[] args) {
// TODO Auto-generated method stub
Account account = new Account(1122, 20000);
System.out.println(account); // invoke toString method in Account class
}
结果将是: Id:1122 余额:20000.0 年利率:0.0 每月利息:0.0 创建于:2月18日星期四18:16:47太平洋标准时间2016
类似地,您还需要为子类重写toString方法。但是你需要使用super关键字来连接超类的toString:
public String toString()
{
String str=super.toString();// call superclass's toString()
str+="...";
return str;
}