Code example

Let's consider an example of a banking application. In this banking application, we need to implement a scenario related to opening an account.

From a class implementation perspective, the following are the possible attributes that should be in the Account class. Please also note that there will be an additional class, Customer, to signify the person who is opening the account:

  • openingDate
  • customer
  • float currentBalance

The following are some of the methods that could be present in the Account class:

  • bool OpenAccount();
  • bool depositMoney(float deposit);
  • bool withdrawMoney(float withdrawalAmt);

In regard to the Customer class, we will just go simple now and define the following attributes:

  • string name
  • string customerId

Please refer to the following code for the declaration of the Customer class in a C# program. Here, we have created a Customer class and defined two attributes in it, that is, the name of the customer and a field of CustomerID, which will be a unique field for that customer.

In the following code, we will declare two variables and use them to showcase examples for the operators we mentioned previously:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
public class Customer
{
public string name;
public string customerId;
}
}

Please refer to the following code for the declaration of the Account class in a C# program:

public class Account
{
public DateTime openingDate;
public Customer customer;
private float currentBalance;
public bool OpenAccount(Customer customer)
{
this.openingDate = DateTime.Now.Date;
this.currentBalance = 0.0f;
this.customer = customer;
return true;
}
public bool DepositMoney(float deposit)
{
if(deposit > 0.0f)
{
this.currentBalance = this.currentBalance + deposit;
return true;
}
else
{
return false;
}
}
public bool WithdrawMoney(float withdraw)
{
if(this.currentBalance >= withdraw)
{
this.currentBalance = this.currentBalance - withdraw;
return true;
}
else
{
return false;
}
}
}
}

The following are some of the key items in the implementation:

  • In the Account class, note that currentBalance is marked as private, as a customer may not want their balance to be exposed to the entire application. 
  • In the Account class, in the methods of OpenAccount, DepositMoney, and WithdrawMoney, we are not passing all of the attributes related to the customer, current balance, or opening date. This is because the required attributes are already grouped together in the Account class.

Now, let's look at how we will invoke these classes:

Customer customer = new Customer();
customer.name = "Sample Customer";
customer.customerId = "12345";

Account newAccount = new Account();
newAccount.OpenAccount(customer);
newAccount.DepositMoney(1000);
newAccount.WithdrawMoney(400);

If you look at the function calling part, you will understand that because the properties are linked to the Account class, we are not passing them explicitly to the functions. Therefore, if the implementation of the functions is changed, from a maintenance perspective, there will be minimal impact. 

Therefore, it's always beneficial to use the principles of encapsulation and divide the application into chunks of classes with the related information.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.11.247