Open In App

Demonstrating Transactions Using Interface Through C#

Improve
Improve
Like Article
Like
Save
Share
Report

The interface is a special class in which we can declare all of our methods. Here in this problem, we are going to create an interface in which we are going to declare all of the required implementations which is necessary for transaction management. Here in this article, we are going to see how real-time transaction works and how we can implement that by using the interface in C#

Our interface contains the following methods:

void addDeposit(int deposit_money);
int withdrawCash(int req_cash);
int checkBalance();

Example 1: 

C#




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace transactions
{
    internal class Program
    {
        private static string ac_no = "12345678";
        static void Main(string[] args)
        {
             
            Account account = new Account();
            account.SetAccount(ac_no);
            account.addDeposit(123,ac_no);
            int bal = account.checkBalance(ac_no);
            Console.WriteLine("your current balance is " + bal);
            int status = account.withdrawCash(12, ac_no);
            if(status != -1)
            {
                Console.WriteLine("Withdrawal successfull");
                int bal1 = account.checkBalance(ac_no);
                Console.WriteLine("your current balance is " + bal1);
            }
             
            account.withdrawCash(12233, ac_no);
            Console.ReadLine();
        }
 
        public interface ITransaction
        {
            void addDeposit(int deposit_money, string ac_no);
            int withdrawCash(int req_cash, string ac_no);
            int checkBalance(string ac_no);
        }
 
        public class Account : ITransaction
        {
            string account_number;
            int total_cash = 0;
 
 
            //Here we can add account number to database
            public void SetAccount(string ac_no)
            {
                this.account_number = ac_no;
            }
             
 
            public bool CheckAccountNumber(string ac_no)
            {
                //we can fetch data from database
                if(ac_no == this.account_number)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
 
 
            public void addDeposit(int deposit_money,string ac_no)
            {
                if(CheckAccountNumber(ac_no) == true)
                {
                    total_cash += deposit_money;
                    Console.WriteLine("Rs "+ deposit_money +
                                      " deposit successfully");
                }
                else
                {
                    Console.WriteLine("You entered wrong account number");
                }
            }
 
            
 
            public int checkBalance(string ac_no)
            {
                if (CheckAccountNumber(ac_no) == true)
                {
                    return this.total_cash;
                }
                else
                {
                    Console.WriteLine("You entered wrong account number");
                    return -1;
                }
            }
 
            public bool isEnoughCash(int req_cash)
            {
                return (total_cash >= req_cash);
            }
 
            public int withdrawCash(int req_cash,string ac_no)
            {
                if(CheckAccountNumber(ac_no) == true)
                {
                    if (isEnoughCash(req_cash) == true)
                    {
                        total_cash -= req_cash;
                        return req_cash;
                    }
                    else
                    {
                        Console.WriteLine("You don't have required cash,
                        please deposit money in your account");
                        return -1;
                    }
                }
                else
                {
                    Console.WriteLine("You entered wrong account number");
                    return -1;
                }
            }
        }
    }
}


Output:

 



Last Updated : 02 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads