Download Lab 5b: Opening and Closing Accounts

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab 5b: Opening and Closing Accounts
File BankAccount5b.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.
Track number of accounts created - Suppose the bank wants to keep track of how many accounts exist.
a. Create counter - 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. Increment counter - Add code to the constructor to increment this variable every time an account is created.
c. Create accessor (“getter”) method - 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. Test counter - Add File TestAccounts5b.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 BankAccount5b class.
2.
Close the account - Add a method void close() to your BankAccount5b 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.
Consolidate accounts - Add a static method public static BankAccount5b consolidate(BankAccount5b
acct1, BankAccount5b acct2) to your BankAccount5b 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:


Consolidate accounts with the same name - Only accounts with the same name can be consolidated. The new account
o gets the same name on the old accounts
o but a new account number
Consolidate accounts with different account numbers - Two accounts with the same number cannot be consolidated.
Otherwise this would be an easy way to double your money!
NOTE: 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.
Test new method in RunAccounts5b - Write a test program that will:
a. Reads in 3 names - Prompts for and reads in three names
b. Creates new accounts - Creates an account with an initial balance of $100 for each.
c. Print the 3 new accounts - Print the three accounts’ summaries
d. Close the first - Close the first account
e. Consolidate the second and third - Consolidate the second and third into a new account.
th
f. Print all 4 accounts (if 4 was created) - Print the first 3 accounts again, including the consolidated one if it was
created.
//***********************************************************
// TestAccounts5b.java [Lab 5b]
//
Student Name: <name>
// A simple program to test the numAccts method of the
// Account class.
//***********************************************************
import java.util.Scanner;
public class TestAccounts5b
{
public static void main( String[] args )
{
BankAccount5b testAcct;
Scanner scan = new Scanner( System.in );
Page 1 of 5
Static Variables & Methods
System.out.println( "How many accounts would you like to create?" );
int num = scan.nextInt();
for ( int i = 1; i <= num; i++ )
{
testAcct = new BankAccount5b( 100, "Name" + i );
System.out.println( "\nCreated account " + testAcct );
System.out.println( "Now there are " + BankAccount5b.getNumAccounts() + " accounts" );
}
}
}
//*******************************************************
// RunAccounts5b.java [Lab 5b]
//
Student Name: <name>
//
// Runner class that will test BankAccount5b
//*******************************************************
import java.util.Scanner;
public class RunAccounts5b
{
public static void main( String[] args )
{
Scanner scan = new Scanner( System.in );
String strName1, strName2, strName3;
BankAccount5b acct1 = null, acct2 = null, acct3 = null, acct4 = null;
// ---------------------------------------------// TODO #5a: Read in 3 names
// ---------------------------------------------// TODO #5b: Create 3 new accounts with those names
// ---------------------------------------------// TODO #5c: Print the new account summaries
System.out.println( "\nAccounts BEFORE transactions occur:" );
// ---------------------------------------------// TODO #5d: Close the first account
//
//
//
//
---------------------------------------------TODO #5e: Consolidate the second and the third
NOTE: Remember to set acct4 to the consolidated
account
// ---------------------------------------------// TODO #5c: Print the account summaries (and
// print the new account IF it is not null)
System.out.println( "\nAccounts AFTER transactions occur:" );
}
}
//*******************************************************
// BankAccount5b.java [Lab 5b]
//
Student Name: <name>
//
// A bank account class with methods to deposit to, withdraw from,
// change the name on, charge a fee to, and print a summary of the account.
//*******************************************************
public
{
//
//
//
//
class BankAccount5b
---------------------------------------------TODO #1a: Add static numAccounts to track how many
accounts exist.
----------------------------------------------
private static int accountNumber = 1000;
private double balance;
private String name;
Page 2 of 5
Static Variables & Methods
private long acctNum;
// ---------------------------------------------// Constructor -- initializes balance, and owner name
// ---------------------------------------------public BankAccount5b( double initBal, String owner )
{
balance = initBal;
name = owner;
acctNum = accountNumber++;
// ---------------------------------------------// TODO #1b: Increment numAccounts counter
}
// ---------------------------------------------// TODO #3: Create consolidate method
// ---------------------------------------------public static BankAccount5b consolidate( BankAccount5b acct1, BankAccount5b acct2 )
{
BankAccount5b newAcct = null;
// ---------------------------------------------// TODO #3: Compare Account names
// ---------------------------------------------// TODO #3: Compare Account Numbers
// ---------------------------------------------// TODO #3: Create new Bank account
// ---------------------------------------------// TODO #3: Close existing accounts
// ---------------------------------------------// TODO #3: Return new account
return newAcct;
}
//
//
//
//
---------------------------------------------TODO #1c: Create static getNumAccounts() to
return # of accounts created
----------------------------------------------
// ---------------------------------------------// Changes the name on the account
// ---------------------------------------------public void changeName( String newName )
{
name = newName;
}
// ---------------------------------------------// Deducts $10 service fee
// ---------------------------------------------public double chargeFee()
{
balance -= 10.0;
return balance;
}
//
//
//
//
---------------------------------------------TODO #1c: Create close method by setting balance
to 0 and appending CLOSED to the account name
----------------------------------------------
// ---------------------------------------------// Adds deposit amount to balance.
// ---------------------------------------------public void deposit( double amount )
{
balance += amount;
}
Page 3 of 5
Static Variables & Methods
// ---------------------------------------------// returns the user's account number
// ---------------------------------------------public long getAcctNumber()
{
return acctNum;
}
// ---------------------------------------------// Returns balance.
// ---------------------------------------------public double getBalance()
{
return balance;
}
// ---------------------------------------------// Return name of the account
// ---------------------------------------------public String getName()
{
return name;
}
// ---------------------------------------------// Prints a summary of the object
// ---------------------------------------------public void printSummary()
{
System.out.println( this );
}
// ---------------------------------------------// Returns a string containing the name, account number, and balance.
// ---------------------------------------------@Override
public String toString()
{
return "Name: [" + name + "] Acct #:[" + acctNum + "] Balance:[" + balance + "]";
}
// ---------------------------------------------// 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" );
}
}
}
Sample Output #1:
Please enter first account name: John
Please enter second account name: Mary
Please enter third account name: Mary
Accounts BEFORE transactions occur:
Name: [John] Acct #:[1000] Balance:[100.0]
Name: [Mary] Acct #:[1001] Balance:[100.0]
Name: [Mary] Acct #:[1002] Balance:[100.0]
Accounts AFTER transactions occur:
Page 4 of 5
Static Variables & Methods
Name:
Name:
Name:
Name:
[John - CLOSED] Acct
[Mary - CLOSED] Acct
[Mary - CLOSED] Acct
[Mary] Acct #:[1003]
#:[1000] Balance:[0.0]
#:[1001] Balance:[0.0]
#:[1002] Balance:[0.0]
Balance:[200.0]
Sample Output #2:
Please enter first account name: John
Please enter second account name: Jacob
Please enter third account name: Jingleheimer
Accounts BEFORE transactions occur:
Name: [John] Acct #:[1000] Balance:[100.0]
Name: [Jacob] Acct #:[1001] Balance:[100.0]
Name: [Jingleheimer] Acct #:[1002] Balance:[100.0]
Can't consolidate accounts[Jacob] and [Jingleheimer]
Accounts AFTER transactions occur:
Name: [John - CLOSED] Acct #:[1000] Balance:[0.0]
Name: [Jacob] Acct #:[1001] Balance:[100.0]
Name: [Jingleheimer] Acct #:[1002] Balance:[100.0]
Page 5 of 5
Static Variables & Methods