La interfaz es una clase especial en la que podemos declarar todos nuestros métodos. Aquí, en este problema, vamos a crear una interfaz en la que vamos a declarar todas las implementaciones requeridas que son necesarias para la gestión de transacciones. Aquí, en este artículo, vamos a ver cómo funcionan las transacciones en tiempo real y cómo podemos implementarlas usando la interfaz en C#.
Nuestra interfaz contiene los siguientes métodos:
void addDeposit(int deposit_money); int withdrawCash(int req_cash); int checkBalance();
Ejemplo 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("Withdrawl 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; } } } } }
Producción: