I'm rather new to Java, and I can't quite figure out what to do with this program. Any help or suggestions would be greatly appreciated. Thanks in advance.

File Account.java (see previous exercise) contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and return a String representation. Note that the constructor for this class creates a random account number. Save this class to your directory and study it to see how it works. Then write the following additional code:

1. Suppose the bank wants to keep track of how many accounts exist.

a. Declare a private static integer variable numAccounts to hold this value. Like all instance and static variables, it will be initialized (to 0, since it’s an int) automatically.
b. Add code to the constructor to increment this variable every time an account is created.
c. Add a static method getNumAccounts that returns the total number of accounts. Think about why this method should be static – its information is not related to any particular account.
d. File TestAccounts1.java contains a simple program that creates the specified number of bank accounts then uses the getNumAccounts method to find how many accounts were created. Save it to your directory, then use it to test your modified Account class.

2. Add a method void close() to your Account class. This method should close the current account by appending “CLOSED” to the account name and setting the balance to 0. (The account number should remain unchanged.) Also decrement the total number of accounts.

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

• Only accounts with the same name can be consolidated. The new account gets the name on the old accounts but a new account number (you can pick any number).
• Two accounts with the same number cannot be consolidated. Otherwise this would be an easy way to double your 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.

4. Write another test program TestConsolidation.java (Do not use TestAccount1.java) 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. Now print the accounts again, including the consolidated one if it was created.

//*******************************************************
// 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;

  //----------------------------------------------
  //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;
  }
  
  //--------------------------------------------
  // Returns total number of accounts.
  //--------------------------------------------
  public static int getNumAccounts()
  {
		return numAccounts;
  }
  
	//--------------------------------------------
	// Closes current account
	//--------------------------------------------
	public void close()
	{
		name = name + " CLOSED";
		balance = 0;
		numAccounts --;
	}
	
	//--------------------------------------------
	// Consolidates acct1 and acct2
	//--------------------------------------------
	public static Account consolidate(Account acct1, Account acct2)
	{
		if (acct1.acctNum == acct2.acctNum)
			{
			System.out.println("Same accounts cannon be consolidated.");
			return null;
			}
		else if (acct1.name == acct2.name)
			{
				Account acctconsol = new Account (acct1.balance + acct2.balance, acct1.name, 1234);
				return acctconsol;
			}
			else
				{
				System.out.println("Accounts are not under the same name.");
				return null;
				}
	}

  //----------------------------------------------
  // Returns a string containing the name, account number, and balance.
  //----------------------------------------------
  public String toString()
  {
	return "Name: " + name + 
"\nAccount Number: " + acctNum +
"\nBalance: " + balance; 
  }
}


import java.util.Scanner;

public class TestConsolidation
{
	public static void main (String[] args)
	{
	
	Account acct1, acct2, acct3, acctcons;
	
	Scanner scan = new Scanner(System.in);
	
	System.out.println("What is the name for account 1?");
	String name = scan.nextLine();
	acct1 = new Account(100, name, 45);
	
	System.out.println("What is the name for account 2?");
	name = scan.nextLine();
	acct2 = new Account(100, name, 21);
	
	System.out.println("What is the name for account 3?");
	name = scan.nextLine();
	acct3 = new Account(100, name, 87);
	
	System.out.println(acct1);
	System.out.println(acct2);
	System.out.println(acct3);
	
	acct1.close();
	acctcons = new Account(0, "name", 33);
	acctcons.consolidate(acct1, acct2);
	
	}
}

Recommended Answers

All 2 Replies

It looks like you posted you homework assignment, but you haven't stated what problem you are having with it.
Let us know a specific area you are having trouble with.

Oh, that would be helpful, wouldn't it? Haha.

The primary problem I'm having is trying to return null in the static method Account consolidate in the Account.java. I'm getting a compile error.

Another problem is that I have no idea how to consolidate accounts 2 and 3. The directions for the problem state that account 1 should be closed, and accounts 2 and 3 need to be consolidated. How do I take the balance from accounts 2 and 3 and add them together into a new account called acctcons, using the Account consolidate method?

Again, I'm new to Java, so I'm sorry if any of this doesn't make sense. Thanks for the help!

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.