2011-04-11 159 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