Wednesday, 10 October 2018

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







No comments:

Post a Comment