//*******************************************************
// 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