Hi Everyone, I have to add a static method Account consolidate(Account acct1, Account acct2) to an existing 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. (only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new account number. Two accounts with the same number cannot be consolidated, otherwise could double money)

Check these conditions before creating the new account. If either condition fails, do not create the new account or close the old ones; print a useful message and return null.

Account class

import java.util.Random;

public class Account
{
  private double balance;
  private String name;
  private long acctNum;
  private static int numAccounts;  //initialized to 0 automatically
  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++;
  }
  /* overload1 */
  public Account(double initBal, String owner)
  {
	balance = initBal;
	name = owner;
	Random generator = new Random();
	long rndNumber = generator.nextLong();
	long rndNumberPos = Math.abs(rndNumber);
	acctNum = rndNumberPos;
	numAccounts++;
  }
  /* overload2 */
  public Account(String owner)
  {
	balance = 0;
	name = owner;
	Random generator = new Random();
	long rndNumber = generator.nextLong();
	long rndNumberPos = Math.abs(rndNumber);
	acctNum = rndNumberPos;
	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");
  }
  public void withdraw(double amount, double fee)
  {
	if (balance >= amount + fee)
       balance -= amount + fee;
    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 a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
	return "Name: " + name + 
        "\nAccount Number: " + acctNum +
        "\nBalance: " + balance; 
  }
  /* Returns total number of accounts */
  public static int getNumAccounts()
  {
	return numAccounts;
  }
  /* closes the current account */
  void close()
  {
	name = name + " CLOSED";  
	balance = 0;
	numAccounts--;
	System.out.println(name);	
  }
  /*   */
  public static Account consolidate(Account acct1, Account acct2)
  {
    if (acct1.name == acct2.name && acct1.acctNum != acct2.acctNum)  
    {
	  Account consolidatedAccount = new Account(0, acct1.name);	  	
	  double a1Bal = acct1.getBalance();
	  double a2Bal = acct2.getBalance();  
	  consolidatedBalance = a1Bal + a2Bal;
	  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;
    }
  }
}

Write a test program that prompts for and reads in three names and creates an account with an initial balance of $100 for each. Print the three accounts, then close the first account and try to consolidate the second and third into a new account. No print the accounts again, including the consolidated one if it was created.

TestAccounts2 class

import java.util.Scanner;

public class TestAccounts2 
{

	public static void main (String args[]) 
	{	
		Scanner scan = new Scanner(System.in);
		final int INITIALBALANCE = 100;
		String name1, name2, name3;
		Account account1, account2, account3, account4;
		
		System.out.println("Enter name one: ");
		name1 = scan.nextLine();
		System.out.println("Enter name two: ");
		name2 = scan.nextLine();
		System.out.println("Enter name three: ");
		name3 = scan.nextLine();
		
		account1 = new Account(100, name1);
		account2 = new Account(100, name2);
		account3 = new Account(100, name3);
		
		System.out.println(account1 +"\n"+ account2 +"\n"+ account3);
		
		account1.close();
		
        account4 = new Account(name2);
        account4.consolidate(account2, account3);
		
		System.out.println("\n" + account4);
			
	}
}

Output
Enter name one:
albert
Enter name two:
brenda
Enter name three:
charles
Name albert
Account number: 23987429749
Balance: 100.0
Name brenda
Account number: 23947293874
Balance: 100.0
Name charles
Account number: 23724385748
Blance: 100.0
albert CLOSED
Cannot consolidat, either names do not match or account are the same.
Name: brenda
Account Number: 34574593479
Balance: 0.0

As you can see my output is not whats needed. Any suggestions on how to get the consolidateAccounts method in Account class to work properly as well as how to test it properly in TestAccounts2. Thanks for any help.

As you can see my output is not whats needed

You could help us a lot by showing us EXACTLY what is wrong and what is needed.

Your error message should show the values for the names that do not match!!

When comparing objects such as Strings you must use a method to compare the contents of the objects. The == operator is not used for comparing the contents of an object. It tests if two variables point to the same object or have the same value like with ints.

So if accounts dont have the same name or accounts have the same number
the output should not have
Name: brenda
Account Number: 34574593479
Balance: 0.0

but rather look like this:
Output
Enter name one:
albert
Enter name two:
brenda
Enter name three:
charles
Name albert
Account number: 23987429749
Balance: 100.0
Name brenda
Account number: 23947293874
Balance: 100.0
Name charles
Account number: 23724385748
Blance: 100.0
albert CLOSED
Cannot consolidat, either names do not match or account are the same.


I think there is a problem with account4.
account4 = new Account(name2);
account4.consolidate(account2, account3);

