//*******************************************************
// 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: ')' 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); ^
最佳答案
String Owner
应该只是 acct1.getName()
或任何检索名称的函数。
此外,行 acctNum = Generator.nextInt();
将失败,因为 acctNum
未在该上下文中定义。此外,您没有将 newAccount
的帐号设置为此 acctNum
变量。
我建议你改成这样:
newAccount.setAcctNumber(generator.nextInt());
关于Java 构造函数帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5626762/