Wednesday, 10 October 2018

Creating a simple WinForm banking app in C# Classes and objects Part2

Part 2 of the banking app

I also overrode the ToString() method that is ubiquitous in every .Net object

public override string ToString()
{
    string fullDetails;
    fullDetails = m_accountNumber + "," + m_firstName + "," +
                  lastName + "," + m_accountBalance.ToString();
    return fullDetails;
}

The constructors
I created two constructors one with parameters that takes in the account details such as first name, last name, account number and account balance.
public BankAccount(string fName, string lName, string acNumber, decimal acBalance)
{
    //initialize the account when the object is created
    m_firstName = fName;
    m_lastName = lName;
    m_accountNumber = acNumber;
    m_accountBalance = acBalance;
}
The second was a parameter-less constructor that allows List of bank account 
(List<BankAccount> AccountsList = new List<BankAccount>();)
to be created on the winform that consumes the bank account class (covered later)
public BankAccount()
{
    //parameterless constructor
}
Getting carried away
Naturally you would want to record all the transactions occurring on the account class that is all the deposits and withdrawals and account opening transaction
The Transaction class
The class would have three members(state): Transaction Type, Transaction Amount and Timestamp
private TransactionType m_transType;
private decimal m_transAmount;
private string m_timestamp;
The transaction type is an enum that allows you to enumerate the kind of transaction being carried out
public enum TransactionType
{
    Deposit=1,
    Withdrawal,
    OpeningBalance
}
 
Property methods
public string Timestamp
{
    get { return m_timestamp; }
    set { m_timestamp = value; }
}
public TransactionType TransType
{
    get { return m_transType; }
    set { m_transType = value; }
}
public decimal TransactionAmount
{
    get { return m_transAmount; }
    set { m_transAmount = value; }
}
 
The m_transAmount is the amount transacted, the m_timestamp is the date and time of transaction. Pretty simple huh?

Methods
Not many methods although this can be enhanced in the remote chance that this becomes a more complex app
Overriding the ToString() method
Very useful when you want to return the state of the object in a string format
public override string ToString()
{
    string res = "";
    res = m_timestamp + ":  " + m_transType.ToString() + " -: " + m_transAmount.ToString();
    return res;
}

And finally the constructor – Parameter – less because we want to be able to create a List<Transaction>( a list of transactions in each bank account class)
public Transaction()
{
    //parameterless constructor
}
 
 
So the whole Transaction class would look like this
  public class Transaction
    {
        public Transaction()
        {
            //parameterless constructor
        }
        public override string ToString()
        {
            string res = "";
            res = m_timestamp + ":  " + m_transType.ToString() + " -: " + m_transAmount.ToString();
            return res;
        }
        public enum TransactionType
        {
            Deposit=1,
            Withdrawal,
            OpeningBalance
 
        }
        private TransactionType m_transType;
        private decimal m_transAmount;
        private string m_timestamp;
        public string Timestamp
        {
            get { return m_timestamp; }
            set { m_timestamp = value; }
        }
        public TransactionType TransType
        {
            get { return m_transType; }
            set { m_transType = value; }
        }
        public decimal TransactionAmount
        {
            get { return m_transAmount; }
            set { m_transAmount = value; }
        }
    }
}

Now Going back to the Account class we enhance the class be declaring the list of transactions List<Transaction> as a member like so

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

Then add a property method to return a list of transactions for each BankAccount object created. Like so
public List<Transaction> Transactions
{
    get { return m_transactions;}
}

We then enhance the Constructor, WithDraw and Deposit Methods in order to record a transaction each time the user transacts

The enhanced constructor
public BankAccount(string fName, string lName, string acNumber, decimal acBalance)
{
    //initialize the account when the object is created
    m_firstName = fName;
    m_lastName = lName;
    m_accountNumber = acNumber;
    m_accountBalance = acBalance;
    Transaction trans = new Transaction();
    trans.TransType = Transaction.TransactionType.OpeningBalance;
    trans.Timestamp = DateTime.UtcNow.ToString();
    trans.TransactionAmount = acBalance;
    m_transactions.Add(trans);
}
Because opening an account is essentially a transaction type of OpeningBalance we create a transaction object, give it the AcBalance as the transaction amount, the timestamp now and we mark the transaction as OpeningBalance. We then add the transaction into the list of transactions of the account

m_transactions.Add(trans);

The WithDrawMethod
public decimal WithDraw(decimal withdrawalAmount)
{
    Transaction trans = new Transaction();
    trans.TransType = Transaction.TransactionType.Withdrawal;
    trans.TransactionAmount = withdrawalAmount;
    trans.Timestamp = DateTime.UtcNow.ToString();
    m_transactions.Add(trans);
    m_accountBalance -= withdrawalAmount;
    return accountBalance;
}

As we have done in the constructor we enhance the WithDraw method by declaring a transaction object, the transaction type is Withdrawal, the TransactionAmount is withdrawalAmount and the time of transaction is the time stamp

The DepositMethod
public decimal Deposit(decimal depositAmount)
{
    Transaction trans = new Transaction();
    trans.TransType = Transaction.TransactionType.Deposit;
    trans.TransactionAmount = depositAmount;
    trans.Timestamp = DateTime.UtcNow.ToString();
    m_transactions.Add(trans);
    m_accountBalance += depositAmount;
    return accountBalance;
}
The transaction type is Deposit

The Enhanced ToString() Method

We want the ToString() method to list the state of the object i.e. The account number, first name, last name, account balance as well as list the transactions carried out on the BankAccount
public override string ToString()
{
    string fullDetails;
    fullDetails = m_accountNumber + "," + m_firstName + "," +
                  lastName + "," + m_accountBalance.ToString();
    foreach(Transaction trans in m_transactions)
    {
        fullDetails += "\n  --" + trans.ToString();
    }
    return fullDetails;
}
So we loop through the m_transactions member and use the trans.ToString method to return the object  state as a string. We then concatenate it with the BankAccount state. Very nifty no?

Part 3 Follows

No comments:

Post a Comment