2015-11-29 141 views
-2

總結,我要寫一個名爲BankAccount的類,我的教授給我提供了一個驅動程序。調用方法(Java)

這是到目前爲止我的課:

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 ; 
} 
} 

是我遇到的問題是,當驅動程序調用myAccount.unfreeze();在我的課不設置帳戶解凍。所以,當司機去存款時,即使我有一個名爲unfreeze的方法,我們班也會返回Cannot Deposit - Account Is Frozen。我一開始以爲我可能錯過拼寫錯誤的凍結或解凍,但事實並非如此。希望一些新鮮的眼睛可以發現我正在跳過的東西。

謝謝你的幫助!

+1

你認爲'frozen = true'的確如此?你爲什麼這麼認爲? '='做什麼? '=='做什麼? (投票結束爲排印錯誤。) –

回答

2

當您使用單方程符號時,您正在分配值。使用雙等號檢查是否相等。在你的情況下,每當你檢查它的值時,你應該使用if(frozen==false)if(frozen==true)

+3

或者更優雅,如果(凍結)和if(!凍結) – Nerdizzle

+1

是的。既然他不瞭解單等同和雙等號之間的區別,就不得不向他解釋這一點。 – mayooran

-2

爲了凍結布爾值的值切換到您可以使用以下相反。

frozen = !frozen; 

這將改變爲真,假爲真。

還必須改變「=」到「==」在你的if語句,「=」是賦值運算符,「==」是一個平等的測試。