//*******************************************************
// 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); ^
下次如果有問題,請將您的問題標記爲家庭作業(這次我會這樣做)並格式化您的代碼,以便它可讀(使用文本框上方的按鈕)。 – MByD 2011-04-11 20:02:11
這看起來很像_ [你幾個小時前的其他問題](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
是的它是一樣的,對不起,不知道我是否應該編輯舊的或新的 – 2011-04-11 20:08:23