Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Transfer Funds Java Assignment File Account.java (see A Flexible Account Class given below) contains a definition for a simple bank account class with methods to

Transfer Funds Java Assignment

File Account.java (see A Flexible Account Class given below) 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:

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. Write a class TransferTest with a main method that creates two bank account objects and enters a loop that does the following: Asks if the user would like to transfer from account1 to account2, transfer from account2 to account1, or quit. If a transfer is chosen, asks the amount of the transfer, carries out the operation, and prints the new balance for each account. Repeats until the user asks to quit, then prints a summary for each account.

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:

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

Modify your TransferTest class to use the static transfer instead of the instance version.

//*******************************************************

// FlexibleAccount.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 FlexibleAccount

{

private double balance;

private String name;

private long acctNum;

//----------------------------------------------

//Constructor -- initializes balance, owner, and account number

//----------------------------------------------

public FlexibleAccount(double balance, String name, long acctNum)

{

this.balance = balance;

this.name = name;

this.acctNum = acctNum;

}

//----------------------------------------------

// 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 a string containing the name, account number, and balance.

//----------------------------------------------

public String toString()

{

return "Name: " + name +

" Account Number: "+ acctNum +

" Balance: " + balance;

}

}

//******************************************************* 
// TestAccount.java 
// 
// A simple driver to test the overloaded methods of 
// the Account class. 
//******************************************************* 
 
import java.util.Scanner; 
 
public class TestAccount 
{ 
 public static void main(String[] args) 
 { 
 String name; 
 double balance; 
 long acctNum; 
 FlexibleAccount acct; 
 
 Scanner scan = new Scanner(System.in); 
 
 System.out.println("Enter account holder's first name"); 
 name = scan.next(); 
 acct = new FlexibleAccount(name); 
 System.out.println("Account for " + name + ":"); 
 System.out.println(acct); 
 
 System.out.println(" Enter initial balance"); 
 balance = scan.nextDouble(); 
 acct = new FlexibleAccount(balance,name); 
 System.out.println("Account for " + name + ":"); 
 System.out.println(acct); 
 
 System.out.println(" Enter account number"); 
 acctNum = scan.nextLong(); 
 acct = new FlexibleAccount(balance,name,acctNum); 
 System.out.println("Account for " + name + ":"); 
 System.out.println(acct); 
 
 System.out.print(" Depositing 100 into account, balance is now "); 
 acct.deposit(100); 
 System.out.println(acct.getBalance()); 
 System.out.print(" Withdrawing $25, balance is now "); 
 acct.withdraw(25); 
 System.out.println(acct.getBalance()); 
 System.out.print(" Withdrawing $25 with $2 fee, balance is now "); 
 acct.withdraw(25,2); 
 System.out.println(acct.getBalance()); 
 
 System.out.println(" Bye!"); 
 } 
} 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Describe how language reflects, builds on, and determines context?

Answered: 1 week ago

Question

=+Where do you want to live and work?

Answered: 1 week ago

Question

=+1 Where are the best places in the world to live (and work)?

Answered: 1 week ago