logo CodeStepByStep logo

BankAccount-TransactionFee

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
ba.name,
ba.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 TransactionFee that will be placed inside the BankAccount class to become a part of each BankAccount object's behavior. The TransactionFee method accepts a fee amount (a real number) as a parameter, and applies that fee to the user's past transactions. The fee is applied once for the first transaction, twice for the second transaction, three times for the third, and so on. These fees are subtracted out from the user's overall balance. If the user's balance is large enough to afford all of the fees with greater than $0.00 remaining, the method returns true. If the balance cannot afford all of the fees or has no money left, the balance is left as 0.0 and the method returns false.

For example, given the following BankAccount object:

BankAccount savings = new BankAccount("Jimmy");
savings.Deposit(10.00);
savings.Deposit(50.00);
savings.Deposit(10.00);
savings.Deposit(70.00);

The account at that point has a balance of $140.00. If the following call were made:

savings.TransactionFee(5.00);

Then the account would be deducted $5 + $10 + $15 + $20 for the four transactions, leaving a final balance of $90.00. The method would return true. If a second call were made,

savings.TransactionFee(10.00);

Then the account would be deducted $10 + $20 + $30 + $40 for the four transactions, leaving a final balance of $0.00. The method would return false.

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.