logo CodeStepByStep logo

BankAccount-ToString

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

Add the following method to the BankAccount class:

public string ToString()

Your method should return a string that contains the account's name and balance separated by a comma and space. For example, if an account object named benben has the name "Benson" and a balance of 17.25, the call of benben.ToString() should return:

Benson, $17.25

There are some special cases you should handle. If the balance is negative, put the - sign before the dollar sign. Also, always display the cents as a two-digit number. For example, if the same object had a balance of -17.5, your method should return:

Benson, -$17.50

Your code is being added to the following class:

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