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

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

Getting started on the banking app

So recently I was twirling round on Freelancer(I know, I know, I should know better) looking at coding projects that I could do. And I came across a simple banking app project and I thought, what the hay. Right? I have recently rekindled my love for Visual Studio and the Microsoft family of programming languages and I have been refreshing my love for Visual C# though I am mainly a Visual Basic developer.
So I thought this would be a real easy and fun test to try out my love for C# and programming! Yay! So I started... 
Easy requirements
The project required no database tasks(phew!) but store the data onto a text file (👀) and I was thinking why not a .xml file? Anyway here are the requirements.
  1. Capture client details such as Account number, first name, last name and account balance.
  2. Perform a client search using an account number and display the results in a RichTextBox.
  3. Display clients that that have positive balances (debits)
  4. Display clients that have negative balances(credits)
  5. Display clients that have zero balances
  6. Allow the user to make a deposit and update the balances and save the details onto the text file
  7. Allow the user to create new clients and save the details onto the text file
I thought simple enough so I got started by modeling the business logic using classes.
1. First I created a Visual C# WinForms Project. Should be straight forward enough to do this:



Creating the business functionality BankAccount.cs

After creating the project I went on to create a BankAccount class (Project Menu> Add Class..- name the class BankAccount.cs)
The properties naturally will be:
Private data members to hold the state of the object:
private string m_firstName;
private string m_lastName;
private string m_accountNumber;
private decimal m_accountBalance;
 Property methods: -  Visual studio complained incessantly about variable naming conventions
public string accountNumber
{
    get{return m_accountNumber;}
    set{m_accountNumber = value;}
}
public string firstName
{
    get{return m_firstName;}
    set{m_firstName = value;}
}
public string lastName
{
    get{return m_lastName;}
    set { m_lastName = value; }
}
public decimal accountBalance
{
    get{return m_accountBalance;}
    set{m_accountBalance = value;}
I added this property method just in case--Like all good programmers do
public string FullName
{
    get { return m_firstName + ", " + m_lastName; }
}
The Class Methods
Naturally according to the requirements these would be WithDraw and Deposit
WithDraw takes in a withdrawalAmount and deducts it from the balance
public decimal WithDraw(decimal withdrawalAmount)
{
    m_accountBalance -= withdrawalAmount;
    return accountBalance;
}
Deposit takes in a depositAmount and adds it to the balance
public decimal Deposit(decimal depositAmount)
{
    m_accountBalance += depositAmount;
    return accountBalance;
}

Part 2