没有得到正确的输出

时间:2016-08-26 14:39:37

标签: java inheritance tostring

可能只是一个小错误,但我似乎无法在任何地方找到它。当我运行该程序时,它打印出“存入100美元后:储蓄账户:”,我的提款类似乎也没有工作,因为取款后的余额不会改变。

public class CheckingandSavings 
{

    public static void main(String[] args) {
        Savings savings = new Savings(1001,1000.0);
        Checking checking = new Checking(1002, 2000.0);

        System.out.println("At the beginning: " + savings);
        savings.deposit(100);
        System.out.println("After depositing $100: " + savings);
        savings.withdraw(500);
        System.out.println("After withdrawing $500: " + savings);

        System.out.println("");

        System.out.println("At the beginning: " + checking);
        checking.deposit(100);
        System.out.println("After depositing $100: " + checking);
        checking.withdraw(500);
        System.out.println("After withdrawing $500: " + checking);
    }
}

public class Account {

    private int accountNumber;
    private double accountBalance;

    //The Two-Arg Constructor
    public Account(int accountNumber, double accountBalance)
    {
        setAccountBalance(accountBalance);
        setAccountNumber(accountNumber);
    }

    //Getter for accountNumber
    public int getAccountNumber()
    {
        return accountNumber;
    }

    //Setter for accountNumber
    public void setAccountNumber(int accountNumber)
    {
        if (accountNumber >= 0)
            this.accountNumber = accountNumber;
    }

    //Getter for accountBalance
    public double getAccountBalance()
    {
        return accountBalance;
    }

    //Setter for accountBalance
    public void setAccountBalance(double accountBalance)
    {
        if (accountNumber >= 0)
            this.accountBalance = accountBalance;
    }

    //Deposit to accountBalance
    public void deposit(double amount)
    {
        if (amount > 0)
            this.accountBalance += amount;
    }

    //Withdraw from accountBalance
    public double withdraw(double amount)
    {
        if (amount > 0 || amount > this.accountBalance)
            return 0;

        this.accountBalance -= amount;
        return this.;
    }

    //Returns a string of the instance data
    public String toString()
    {
        String result = "";
        result += "Account Number: " + this.accountNumber;
        result += "\nAccount Balance: $" + String.format("%.2f", this.accountBalance);
        return result;
    }
}

public class Savings extends Account {

    //The two-arg constructor
    public Savings(int accountNumber, double accountBalance)
    {
        super(accountNumber, accountBalance);
    }

    //Returns a string of the instance data
    public String toString()
    {
        String result = "";
        result += "Savings Account: \n" + super.toString();
        return result;
    }
}

public class Checking extends Account {


    //The two-arg constructor
    public Checking(int accountNumber, double accountBalance)
    {
        super(accountNumber,accountBalance);
    }

    //Returns a string of the instance data
    public String toString() {
        String result = "";
        result += "Checking Account: \n" + super.toString();
        return result;
    }
}

1 个答案:

答案 0 :(得分:2)

看看你的退出方法:

//Withdraw from accountBalance
public double withdraw(double amount)
{
    if (amount > 0 || amount > this.accountBalance) //This needs to be &&
        return 0;

    this.accountBalance -= amount;
    return this.; //I am assuming you meant this to be this.accountBalance?
}

您说要提取的金额是否大于0或者是否大于您的帐户余额,请返回0.我想您想说AND而是放amount > 0 && amount > this.accountBalance

此外,您应该返回this.accountBalance

最后,您应该将@Override注释放在toString方法之上。这使编译器知道您正在覆盖父方法。

相关问题