Demonstrating Transactions Using Interface Through C# Last Updated : 02 Aug, 2022 Comments Improve Suggest changes Like Article Like 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: Comment More infoAdvertise with us Next Article C# Program to Demonstrate Interface Implementation with Multi-level Inheritance S sumu4034 Follow Improve Article Tags : C# Similar Reads C# Program For Implementing IEnumerable Interface Using LINQ LINQ is known as Language Integrated Query and it is introduced in .NET 3.5. It gives the ability to .NET languages to generate queries to retrieve data from the data source. It removes the mismatch between programming languages and databases and the syntax used to create a query is the same no matt 3 min read C# | Multiple inheritance using interfaces Introduction:Multiple inheritance refers to the ability of a class to inherit from multiple base classes. C# does not support multiple inheritance of classes, but it does supportmultiple inheritance using interfaces. An interface is a collection of abstract methods that a class can implement to prov 7 min read C# Program to Implement an Interface in a Structure Structure is a value type and a collection of variables of different data types under a single unit. It is almost similar to a class because both are user-defined data types and both hold a bunch of different data types. We can create a structure by using struct keyword. A structure can also hold co 2 min read C# Program to Demonstrate Interface Implementation with Multi-level Inheritance Multilevel Inheritance is the process of extending parent classes to child classes in a level. In this type of inheritance, a child class will inherit a parent class, and as well as the child class also act as the parent class to other class which is created. For example, three classes called P, Q, 3 min read C# | How to use Interface References In C#, you are allowed to create a reference variable of an interface type or in other words, you are allowed to create an interface reference variable. Such kind of variable can refer to any object that implements its interface. An interface reference variable only knows that methods which are decl 5 min read C# Program To Implement IDisposable Interface IDisposable is an interface defined in the System namespace. It is used to release managed and unmanaged resources. Implementing IDisposable interface compels us to implement 2 methods and 1 boolean variable -Â Public Dispose() : This method will be called by the consumer of the object when resource 4 min read Like