Skip to content

Instantly share code, notes, and snippets.

@sjwaight
Created September 25, 2023 06:43
Show Gist options
  • Save sjwaight/27e4b3768db6cec4f3d715c8725bd752 to your computer and use it in GitHub Desktop.
Save sjwaight/27e4b3768db6cec4f3d715c8725bd752 to your computer and use it in GitHub Desktop.
Sample Bank Account C# class
namespace BankAccountNS
{
/// <summary>
/// Bank account demo class.
/// </summary>
public class BankAccount
{
private readonly string m_customerName;
private double m_balance;
private BankAccount() { }
public BankAccount(string customerName, double balance)
{
m_customerName = customerName;
m_balance = balance;
}
public string CustomerName
{
get { return m_customerName; }
}
public double Balance
{
get { return m_balance; }
}
public void Debit(double amount)
{
if (amount > m_balance)
{
throw new ArgumentOutOfRangeException("amount");
}
if (amount < 0)
{
throw new ArgumentOutOfRangeException("amount");
}
m_balance += amount; // intentionally incorrect code
}
public void Credit(double amount)
{
if (amount < 0)
{
throw new ArgumentOutOfRangeException("amount");
}
m_balance += amount;
}
public static void Main()
{
BankAccount ba = new BankAccount("Mr. Bryan Walton", 11.99);
ba.Credit(5.77);
ba.Debit(11.22);
Console.WriteLine("Current balance is ${0}", ba.Balance);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment