Ok this is driving me absolutely crary!
I'm trying to add a static method Account consolidate(Account acct1, Account acct2) to my Account class that creates a new account whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned.
import java.util.Random;
public class Account
{
private double balance;
private String name;
private long acctNum;
private static int numAccounts;
private static double consolidatedBalance;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
numAccounts++;
}
//----------------------------------------------
// Returns total number of accounts
//----------------------------------------------
public static int getNumAccounts()
{
return numAccounts;
}
//----------------------------------------------
// closes the current account
//----------------------------------------------
void close()
{
name += " CLOSED";
balance = 0;
numAccounts--;
System.out.println(name);
}
//----------------------------------------------------
// Consolidates two accounts of the same name
//----------------------------------------------------
public static Account consolidate(Account acct1, Account acct2)
{
if (acct1.name.equals(acct2.name) && acct1.acctNum != acct2.acctNum)
{
Account consolidatedAccount = new Account(0, acct1.name);
double acct1Bal = acct1.getBalance();
double acct2Bal = acct2.getBalance();
consolidatedBalance = acct1Bal + acct2Bal;
consolidatedAccount.balance = consolidatedBalance;
acct1.close();
acct2.close();
return consolidatedAccount;
}
else
{
System.out.println("Cannot consolidate, either names do not match"
+ " or account numbers are the same");
return null;
}
}
//----------------------------------------------
// 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");
}
//----------------------------------------------
// 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;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name: " + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
}
Here's the error I'm getting.
----jGRASP exec: javac -g Account.java
Account.java:55: error: constructor Account in class Account cannot be applied to given types;
Account consolidatedAccount = new Account(0.00, acct1.name);
^
required: double,String,long
found: double,String
reason: actual and formal argument lists differ in length
1 error
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Any help would be greatly appreciated.