Демонстрация транзакций с использованием интерфейса через C#
Опубликовано: 10 Января, 2023
Интерфейс — это специальный класс, в котором мы можем объявить все наши методы. Здесь, в этой задаче, мы собираемся создать интерфейс, в котором мы собираемся объявить все необходимые реализации, необходимые для управления транзакциями. Здесь, в этой статье, мы увидим, как работает транзакция в реальном времени и как мы можем реализовать это, используя интерфейс в C#.
Наш интерфейс содержит следующие методы:
void addDeposit(int deposit_money); int withdrawCash(int req_cash); int checkBalance();
Пример 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; } } } }} |
Выход: