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;

  }
  
}

Edited 7 Years Ago by COKEDUDE: n/a

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

Break this down :

>>Add a static method Account consolidate(Account acct1, Account acct2) to your Account class
<<

public Account consolidate(Account account1, Account account2)
{ /* Code */ }

Next part of the statement :
>>
Account class creates a new account
<<

public Account consolidate(Account account1, Account account2)
{ 
     Account accountSum;
}

whose balance is the sum of the balances in acct1 and acct2 and closes acct1 and acct2. The new account should be returned.

public Account consolidate(Account account1, Account account2)
{
       Account accountSum;
      accountSum.balance = account1.getBalance() + account2.getBalance();

   //make account1 and account close()
}

See how that works. Break down the question, and it becomes simpler.
So you need to sum the balance, and close account1 and account2.
By the way, You should make account1 and account2 = null when its closed.

1)You should always post error you getting
2)Line 93 if statement is with capital "i"
3)You are missing brackets for your first if statement on line 92
4)Line 95 is not a Java expression. Haven't you been given rules about consolidation?

1)You should always post error you getting
2)Line 93 if statement is with capital "i"
3)You are missing brackets for your first if statement on line 92
4)Line 95 is not a Java expression. Haven't you been given rules about consolidation?

Is there a particular program that you know of that gives good error codes? I use a combination of Jgrasp and netbeans. Thank you for the figuring out the "I" and brackets.
Could you explain the consolidation rules please. I really don't understand them.

>>

public Account consolidate(Account account1, Account account2)
{
       Account accountSum;
      accountSum.balance = account1.getBalance() + account2.getBalance();

   //make account1 and account close()
}

See how that works. Break down the question, and it becomes simpler.
So you need to sum the balance, and close account1 and account2.
By the way, You should make account1 and account2 = null when its closed.

Ok this looks better. For some reason it doesn't think accountSum has been initialized so my program won't run :(.
Here's what my program looks like so far.

//*******************************************************
// 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
  public Account consolidate(Account account1, Account account2)
  {
      Account newAccount = null;
      if((account1.name).equals(account2.name))
      {
          if(account1.acctNum!=account2.acctNum)
          {
              Account accountSum;
              accountSum.balance = account1.getBalance() + account2.getBalance();
              //Consolidate acct1 and acct2 like newAccount = (name, balance of 2 accts, acctnum(random));
		account1.close();
		account2.close();
          }}


      return newAccount;

  }
  
}

>>or some reason it doesn't think accountSum has been initialized so my program won't run
<<

Yea its right, sorry I am in c++ mode.

try :

Account accountSum = new Account();
//Account accountSum;
Account accountSum=new Account();

What exactly the issue ?

This article has been dead for over six months. Start a new discussion instead.