From 772c7d24485bea2d6ded41a0e22d0f78f865ab7f Mon Sep 17 00:00:00 2001 From: Ikhsanovak <45394957+Ikhsanovak@users.noreply.github.com> Date: Thu, 28 Feb 2019 20:38:53 +0300 Subject: [PATCH] Add files via upload --- Product.cs | 162 ++++++++++++++++++++++++-------------------- ProductEventArgs.cs | 76 ++++++++++++++++----- Program.cs | 94 +++++++++++++++---------- 3 files changed, 202 insertions(+), 130 deletions(-) diff --git a/Product.cs b/Product.cs index f68c8c9..fcdaf96 100644 --- a/Product.cs +++ b/Product.cs @@ -1,75 +1,87 @@ -using System; - -namespace Events -{ - /// - /// Класс должен описывать представление о товаре. - /// В рамках лабораторной работы должен являться - /// источником события - /// - class Product - { - - #region Variables - /// - /// Наименование - /// - private string name; - /// - /// Стоимость - /// - private decimal price; - - #endregion - - #region Properties - - /// - /// Наименование - /// - public string Name - { - get { return name; } - set - { - name = value; - /* - * TODO #4 Инициировать уведомление об - * изменении наименования - */ - } - } - /// - /// Стоимость - /// - public decimal Price - { - get { return price; } - set - { - price = value; - /* - * TODO #5 Инициировать уведомление об - * изменении стоимости - */ - } - } - - #endregion - - #region Events - - /* - * TODO #3 Добавить определение событий - */ - - #endregion - - public Product(string name, decimal price) - { - Name = name; - Price = price; - } - - } -} +using System; + +namespace Events +{ + /// + /// Класс должен описывать представление о товаре. + /// В рамках лабораторной работы должен являться + /// источником события + /// + + class Product + { + #region Variables + /// + /// Наименование + /// + private string name; + /// + /// Стоимость + /// + private decimal price; + + #endregion + + #region Properties + + /// + /// Наименование + /// + public string Name + { + get { return name; } + set + { + /* + * TODO #4 Инициировать уведомление об + * изменении наименования + */ + var args = new NameChangeEventArgs(name, value); + name = value; + if (NameChanged != null) + { + NameChanged(this, args); + } + } + } + /// + /// Стоимость + /// + public decimal Price + { + get { return price; } + set + { + /* + * TODO #5 Инициировать уведомление об + * изменении стоимости + */ + var args = new PriceChangeEventArgs(price, value); + price = value; + if (PriceChanged != null) + { + PriceChanged(this, args); + } + } + } + + #endregion + + #region Events + + /* + * TODO #3 Добавить определение событий + */ + public event EventHandler NameChanged; + public event EventHandler PriceChanged; + + #endregion + + public Product(string name, decimal price) + { + Name = name; + Price = price; + } + + } +} diff --git a/ProductEventArgs.cs b/ProductEventArgs.cs index a79a6d6..30d1b1f 100644 --- a/ProductEventArgs.cs +++ b/ProductEventArgs.cs @@ -1,18 +1,58 @@ -namespace Events -{ - /// - /// Класс, который служит для передачи аргументов - /// в обработчик событий, возникающих в классе - /// Product - /// - /* - * TODO #1 Закончить определение класса ProductEventArgs - */ - class ProductEventArgs - { - /* - * TODO #2 Добавить определение необходимых компонент - * класса ProductEventArgs - */ - } -} +namespace Events +{ + /// + /// Класс, который служит для передачи аргументов + /// в обработчик событий, возникающих в классе + /// Product + /// + /* + * TODO #1 Закончить определение класса ProductEventArgs + */ + public class ProductEventArgs + { + /* + * TODO #2 Добавить определение необходимых компонент + * класса ProductEventArgs + */ + public static readonly ProductEventArgs Empty = new ProductEventArgs(); + public ProductEventArgs() { } +} + + class NameChangeEventArgs : ProductEventArgs + { + /// + /// Имя до события. + /// + public string Old { get; } + + /// + /// Имя, после события. + /// + public string Current { get; } + + public NameChangeEventArgs(string old, string current) + { + Old = old; + Current = current; + } + } + + class PriceChangeEventArgs : ProductEventArgs + { + /// + /// Имя до события. + /// + public decimal Old { get; } + + /// + /// Имя, после события. + /// + public decimal Current { get; } + + public PriceChangeEventArgs(decimal old, decimal current) + { + Old = old; + Current = current; + } + } +} diff --git a/Program.cs b/Program.cs index b9247f7..c619069 100644 --- a/Program.cs +++ b/Program.cs @@ -1,37 +1,57 @@ -namespace Events -{ - class Program - { - internal Product Product - { - get - { - throw new System.NotImplementedException(); - } - - set - { - throw new System.NotImplementedException(); - } - } - - static void Main(string[] args) - { - Product product = new Product("Some product name", 0); - - /* - * TODO #6 Назначить обработчики событий в текущем контексте - */ - - /* - * TODO #7 Выполнить с экземпляром класса Product действия, - * приводящие к возникновению описанных Вами событий - */ - } - - /* - * TODO #8 Добавить определение обработчиков событий - */ - - } -} +using System; + + +namespace Events +{ + class Program + { + internal Product Product + { + get + { + throw new System.NotImplementedException(); + } + + set + { + throw new System.NotImplementedException(); + } + } + + static void Main(string[] args) + { + Product product = new Product("Some product name", 0); + + /* + * TODO #6 Назначить обработчики событий в текущем контексте + */ + + product.NameChanged += ProductNameChanged; + // ... + product.Name = "Plate"; + + product.PriceChanged += ProductPriceChanged; + // ... + product.Price = 55; + Console.Read(); + /* + * TODO #7 Выполнить с экземпляром класса Product действия, + * приводящие к возникновению описанных Вами событий + */ + } + + /* + * TODO #8 Добавить определение обработчиков событий + */ + private static void ProductNameChanged(object sender, NameChangeEventArgs args) + { + Console.WriteLine("Название продукта {0} изменено с '{0}' на '{1}'", args.Old, args.Current); + } + + private static void ProductPriceChanged(object sender, PriceChangeEventArgs args) + { + Console.WriteLine("Цена продукта изменена с '{0}' на '{1}'", args.Old, args.Current); + } + + } +}