BTW, give this to your instructor and tell him to teach this stuff correctly, so experienced programmers already in the industry don't have to re-teach correct technique to new programmers fresh out of school. Just because it's a simple homework assignment does NOT mean the code he gives you can be crap.
This version of the
BankAcc
class allows for teachable moments for several aspects of .Net in general, and C#:
- basic class structure
- overloading constructors
- how fields and properties work together
- how regions are used for code organization
- how to use Intellisense comments, and their importance
- using return values in methods
- return void in methods
- sanity checking parameters and throwing exceptions
- industry-accepted best practice with object naming, and code commenting/formatting
- separation of concerns - avoid putting UI or presentation code in an entity
public class BankAccount
{
#region fields
private string firstName = "";
private string lastName = "";
private decimal balance;
#endregion fields
#region properties
public string FirstName
{
get { return this.firstName; }
set { this.firstName = value; }
}
public string LastName
{
get { return this.lastName; }
set { this.lastName = value; }
}
public decimal Balance
{
get { return this.balance; }
set { this.balance = value; }
}
#endregion properties
#region constructors
public BankAccount(string firstName, string lastName) : this(firstName, lastName, 50000)
{
}
public BankAccount(string firstName, string lastName, decimal balance)
{
if (string.IsNullOrEmpty(firstName))
{
throw new ArgumentNullException("firstName");
}
if (string.IsNullOrEmpty(lastName))
{
throw new ArgumentNullException("lastName");
}
this.FirstName = firstName;
this.LastName = lastName;
this.Balance = balance;
}
#endregion constructors
#region methods
public void MakeDeposit(decimal amount)
{
this.Balance += amount;
}
public bool MakeWithdrawal(decimal amount)
{
bool result = false;
if (amount <= this.Balance)
{
this.Balance -= amount;
result = true;
}
return result;
}
#endregion fields
}