Question: Java Code: Suppose that you are provided with a pre - written class BankAccount as shown below. ( The headings are shown, but not the
Java Code: Suppose that you are provided with a prewritten class BankAccount as shown below. The headings are shown, but not the method bodies, to save space. Assume that the fields, constructor, and methods shown are already implemented. You may refer to them or use them in solving this problem if necessary.
Write a method named transfer that will be placed inside the BankAccount class to become a part of each BankAccount object's behavior. The transfer method moves money from this bank account to another account. The method accepts two parameters: a second BankAccount to accept the money, and a real number for the amount of money to transfer.
There is a $ fee for transferring money, so this much must be deducted from the current account's balance before any transfer.
The method should modify the two BankAccount objects such that "this" current object has its balance decreased by the given amount plus the $ fee, and the other BankAccount object's balance is increased by the given amount. A transfer also counts as a transaction on both accounts.
If this account object does not have enough money to make the full transfer, transfer whatever money is left after the $ fee is deducted. If this account has under $ or the amount is or less, no transfer should occur and neither account's state should be modified.
A BankAccount keeps track of a user's money balance and ID
and counts how many transactions depositswithdrawals are made.
public class BankAccount
private String id;
private double balance;
private int transactions;
Constructs a BankAccount object with the given id and
balance and transactions.
public BankAccountString id
returns the field values
public double getBalance
public String getID
public String getTransactions
Adds the amount to the balance if it is between
Also counts as transaction.
public void depositdouble amount
Subtracts the amount from the balance if the user has enough money.
Also counts as transaction.
public void withdrawdouble amount
Ja your method would go here
For example, given the following BankAccount objects:
BankAccount ben new BankAccountBenson;
ben.deposit;
BankAccount mar new BankAccountMarty;
mar.deposit;
Assuming that the following calls were made, the balances afterward are shown in comments to the right of each call:
ben.transfermar; ben $ mar $ben loses $ mar gains $
ben.transfermar; ben $ mar $ben loses $ mar gains $
ben.transfermar; ben $ mar $no effect; negative amount
mar.transferben; ben $ mar $mar loses $ ben gains $
mar.transferben; ben $ mar $ mar loses $ ben gains $
mar.transferben; ben $ mar $ no effect; no money in account
ben.transfermar; ben $ mar $ben loses $ mar gains $
ben.transfermar; ben $ mar $no effect; can't afford fee
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
