调用方法(Java)

时间:2015-11-29 02:09:43

标签: java methods

总结,我要写一个名为BankAccount的课程,我的教授为我提供了一个驱动程序。

到目前为止,这是我的课程:

import  java.util.Date;

public class BankAccount implements AccountInterface
{
private double balance;
private String name;
private Date creationDate = new Date ();
private boolean frozen;
private double limit;
private final double MAXLIMIT = 500;
private int accountNumber;
private static int howMany;

public BankAccount( )
{
    this.name = "Classified";
    this.creationDate.getTime();
    this.frozen = false;
    this.limit = 300;
    this.howMany++;
    this.accountNumber = howMany;
    this.balance = 0;
}

public BankAccount (String creationName)
{
    this.name = creationName;
    this.creationDate.getTime();
    this.frozen = false;
    this.limit = 300;
    this.howMany++;
    this.accountNumber = howMany;
    this.balance = 0;
}

public static int getNumAccounts ( )
{
    return howMany;
}

public void deposit(double theMoney)
{
    if (frozen = true)
        throw new IllegalStateException ("Cannot Deposit - Account Is Frozen");
    else if (theMoney < 0)
        throw new IllegalArgumentException("Insufficient funds");
    else
        balance = balance + theMoney;
}

public double withdraw(double theMoney)
{
    if (theMoney < 0 ||  balance == 0 || theMoney > limit || theMoney % 20 !=0)
        throw new IllegalArgumentException ("There was an error in your withdraw.");
    else if (frozen = true)
        throw new IllegalStateException ("Cannot Deposit - Account Is Frozen");
    else
        balance = balance - theMoney;

    return balance;
}

public double getBalance()
{
    return balance;
}

public void freeze()
{
    frozen = true;
}

public void unfreeze()
{
    frozen = false;
}

public void setLimit(double newLimit)
{
    if (newLimit < 0 || newLimit > MAXLIMIT)
        throw new IllegalArgumentException ("There was a limit error.");
    else if (frozen = true)
        throw new IllegalStateException ("Cannot Deposit - Account Is Frozen");
    else
        limit = newLimit;
}

public double getLimit( )
{
    return limit;
}

 public String toString( )
 {
     return "\nAccount number: " + accountNumber + "\nName: " + name + "\nCreation Date: " + creationDate + "\nBalance: " + balance + "\nWithdrawal Limit: " + limit ;
 }
}

我遇到的问题是当我的课程中的驱动程序调用{​​{1}}时,它没有将帐户设置为解冻。因此,当司机去存款时,我的班级会返回myAccount.unfreeze();,即使我有一个名为unfreeze的方法。我一开始以为我可能错过了拼写冻结或解冻错误,但事实并非如此。希望一些新鲜的眼睛可以发现我正在跳过的东西。

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

使用单个方程式符号时,您将分配该值。使用双等号检查是否相等。在您的情况下,只要您检查其值,就应该使用if(frozen==false)if(frozen==true)

答案 1 :(得分:-2)

为了将冻结的布尔值的值切换为相反的值,您可以使用以下内容。

frozen = !frozen;

这会将true更改为false,将false更改为true。

您还必须在if语句中将“=”更改为“==”,“=”是赋值运算符,“==”是一个相等测试。