//*******************************************************
// 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;
}
}
请查看// consolidate部分。我想做的是,将acct1和acct2合并到一个新帐户中,限制是acct1和acct2必须具有相同的名称,acct1和acct2帐号必须彼此不同,如果是遇到,用两个旧帐户创建一个具有新余额的新帐户,保持相同的名称,并随机生成一个新的帐号。我的代码中缺少什么吗?它不会编译。这些是我得到的错误
Account.java:95:')'预期
{newAccount =新帐户(acct1.getBalance()+ acct2.getBalance(),字符串所有者);
^
Account.java:95:表达式的非法开始
{newAccount =新帐户(acct1.getBalance()+ acct2.getBalance(),字符串所有者);
^
最佳答案
String owner
应该只是acct1.getName()
或任何检索名称的函数。
另外,行acctNum = generator.nextInt();
将失败,因为在该上下文中未定义acctNum
。此外,您不必将newAccount
的帐号设置为此acctNum
变量。
我建议您将其更改为:newAccount.setAcctNumber(generator.nextInt());