Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SZTF01
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- BankAccount account = new BankAccount();
- AccountMonitor accountMonitor = new AccountMonitor(account);
- account.Deposited += accountMonitor.OnDeposited;
- account.Withdrawn += accountMonitor.OnWithdrawn;
- account.Deposit(5000);
- account.Withdraw(300);
- account.Withdraw(5300);
- account.Deposit(85000);
- account.Withdraw(75000);
- account.Withdraw(14800);
- Console.ReadKey();
- }
- }
- }
- //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,
- //és értesítéseket kell küldenie a tranzakciókról.
- //BankAccount Osztály:
- //Tulajdonságok:
- //Balance(a számla egyenlege)
- //Események:
- //Deposited(amikor pénz kerül hozzáadásra a számlára)
- //Withdrawn(amikor pénz kerül levonásra a számláról)
- //Delegáltak:
- //Használj beépített delegáltakat(Action<decimal>) az eseményekhez.
- //Metódusok:
- //Deposit(decimal amount)(pénz hozzáadása)
- //Withdraw(decimal amount)(pénz levonása)
- //AccountMonitor Osztály:
- //Funkciók:
- //Figyelj a Deposited és Withdrawn eseményekre.
- //Naplózza a tranzakciókat és értesítéseket küld a konzolra, amikor egy tranzakció történik.
- //Program Osztály:
- //Létrehoz egy BankAccount és egy AccountMonitor példányt.
- //Regisztrálja a monitor-t a számla eseményeire.
- //Végezz el néhány tranzakciót, és figyeld meg, hogyan reagál a rendszer.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SZTF01
- {
- public class BankAccount
- {
- public decimal Balance { get; set; }
- public event Action<decimal> Deposited;
- public event Action<decimal> Withdrawn;
- public void Deposit(decimal amount)
- {
- Balance += amount;
- Deposited?.Invoke(amount);
- }
- public void Withdraw(decimal amount)
- {
- if (Balance >= amount)
- {
- Balance -= amount;
- }
- else
- {
- Console.WriteLine("Nincs elég pénz a számlán");
- return;
- }
- Withdrawn?.Invoke(amount);
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SZTF01
- {
- public class AccountMonitor
- {
- public AccountMonitor(BankAccount bankAccount)
- {
- bankAccount.Deposited += OnDeposited;
- bankAccount.Withdrawn += OnWithdrawn;
- }
- public void OnDeposited(decimal amount)
- {
- Console.WriteLine($"BEFIZETÉS: +{amount}");
- }
- public void OnWithdrawn(decimal amount)
- {
- Console.WriteLine($"KIFIZETÉS: -{amount}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment