Codiqo

Learn C# · lesson 41 of 61 · about 10 minutes

Project: Bank Account

Open the guild bank: build an account class with deposits, withdrawals and a transaction history.

What you'll learn

  • Combine fields, properties and methods
  • Keep a private list of transactions
  • Refuse invalid operations

The guild bank

The guild keeps a bank account in the hero's name. Every other member pays in a tenth of their XP, and then the hero takes 20 out for repairs. The Account has a read-only Owner, a Balance that only the class can change, and a private history list. Deposit() and Withdraw() refuse amounts that aren't above 0, and withdrawals bigger than the balance, and return whether they happened, so the bank can report refusals like Refused: withdraw 20. Every change goes into the history, and Statement() shows it, like Ada: +12 +4 +8 -20 = 4.

The design

BankAccount has:

  • Owner (read-only) and Balance (private set)
  • Deposit and Withdraw that check the amount and return true or false
  • A private List<string> history, shown by PrintStatement

Money uses decimal.

Step 1: a total only the class can change

var j = new Jar();
j.Add(10);
j.Add(-3);
Console.WriteLine(j.Sweets);

class Jar
{
    public int Sweets { get; private set; }

    public void Add(int amount)
    {
        if (amount > 0) Sweets += amount;
    }
}

Output

10

Money uses decimal

decimal keeps amounts like 30.50 exact, which double can't always do. Each change is written to the history as it happens, and refusals are recorded too.

The account

var acc = new BankAccount("Ada");
acc.Deposit(100m);
acc.Withdraw(30.50m);
acc.Withdraw(500m);
acc.Deposit(-5m);
acc.PrintStatement();

class BankAccount
{
    private readonly List<string> _history = new();
    public string Owner { get; }
    public decimal Balance { get; private set; }

    public BankAccount(string owner) => Owner = owner;

    public bool Deposit(decimal amount)
    {
        if (amount <= 0) return Log($"Rejected deposit of {amount:F2}");
        Balance += amount;
        return Log($"Deposit  +{amount,7:F2}", true);
    }

    public bool Withdraw(decimal amount)
    {
        if (amount <= 0 || amount > Balance) return Log($"Rejected withdrawal of {amount:F2}");
        Balance -= amount;
        return Log($"Withdraw -{amount,7:F2}", true);
    }

    private bool Log(string entry, bool ok = false)
    {
        _history.Add(entry);
        return ok;
    }

    public void PrintStatement()
    {
        Console.WriteLine($"Statement for {Owner}");
        foreach (string entry in _history) Console.WriteLine(entry);
        Console.WriteLine($"Balance: £{Balance:F2}");
    }
}

Output

Statement for Ada
Deposit  + 100.00
Withdraw -  30.50
Rejected withdrawal of 500.00
Rejected deposit of -5.00
Balance: £69.50

Now try it yourself

This lesson is free in Codiqo, with 9 questions on what you just read. You can only learn to code by writing code, so it ends with a short program you write yourself, which we run and check straight away.