Java Constructor help
//*******************************************************
// 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;
}
}
Please look at the //consolidate section. What I'm trying to do is, consolidate acct1 and acct2 into one new account, with the restrictions that is acct1 and acct2 has to has the same name, acct1 and acct2 account number has to be different from each other, and if those are met, create a new account with a new balance from the two old account, keep the same name and also randomly generate a new account number. Is there something missing in my code? It wont compile. These are errors i got
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
should just be acct1.getName()
or whatever function retrieves the name.
Also, the line acctNum = generator.nextInt();
will fail as acctNum
isn't defined in that context. Furthermore, you don't set the account number of newAccount
to be this acctNum
variable.
I suggest you change it to this:
newAccount.setAcctNumber(generator.nextInt());
The compile error at the line
newAccount= new Account(acct1.getBalance()+acct2.getBalance(),String owner)
is because String owner
is a declaration that should be used in a method signature, as you did above. When you actually make a call to the constructor, though, you need to send a String
parameter in.
The compiler is complaining because what you are actually doing at this line is declaring a String
variable named owner
. Java won't allow that in a method call.
Finbarr is right; use the method to get the name of the account instead.
精彩评论