Could anyone tell me what is wrong this code please. I'm trying to do this.
Add a static method Account consolidate(Account acct1, Account acct2) to your 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. Two important rules of consolidation:
Starting at line 98 is where I'm starting to have problems :(.
//*******************************************************
// 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;
private static int numAccounts = 0;
//----------------------------------------------
//Constructor -- initializes balance, owner, and account number
//----------------------------------------------
public Account(double initBal, String owner, long number)
{
balance = initBal;
name = owner;
acctNum = number;
numAccounts++;
}
//----------------------------------------------
// 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;
}
//-----------------------------------------------
//getNumAccount: return the of accounts
//-----------------------------------------------
public static int getNumAccounts()
{
return numAccounts;
}
//----------------------------------------------
// Returns a string containing the name, account number, and balance.
//----------------------------------------------
public String toString()
{
return "Name: " + name +
"\nAccount Number: " + acctNum +
"\nBalance: " + balance;
}
public void close ()
{
name = name + "CLOSED";
balance = 0;
numAccounts--;
}
//Account consolidate- the sum of acct1 and acct2
private static Account consolidate(Account acct2, Account acct3)
{
Account newAccount = null;
if((acct2.name).equals(acct3.name))
If(acct2.acctNum!=acct3.acctNum)
{
Consolidate acct1 and acct2 like newAccount = (name, balance of 2 accts, acctnum(random));
acct1.close();
acct2.close();
}
return newAccount;
}
}