2011-04-11 125 views
0
//******************************************************* 
// Account.java 
// 
// A bank account class with methods to deposit to, withdraw from, 
// change the name on, and get a String representation 
// of the account. 
//******************************************************* 
import java.util.Random; 
public class Account 
{ 
    private double balance; 
    private String name; 
    private long acctNum; 

    //---------------------------------------------- 
    //Constructor -- initializes balance, owner, and account number 
    //---------------------------------------------- 
    public Account(double initBal, String owner, long number) 
    { 
    balance = initBal; 
    name = owner; 
    acctNum = number; 
    } 

    //---------------------------------------------- 
    // Checks to see if balance is sufficient for withdrawal. 
    // If so, decrements balance by amount; if not, prints message. 
    //---------------------------------------------- 
    public void withdraw(double amount) 
    { 
    if (balance >= amount) 
     balance -= amount; 
    else 
     System.out.println("Insufficient funds"); 
    } 
//---------------- 
//Track how many accounts 
//---------------- 
    private static int numAccounts=0; 
    { 
     numAccounts++; 
     } 
    public static int getNumAccounts() 
    { 
     return numAccounts; 
     } 

    //---------------------------------------------- 
    // Adds deposit amount to balance. 
    //---------------------------------------------- 
    public void deposit(double amount) 
    { 
    balance += amount; 
    } 

    //---------------------------------------------- 
    // Returns balance. 
    //---------------------------------------------- 
    public double getBalance() 
    { 
    return balance; 
    } 
// Get name of account 
    public String getName() 
    { 
     return name; 
    } 
    //---------------------------------------------- 
    // Returns account number. 
    //---------------------------------------------- 

    public long getAcctNumber() 
    { 
    return acctNum; 
    } 

//---------------- 
//Void and close the accounts 
//---------------- 

    public void close() 
{ 
    balance = 0; 
    name += "CLOSE"; 
    numAccounts--; 
    } 

//---------------- 
//Consolidating accounts 
//---------------- 
    public static Account consolidate(Account acct1,Account acct2) 
    { Account newAccount=null; 
     if((acct1.getName()).equals(acct2.getName())) 
     if(acct1.getAcctNumber()!=acct2.getAcctNumber()) 
      {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner); 

         Random generator = new Random(); 
      acctNum= generator.nextInt(); 
       acct1.close(); 
       acct2.close(); 
    } 
    else 
    System.out.println("Not allow,same account number"); 
    else 
    System.out.println("Can't use other people account"); 
    return newAccount; 
    } 


//---------------------------------------------- 
    // Returns a string containing the name, account number, and balance. 
    //---------------------------------------------- 
    public String toString() 
    { 
    return "Name: " + name + 
"\nAccount Number: " + acctNum + 
"\nBalance: " + balance; 
    } 
} 

請看//鞏固部分。我想要做的是,將acct1和acct2合併到一個新帳戶中,限制條件是acct1和acct2必須具有相同的名稱,acct1和acct2帳號必須彼此不同,並且如果是遇到問題後,從兩個舊帳戶中創建一個新餘額並保留相同名稱,並隨機生成一個新帳號。我的代碼中是否有缺少的東西?它不會編譯。這些都是錯誤的,我得到Java構造函數幫助

 

    Account.java:95: ')' expected 
       {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner); 
                        ^
    Account.java:95: illegal start of expression 
       {newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner); 
                         ^

+0

下次如果有問題,請將您的問題標記爲家庭作業(這次我會這樣做)並格式化您的代碼,以便它可讀(使用文本框上方的按鈕)。 – MByD 2011-04-11 20:02:11

+0

這看起來很像_ [你幾個小時前的其他問題](http://stackoverflow.com/questions/5625314/method-of-overload)(並且,對於這個問題,[你的問題從一個小時前](http://stackoverflow.com/questions/5624239/why-isnt-this-creating-an-object))。你可以選擇一個編輯更新,保持在一個地方? – Pops 2011-04-11 20:03:52

+0

是的它是一樣的,對不起,不知道我是否應該編輯舊的或新的 – 2011-04-11 20:08:23

回答

5

String owner應該只是acct1.getName()或任何功能檢索名稱。

此外,行acctNum = generator.nextInt();將失敗,因爲acctNum未在該上下文中定義。此外,您不要將帳戶號newAccount設置爲此acctNum變量。

我建議你把它改成這樣:

newAccount.setAcctNumber(generator.nextInt());

+0

如果我做newAccount.setAcctNumber(generator.nextInt()); 然後我不希望做一個setAcctNumber太一些地方在構造函數? – 2011-04-11 20:06:51

+0

不是在構造函數中 - 而是在'Account'類的主體中,您將聲明一個帶有簽名的方法void setAcctNumber(int number){this.acctNumber = number;}' – Finbarr 2011-04-11 21:24:48

0

在該行的編譯錯誤

newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner)

是因爲String owner是應該在一個方法簽名中使用的聲明,就像你上面所做的那樣。當你真正做一個調用構造函數,不過,你需要在發送String參數。

編譯器是抱怨,因爲你實際上是在做什麼,在這條線被命名爲聲明一個owner變量String。 Java不會允許在方法調用中使用它。

Finbarr是對的;使用該方法來獲取帳戶的名稱。

+0

如果我想生成一個新合併帳戶的隨機數字?我無法想象如何在該部分內做到這一點 – 2011-04-11 20:14:34