logo CodeStepByStep logo


Language/Type: C# classes instance methods
Related Links:

Suppose that you are provided with a pre-written class BankAccount as shown below. (The headings are shown, but not the method bodies, to save space.) Assume that the fields, constructor, properties, and methods shown are already implemented. You may refer to them or use them in solving this problem if necessary.

member name type description
new BankAccount(name) constructor constructs a new account for the person with the given name, with $0.00 balance
fields private data storing account's name and balance
ba.Name property the account name as a string (read-only)
ba.Balance property the account balance as a real number (read-only)
ba.Deposit(amount); method adds the given amount of money, as a real number, to the account balance; if the amount is negative, does nothing
ba.Withdraw(amount); method subtracts the given amount of money, as a real number, from the account balance; if the amount is negative or exceeds the account's balance, does nothing

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 $5.00 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 $5 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 $5 fee is deducted. If this account has under $5 or the amount is 0 or less, no transfer should occur and neither account's state should be modified.

For example, given the following BankAccount objects:

BankAccount ben = new BankAccount("Benson");
BankAccount mar = new BankAccount("Marty");

Assuming that the following calls were made, the balances afterward are shown in comments to the right of each call:

ben.Transfer(mar, 20.00);     // ben $65, mar $45   (ben loses $25, mar gains $20)
ben.Transfer(mar, 10.00);     // ben $50, mar $55   (ben loses $15, mar gains $10)
ben.Transfer(mar, -1);        // ben $50, mar $55   (no effect; negative amount)
mar.Transfer(ben, 39.00);     // ben $89, mar $11   (mar loses $44, ben gains $39)
mar.Transfer(ben, 50.00);     // ben $95, mar $ 0   (mar loses $11, ben gains $ 6)
mar.Transfer(ben,  1.00);     // ben $95, mar $ 0   (no effect; no money in account)
ben.Transfer(mar, 88.00);     // ben $ 2, mar $88   (ben loses $93, mar gains $88)
ben.Transfer(mar,  1.00);     // ben $ 2, mar $88   (no effect; can't afford fee)
Partial class: Write code that will become part of an existing class as described. You do not need to write the complete class, just the portion described in the exercise.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.