From 6b8f756b4383723927bc80d77a99f5335b52ab7a Mon Sep 17 00:00:00 2001 From: Maksim Date: Tue, 2 Dec 2025 00:14:18 +0300 Subject: [PATCH 1/2] =?UTF-8?q?=D0=B7=D0=B0=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kt8888/Program.cs | 115 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 81 insertions(+), 34 deletions(-) diff --git a/kt8888/Program.cs b/kt8888/Program.cs index dce78d0..32cc5aa 100644 --- a/kt8888/Program.cs +++ b/kt8888/Program.cs @@ -1,46 +1,52 @@ using System; -using System.IO; +using System.Collections.Generic; namespace kt8888 { - public class BankAccount + public class Button { - private decimal balance; + private string text; + private EventHandler clickEvent; + private List subscribers = new List(); - public decimal Balance + public string Text { - get { return balance; } - private set - { - balance = value; - BalanceChanged?.Invoke(balance); - } + get { return text; } + set { text = value; } } - public event Action BalanceChanged; - - public void Deposit(decimal amount) + public event EventHandler Click { - Balance += amount; - } + add + { + if (subscribers.Count >= 3) + { + Console.WriteLine("cannot add more than 3 subscribers"); + return; + } - public void Withdraw(decimal amount) - { - if (amount <= Balance) + if (subscribers.Contains(value)) + { + Console.WriteLine("this subscriber is already added"); + return; + } + + subscribers.Add(value); + clickEvent += value; + Console.WriteLine("subscriber added successfully"); + } + remove { - Balance -= amount; + subscribers.Remove(value); + clickEvent -= value; + Console.WriteLine("subscriber removed successfully"); } } - } - public class Logger - { - public Logger(BankAccount account) + public void SimulateClick() { - account.BalanceChanged += (balance) => - { - File.AppendAllText("log.txt", $"balance {balance}\n"); - }; + Console.WriteLine($"\nbutton '{Text}' clicked"); + clickEvent?.Invoke(this, EventArgs.Empty); } } @@ -48,15 +54,56 @@ class Program { static void Main() { - BankAccount account1 = new BankAccount(); - Logger logger1 = new Logger(account1); + Button button1 = new Button(); + button1.Text = "button"; + + void PrintText(object sender, EventArgs e) + { + Console.WriteLine("button text " + ((Button)sender).Text); + } + + void ChangeColor(object sender, EventArgs e) + { + Console.WriteLine("changing button color to red"); + } + + void ShowMessage(object sender, EventArgs e) + { + Console.WriteLine("button was clicked"); + } + + void FourthSubscriber(object sender, EventArgs e) + { + Console.WriteLine("this is the fourth subscriber not work"); + } + + // подписка методы + Console.WriteLine(); + button1.Click += PrintText; + button1.Click += ChangeColor; + button1.Click += ShowMessage; + + // четвертый подписчик + Console.WriteLine(); + button1.Click += FourthSubscriber; + + // существующий подписчик + Console.WriteLine(); + button1.Click += PrintText; + + // нажатие кнопки + Console.WriteLine(); + button1.SimulateClick(); + + // удаление и добавление подписчикв + Console.WriteLine(); + button1.Click -= ShowMessage; + button1.Click += FourthSubscriber; - account1.Deposit(1000); - account1.Withdraw(500); - account1.Deposit(300); + //нажатие кнопки + Console.WriteLine(); + button1.SimulateClick(); - Console.WriteLine("final balance " + account1.Balance); - Console.ReadLine(); } } } \ No newline at end of file From 2f45b4fb9c9962e033f50072ead314a718b6172e Mon Sep 17 00:00:00 2001 From: SLAMPER <50207606+SLAMPER@users.noreply.github.com> Date: Tue, 2 Dec 2025 14:55:28 +0300 Subject: [PATCH 2/2] Create README.md --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..35d1f8d --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +Задание 3 + +Задание: Напишите класс Button, который имеет свойство Text типа string и событие типа EventHandler с именем Click, которое возникает при нажатии на кнопку. Затем напишите аксессоры для этого события: add и remove, которые контролируют, сколько подписчиков может иметь это событие. Например, вы можете ограничить максимальное количество подписчиков до трех или запретить добавлять один и тот же подписчик дважды. Затем напишите несколько методов, которые могут быть подписаны на это событие, например, выводящие на консоль текст кнопки или меняющие его цвет. Продемонстрируйте работу этих методов в методе Main. + +Результат: + +изображение