Cannot consolidat, either names do not match or account are the same.

I see you ignored my suggestion to print out the values that caused the error in the error message. That information is useful for debugging the code.

Your error message should show the values for the names that do not match!!

Line 101 you compare the names with ==
That tests for being the same object. To test if two different string objects contain the same text, use string1.equals(string2)

darn, ok got that to if (acct1.name.equals(acct2.name) && acct1.acctNum != acct2.acctNum) Thanks!


I think the problem may now be with consolidatedAccount.balance = consolidatedBalance;
since when the two accts are merged the balances are not added. hmmm

Edited 6 Years Ago by kezkez: n/a

You are trying to access instance variables from your static function. Doesn't that throw you any exception?

I mean from your consolidate function you are accesisng the instance variables of your acc1 and acc2 references.
Doesn't that cause trouble?

Hi Everyone, I have to add a static method Account consolidate(Account acct1, Account acct2) to an existing 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. (only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new account number. Two accounts with the same number cannot be consolidated, otherwise could double money)

Check these conditions before creating the new account. If either condition fails, do not create the new account or close the old ones; print a useful message and return null.

Account class

import java.util.Random;

public class Account
{
  private double balance;
  private String name;
  private long acctNum;
  private static int numAccounts;  //initialized to 0 automatically
  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++;
  }
  /* overload1 */
  public Account(double initBal, String owner)
  {
	balance = initBal;
	name = owner;
	Random generator = new Random();
	long rndNumber = generator.nextLong();
	long rndNumberPos = Math.abs(rndNumber);
	acctNum = rndNumberPos;
	numAccounts++;
  }
  /* overload2 */
  public Account(String owner)
  {
	balance = 0;
	name = owner;
	Random generator = new Random();
	long rndNumber = generator.nextLong();
	long rndNumberPos = Math.abs(rndNumber);
	acctNum = rndNumberPos;
	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");
  }
  public void withdraw(double amount, double fee)
  {
	if (balance >= amount + fee)
       balance -= amount + fee;
    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 a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
	return "Name: " + name + 
        "\nAccount Number: " + acctNum +
        "\nBalance: " + balance; 
  }
  /* Returns total number of accounts */
  public static int getNumAccounts()
  {
	return numAccounts;
  }
  /* closes the current account */
  void close()
  {
	name = name + " CLOSED";  
	balance = 0;
	numAccounts--;
	System.out.println(name);	
  }
  /*   */
  public static Account consolidate(Account acct1, Account acct2)
  {
    if (acct1.name == acct2.name && acct1.acctNum != acct2.acctNum)  
    {
	  Account consolidatedAccount = new Account(0, acct1.name);	  	
	  double a1Bal = acct1.getBalance();
	  double a2Bal = acct2.getBalance();  
	  consolidatedBalance = a1Bal + a2Bal;
	  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;
    }
  }
}

Write a test program that prompts for and reads in three names and creates an account with an initial balance of $100 for each. Print the three accounts, then close the first account and try to consolidate the second and third into a new account. No print the accounts again, including the consolidated one if it was created.

TestAccounts2 class

import java.util.Scanner;

public class TestAccounts2 
{

	public static void main (String args[]) 
	{	
		Scanner scan = new Scanner(System.in);
		final int INITIALBALANCE = 100;
		String name1, name2, name3;
		Account account1, account2, account3, account4;
		
		System.out.println("Enter name one: ");
		name1 = scan.nextLine();
		System.out.println("Enter name two: ");
		name2 = scan.nextLine();
		System.out.println("Enter name three: ");
		name3 = scan.nextLine();
		
		account1 = new Account(100, name1);
		account2 = new Account(100, name2);
		account3 = new Account(100, name3);
		
		System.out.println(account1 +"\n"+ account2 +"\n"+ account3);
		
		account1.close();
		
        account4 = new Account(name2);
        account4.consolidate(account2, account3);
		
		System.out.println("\n" + account4);
			
	}
}

Output
Enter name one:
albert
Enter name two:
brenda
Enter name three:
charles
Name albert
Account number: 23987429749
Balance: 100.0
Name brenda
Account number: 23947293874
Balance: 100.0
Name charles
Account number: 23724385748
Blance: 100.0
albert CLOSED
Cannot consolidat, either names do not match or account are the same.
Name: brenda
Account Number: 34574593479
Balance: 0.0

As you can see my output is not whats needed. Any suggestions on how to get the consolidateAccounts method in Account class to work properly as well as how to test it properly in TestAccounts2. Thanks for any help.

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