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.

Recommended Answers

All 6 Replies

The errors on line 48 btw

Here's the driver I was gonna use to test it btw. Incidentally I get the same exact type of error with this one too.

import java.util.Scanner;

public class TestConsolidation
{
	public static void main (String args[]) 
	{	
		Scanner scan = new Scanner(System.in);
		final double intBal = 100.00;
		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(intBal, name1);
		account2 = new Account(intBal, name2);
		account3 = new Account(intBal, name3);
		
		System.out.println(account1 +"\n"+ account2 +"\n"+ account3);
		
		account1.close();
		
        account4 = new Account(name2);
        account4.consolidate(account2, account3);
		
		System.out.println("\n" + account4);
			
	}
}

Try and go easy on me lol I'm a noob and I barely understand this stuff.

You have a constructor which takes 3 params

public Account(double initBal, String owner, long number)
  {
    balance = initBal;
    name = owner;
    acctNum = number;
	 numAccounts++;
  }

But you try to use a constructor which has two parameters.

Account consolidatedAccount = new Account(0, acct1.name);

Your error message says exactly the same. Its got nothing to do with Static usage.

OK, I think I understand. I added a random generator to to my constructor to create a random long number for the variable "acctnum" and at the very least it now compiles. Now i need to test the consolidation I wrote using the driver I "attempted to write" lol It compiles but doesn't work as expected. It should create 3 bank accounts with variables name (as inputted by the user, balance (set at $100) and acctnum (random long number). Then it should close the 1st account( and attempt to consolidate the 2nd and 3rd if the variables name match and the acctnum's don't. I feel like i'm close but i'm not understanding it enough to write the proper code.

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, 0);	  	
	  double acct1Bal = acct1.getBalance();
	  double acct2Bal = acct2.getBalance();  
	  consolidatedBalance = acct1Bal + acct2Bal;
	  consolidatedAccount.balance = consolidatedBalance;
	  Random generator = new Random();
	  long rndNumber = generator.nextLong();
	  long rndNumberPos = Math.abs(rndNumber);
	  consolidatedAccount.acctNum = rndNumberPos;

	  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; 
  }
}

and here's the driver:

import java.util.Scanner;

public class TestConsolidation
{
	public static void main (String args[]) 
	{	
		Scanner scan = new Scanner(System.in);
		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, 0);
		account2 = new Account(100, name2, 1);
		account3 = new Account(100, name3, 2);
		
		System.out.println(account1 +"\n"+ account2 +"\n"+ account3);
		
		account1.close();
		
        account4 = new Account(0, name2, 3);
        account4.consolidate(account2, account3);
		
		System.out.println("\n" + account4);
			
	}
}

I think my problems is in the driver. particularly in how I call the static method "Account.consolidate"

That's right.
static methods belong to the class, not to an instance, so you call them using the class, eg Account.consolidate(etc), not account4.consolidate(etc).
consolidate returns the new account, so you need to assign that returned value to an instance, eg
Account consolidatedAccount = Account.consolidate(acc1, acc2);

Your current code compiles because Java can find the static method from any instance of the class, but that's now a deprecated way of doing things. However, doing it that way doesn't assign the new account to account4

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.