Lab 5d: Transferring Funds
File BankAccount5d.java contains a definition for a simple bank account class with methods to withdraw, deposit, get the balance and account number, and print a summary. Save it to your directory and study it to see how it works. Then write the following additional code:
- Add transfer() function - Add a static method to the BankAccount5d class with the signature:
public static void transfer(BankAccount5d acct1, BankAccount5d acct2, double amount)
that lets the user transfer money between two accounts. Your new method should take two BankAccount5d objects and an amount and transfer the amount from the first account to the second account.
For example: If acct1 and acct2 are BankAccount5dobjects, then the call:
BankAccount5d.transfer(acct1, acct2,957.80);
shouldREMOVE$957.80 from acct1and ADD that amount to acct2. NOTE: Be sure to clearly document which way the transfer goes!
- New TransferTest5dclass - Write a class TransferTest5d with a main method that creates two bank account objectseach starting with $1000and enters a loop that does the following:
- Prompt user for type of transfer - Asks if the user would like to:
- Transfer money from account1 to account2
- Transfer from account2 to account1
- Quit.
- If a transfer is chosen:
- Prompt user for amount - If a transfer is chosen, asks the amount of the transfer
- Carry out transfer - Carry out the transfer operation.
- Print new balance - Print the new balance for each account.
- Repeat until Quit -Repeats until the user asks to quit.
EXTRA CREDIT: In the transfer() methods, prevent the transfer from occurring if the source account doesn’t have enough money to withdraw and notify the user.
//*******************************************************// BankAccount5d.java [Lab 5d]
// 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.
//*******************************************************
publicclass BankAccount5d
{
privatestaticintiNumDeposits, iNumWithdrawals;
privatestaticdoubledTotDeposits, dTotWithdrawals;
privatedouble balance;
private String name;
privatelongacctNum;
// ------
// Constructor -- initializes balance, owner, and account number
// ------
publicBankAccount5d( doubleinitBal, String owner, long number )
{
balance = initBal;
name = owner;
acctNum = number;
}
// ------
// Start counters over again
// ------
publicstaticvoidclearDailyTotals()
{
iNumDeposits = 0;
iNumWithdrawals = 0;
dTotDeposits = 0;
dTotWithdrawals = 0;
}
// ------
// Returns the number of withdrawals made
// ------
publicstaticintgetNumDeposits()
{
returniNumDeposits;
}
// ------
// Returns the number of withdrawals made
// ------
publicstaticintgetNumWithdrawals()
{
returniNumWithdrawals;
}
// ------
// Returns the total $$ of deposits made
// ------
publicstaticdoublegetTotDeposit()
{
returndTotDeposits;
}
// ------
// Returns the total $$ of withdrawals made
// ------
publicstaticdoublegetTotWithdrawals()
{
returndTotWithdrawals;
}
// ------
// TODO #1: Overload transfer() function -
// Create a static function that takes in 2 accounts
// and the amount and perform the transfer.
// ------
// ------
// Changes the name on the account
// ------
publicvoidchangeName( String newName )
{
name = newName;
}
// ------
// Deducts $10 service fee
// ------
publicdoublechargeFee()
{
balance -= 10.0;
return balance;
}
// ------
// Adds deposit amount to balance.
// ------
publicvoiddeposit( double amount )
{
balance += amount;
iNumDeposits += 1;
dTotDeposits += amount;
}
// ------
// returns the user's account number
// ------
publiclonggetAcctNumber()
{
returnacctNum;
}
// ------
// Returns balance.
// ------
publicdoublegetBalance()
{
return balance;
}
// ------
// Prints a summary of the object
// ------
publicvoidprintSummary()
{
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.
// ------
publicvoidwithdraw( double amount )
{
if ( balance >= amount )
{
balance -= amount;
iNumWithdrawals += 1;
dTotWithdrawals += amount;
}
else
{
System.out.println( "Insufficient funds" );
}
}
}
//*******************************************************
// TransferTest5d.java [Lab 5d]
// 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.
//*******************************************************
publicclass TransferTest5d
{
publicstaticvoidmain( String[] args )
{
// ------
// TODO #2: Build main() method for class
// TransferTest5d based on instructions
// ------
}
}
Sample Output
Would you like to:(1) Transfer $ from account1 to account2:
(2) Transfer $ from account2 to account1:
(3) Quit
1
How much would you like to transfer?
200
Account 1: Name: [Mike] Acct #:[1111] Balance:[800.0]
Account 2: Name: [Susan] Acct #:[2222] Balance:[1200.0]
Would you like to:
(1) Transfer $ from account1 to account2:
(2) Transfer $ from account2 to account1:
(3) Quit
2
How much would you like to transfer?
50
Account 1: Name: [Mike] Acct #:[1111] Balance:[850.0]
Account 2: Name: [Susan] Acct #:[2222] Balance:[1150.0]
Would you like to:
(1) Transfer $ from account1 to account2:
(2) Transfer $ from account2 to account1:
(3) Quit
3
Page 1 of 4 / Static Variables & Methods