❔ OOP Help

BankAccount.cs
namespace Classes;

public class BankAccount
{
    // These are fields / properties / characteristics
    public string Number { get; set; }
    public string Owner { get; set; }
    public decimal Balance {
        get
        {
            decimal balance = 0;
            foreach (var item in allTransactions)
            {
                balance += item.Amount;
            }
            return balance;
        }
    }
    
    // private = it can only be accessed by the code in THIS class (BankAccount)
    // static = this is shared by all of the BankAccount objects
    private static int accountNumberSeed = 1234567890;

    private List<Transaction> allTransactions = new List<Transaction>();

    // This is a constructor to assign values / initialize objects of this class type
    public BankAccount(string name, decimal initialBalance)
    {
        Number = accountNumberSeed.ToString();
        accountNumberSeed++;

        Owner = name;
        // MakeDeposit(initialBalance, DateTime.Now, "Initial balance");
    }

    // These are methods 
    public void MakeDeposit(decimal amount, DateTime date, string note)
    {
        if (amount <= 0)
        {
            throw new ArgumentOutOfRangeException(nameof(amount), "Amount of deposit must be positive");
            var deposit = new Transaction(amount, date, note);
            allTransactions.Add(deposit);
        }
    }

    public void MakeWithdrawal(decimal amount, DateTime date, string note)
    {
        if (amount <= 0)
        {
            throw new ArgumentOutOfRangeException(nameof(amount), "Amount of withdrawal must be positive");
        }
        if (Balance - amount < 0)
        {
            throw new InvalidOperationException("Not enough sufficient funds for this withdrawal");
        }
        var withdrawal = new Transaction(-amount, date, note);
        allTransactions.Add(withdrawal);
    }
}
Was this page helpful?