开发者

Why isn't this creating an object?

Could someone look at the portion of my code where I'm trying to void and close an account? This is what I want to achieve:

"2. Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. (The account number should remain unchanged.) Also decrement the total number of accounts."

When I tried to compile it, the program gave me an error about an identifier being expected. What am I missing?

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

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;
  }

    //----------------------------------------------
  // Returns account number.
  //----------------------------------------------

  public long getAcctNumber()
  {
    return acctNum;
  }

//----------------
//Void and close the accounts
//----------------

public void close(Account)
{
    balance = 0;
    name = "CLOSE";
    acctNum = number;
    numAccounts--;
     return Account;
     }


  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //-------开发者_StackOverflow社区---------------------------------------
  public String toString()
  {
    return "Name: " + name + 
"\nAccount Number: " + acctNum +
"\nBalance: " + balance; 
  }
}


public void close(Account) needs a variable name. The declaration says it takes an Account parameter, but you also need to give the parameter a name e.g. public void close(Account account).

Note that I don't think that makes much sense. Given an account object you should probably be able to close it, so perhaps you should reconsider whether that parameter makes sense.


There are few problems:

public void close(Account) << why do you have Account as a parameter (half parameter)?
{
    balance = 0;
    name = "CLOSE";     << This is assignment, not addition
    acctNum = number;   << where number came from
     acctNum--;
     return Account;    << this is a void method, why do you return something?
}

It should look like that:

public void close()
{
    balance = 0;
    name += "CLOSE";     << adding "close" to current account name
     acctNum--;
}

Another thing, acctNum is the total number of accounts, it shouldn't be related to a specific object, so it should be declared as static.

EDIT

Clarification regarding the account count and account number:

For each account, there should be an account number, let's say the first account has account number = 1 and the second account has account number = 2 etc. so we'll call this variable accountNumber and declare it as private variable:

private int accountNumber;

You also want to track the number of active accounts, right? So you create another variable which is not bounded to a specific account, but is related to all, and call it accountCount and you should declare it as static variable:

private static int accountCount = 0;

When you close an account, you want to decrease the account count and keep the count number, so your method should look like:

public void close()
{
    balance = 0;
    name += "CLOSE";     
    accountCount--;  << we decrease the account count, not the specific account number
}


Would be best to go for public void close() { ... }

you'd be calling it on an instance of the account class (like myAccount.close()) so it's going to be working on itself. Adding stuff you don't need is almost always a bad idea :)


You don't specify a parameter name.

Change it to

public void close(Account acct){
  //...
}


public void close() {
    ...
}

I think they're expecting you to add a method with that signature. Also, it says to append "CLOSED" to the name not "set" the name to "CLOSED". I don't think you need to modify anything with the acctNum variable.

Those should be enough hints to complete this.


This line needs to be inside of a method rather than part of your class declaration:

{        
     numAccounts++;
} 

Try making it:

public void incrementAccountNumber() {
     numAccounts++;
}


You need to specify an argument name for your close() method:

public void close(Account account) { ... }


Further to the other answers, you've got an unknown variable number:

 acctNum = number;


public void close(Account)

should have a variable name. Something like this:

public void close(Account account)
{
    balance = 0;
    name = "CLOSE";
    acctNum = number;
    acctNum--;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