Levi0227

sztf 01

Sep 16th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.24 KB | Source Code | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SZTF01
  8. {
  9.     internal class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             BankAccount account = new BankAccount();
  14.             AccountMonitor accountMonitor = new AccountMonitor(account);
  15.  
  16.             account.Deposited += accountMonitor.OnDeposited;
  17.             account.Withdrawn += accountMonitor.OnWithdrawn;
  18.  
  19.             account.Deposit(5000);
  20.             account.Withdraw(300);
  21.             account.Withdraw(5300);
  22.             account.Deposit(85000);
  23.             account.Withdraw(75000);
  24.             account.Withdraw(14800);
  25.  
  26.             Console.ReadKey();
  27.         }
  28.     }
  29. }
  30. //Feladat: Készíts egy egyszerű banki rendszert, amely demonstrálja a beépített delegáltakat és eseményeket. A rendszernek figyelnie kell a pénzmozgásokat,
  31. //és értesítéseket kell küldenie a tranzakciókról.
  32.  
  33. //BankAccount Osztály:
  34.  
  35. //Tulajdonságok:
  36. //Balance(a számla egyenlege)
  37. //Események:
  38. //Deposited(amikor pénz kerül hozzáadásra a számlára)
  39. //Withdrawn(amikor pénz kerül levonásra a számláról)
  40. //Delegáltak:
  41. //Használj beépített delegáltakat(Action<decimal>) az eseményekhez.
  42. //Metódusok:
  43. //Deposit(decimal amount)(pénz hozzáadása)
  44. //Withdraw(decimal amount)(pénz levonása)
  45.  
  46. //AccountMonitor Osztály:
  47.  
  48. //Funkciók:
  49. //Figyelj a Deposited és Withdrawn eseményekre.
  50. //Naplózza a tranzakciókat és értesítéseket küld a konzolra, amikor egy tranzakció történik.
  51.  
  52. //Program Osztály:
  53.  
  54. //Létrehoz egy BankAccount és egy AccountMonitor példányt.
  55. //Regisztrálja a monitor-t a számla eseményeire.
  56. //Végezz el néhány tranzakciót, és figyeld meg, hogyan reagál a rendszer.
  57.  
  58. using System;
  59. using System.Collections.Generic;
  60. using System.Linq;
  61. using System.Text;
  62. using System.Threading.Tasks;
  63.  
  64. namespace SZTF01
  65. {
  66.     public class BankAccount
  67.     {
  68.         public decimal Balance { get; set; }
  69.  
  70.         public event Action<decimal> Deposited;
  71.         public event Action<decimal> Withdrawn;
  72.  
  73.         public void Deposit(decimal amount)
  74.         {
  75.             Balance += amount;
  76.  
  77.             Deposited?.Invoke(amount);
  78.         }
  79.        
  80.         public void Withdraw(decimal amount)
  81.         {
  82.             if (Balance >= amount)
  83.             {
  84.                 Balance -= amount;
  85.             }
  86.             else
  87.             {
  88.                 Console.WriteLine("Nincs elég pénz a számlán");
  89.                 return;
  90.             }
  91.  
  92.             Withdrawn?.Invoke(amount);
  93.         }
  94.     }
  95. }
  96.  
  97. using System;
  98. using System.Collections.Generic;
  99. using System.Linq;
  100. using System.Text;
  101. using System.Threading.Tasks;
  102.  
  103. namespace SZTF01
  104. {
  105.     public class AccountMonitor
  106.     {
  107.         public AccountMonitor(BankAccount bankAccount)
  108.         {
  109.             bankAccount.Deposited += OnDeposited;
  110.             bankAccount.Withdrawn += OnWithdrawn;
  111.         }
  112.  
  113.         public void OnDeposited(decimal amount)
  114.         {
  115.             Console.WriteLine($"BEFIZETÉS: +{amount}");
  116.         }
  117.         public void OnWithdrawn(decimal amount)
  118.         {
  119.             Console.WriteLine($"KIFIZETÉS: -{amount}");
  120.         }
  121.  
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment