notes intro/old projects AP Problems the dump links
 

Accounts II

Start with the accounts file here.

Part I

Here is a test program for the part1 and part 2. Copy it to a new class.

  1. Suppose the bank wants to keep track of how much money is in all accounts.
    • Declare a private static variable to represent this sum.
    • Add code to constructor and nec. methods that will manage this variable.
    • Add a static method getTotalMoney that returns the total amount of money in all accounts. Think about why this method should be static – its information is not related to any particular account.

     

  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.) This is going to affect how much money the bank has. Deal with this.
  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:
    • header would look like: public static Account consolidate(Account acct1, Account acct2)
    • 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 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.

Part II Transfering funds

  1. Add a method public void transfer(Account acct, double amount) to the Account class that allows the user to transfer funds from one bank account to another. If acct1 and acct2 are Account objects, then the call acct1.transfer(acct2,957.80) should transfer $957.80 from acct1 to acct2. Be sure to clearly document which way the transfer goes!
  2. Test this out either in bluej or ecilpse (in a test class).
  3. Add a static method to the Account class that lets the user transfer money between two accounts without going through either account. You can (and should) call the method transfer just like the other one – you are overloading this method. Your new method should take two Account objects and an amount and transfer the amount from the first account to the second account. The signature will look like this:


  4. public static void transfer(Account acct1, Account acct2, double amount)

  5. Test this

     

Add these methods to BankAccount:

public static BankAccount consolidate(BankAccount acctOne, BankAccount acctTwo) {
		// TODO Auto-generated method stub
		return null;
	}

	public void transfer(BankAccount acct, double amount) 
	{
		// TODO Auto-generated method stub
		
	}

	public static void transfer(BankAccount acct1, BankAccount acct2, double amount)
	{
		// TODO Auto-generated method stub
		
	}