输出未按预期显示

时间:2014-07-28 05:27:53

标签: java inheritance override tostring

经过一段时间的努力但终于克服了一些编译错误,我终于能够让我的程序编译并运行了。但是,我没有得到任何接近我应该得到的输出。

程序应该使用Account类和2个子类(SavingsAccount和CheckingAccount)以及几种显示帐户历史记录类型的方法。

以下是我到目前为止编写的代码:

import java.util.Date;

public class Account {
    private int id = 0;
    private double balance = 0;
    private double annualInterestRate = 0;
    private Date dateCreated = new Date();

    public Account() {
      id = 0;
      balance = 0.0;
      annualInterestRate = 0.0;
    } 

    public Account(int id, double balance) {
      this.id = id;
      this.balance = balance;
    }

    public int getId() {
      return id;
    }

    public double getBalance() {
      return balance;
    }

    public double getAnnualInterestRate() {
      return annualInterestRate;
    }

    public void setId(int newId) {
      this.id = id;
    }

    public void setBalance(double newBalance) {
      this.balance = balance;
    }

    public void setAnnualInterestRate(double newAnnualInterestRate) {
      this.annualInterestRate = annualInterestRate;
      this.balance = balance * annualInterestRate;
    }

    public java.util.Date getDateCreated() {
      return dateCreated;
    }

    public double getMonthlyInterestRate() {
      return annualInterestRate/12;
    }

    public double withdrawal (double withdrawalAmount) {
      return balance -= withdrawalAmount;
    }

    public double deposit (double depositAmount) {
      return balance += depositAmount;
    }

  public static void main(String[] args) {
    Account savingsAccount = new Account(1122, 20000);
    Account checkingAccount = new Account(1271, 150);
    System.out.println("Accounts Created!");
    System.out.println(savingsAccount);
    System.out.println(checkingAccount);
    savingsAccount.setAnnualInterestRate(4.5);
    checkingAccount.setAnnualInterestRate(1.25);
    System.out.println("Updating Interest");
    System.out.println(savingsAccount);
    System.out.println(checkingAccount);
    savingsAccount.withdrawal(5000);
    checkingAccount.withdrawal(300);
    System.out.println("Processing Withdrawal");
    System.out.println(savingsAccount);
    System.out.println(checkingAccount);
    savingsAccount.deposit(10000);
    checkingAccount.deposit(500);
    System.out.println("Processing Deposit");
    System.out.println(savingsAccount);
    System.out.println(checkingAccount);
    System.out.println("Thank you for your business!");
    }
  }

以下是运行此代码时显示的输出:

Accounts Created!
Account@6aad8bf3
Account@273221e
Updating Interest
Account@6aad8bf3
Account@273221e
Processing Withdrawal
Account@6aad8bf3
Account@273221e
Processing Deposit
Account@6aad8bf3
Account@273221e
Thank you for your business!

但是,输出实际上应该是这样的:

Accounts Created!
Savings Account: ID: 1122 Balance: $20000.00 Rate: 4.5%
Checking Account: ID: 1271 Balance: $150.00 Rate: 1.25%
Updating Interest
Savings Account: ID: 1122 Balance: $20900.00 Rate: 4.5%
Checking Account: ID: 1271 Balance: $151.88 Rate: 1.25%
Processing Withdrawals 
Savings Account: ID: 1122 Balance:... Rate: 4.5%
Checking Account: ID: 1271 Balance:... Rate 1.25%
Processing Deposits
Savings Account: ID: 1122 Balance... Rate: 4.5%
Checking Account: ID 1271 Balance... Rate:1.25%
Thank you for your business! 

任何想法我做错了什么?

3 个答案:

答案 0 :(得分:1)

您需要在自定义类中覆盖toString()方法以返回首选输出

答案 1 :(得分:1)

您需要在代码中添加以下覆盖方法:

@Override
public String toString() {
    return "Account [id=" + id + ", balance=" + balance + ", annualInterestRate="
            + annualInterestRate + ", dateCreated=" + dateCreated + "]";
}

第二期问题的解决方案:

使用以下代码替换 setId,setBalance,setAnnualInterestRate 方法:

public void setId(int id) {
      this.id = id;
    }

    public void setBalance(double balance) {
      this.balance = balance;
    }

    public void setAnnualInterestRate(double annualInterestRate) {
      this.annualInterestRate = annualInterestRate;
      this.balance = balance * annualInterestRate;
    }

答案 2 :(得分:0)

部分基于your question from yesterday ...您在toString()SavingsAccount课程中定义了CheckingAccount。这里的问题是,在main方法中,您实例化了Account而不是SavingsAccountCheckingAccount,这意味着您没有按照问题告诉您的方式使用您的子类到。

所以不要再次覆盖toString(),这是其他答案中的建议。你已经完成了。你需要的是实际使用你的子类。将main的前两行更改为

Account savingsAccount = new SavingsAccount(1122, 20000);
Account checkingAccount = new CheckingAccount(1271, 150);

您可能还需要向SavingsAccountCheckingAccount添加适当的构造函数。如果你不这样做,那么你就不会做你的导师要求你做的事。