diff --git a/Product.cs b/Product.cs index f68c8c9..84b49ec 100644 --- a/Product.cs +++ b/Product.cs @@ -1,75 +1,86 @@ -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 + { + var args= new NameChangeEventArgs(name, value); + name = value; + if (NameChanged != null) + { + NameChanged(this, args); + } + /* + * TODO #4 Инициировать уведомление об + * изменении наименования + */ + } + } + /// + /// Стоимость + /// + 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 PriceChanged; + public event EventHandler NameChanged; + + #endregion + + public Product(string name, decimal price) + { + Name = name; + Price = price; + } + + } +} diff --git a/ProductEventArgs.cs b/ProductEventArgs.cs index a79a6d6..6654d3c 100644 --- a/ProductEventArgs.cs +++ b/ProductEventArgs.cs @@ -1,18 +1,53 @@ -namespace Events -{ - /// - /// Класс, который служит для передачи аргументов - /// в обработчик событий, возникающих в классе - /// Product - /// - /* - * TODO #1 Закончить определение класса ProductEventArgs - */ - class ProductEventArgs - { - /* - * TODO #2 Добавить определение необходимых компонент - * класса ProductEventArgs - */ - } -} +namespace Events +{ + /// + /// Класс, который служит для передачи аргументов + /// в обработчик событий, возникающих в классе + /// Product + /// + /* + * TODO #1 Закончить определение класса ProductEventArgs + */ + + class ProductEventArgs + { + /* + * TODO #2 Добавить определение необходимых компонент + * класса ProductEventArgs + */ + public static 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..c96c308 100644 --- a/Program.cs +++ b/Program.cs @@ -1,37 +1,49 @@ -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("Milk", 0); + product.NameChanged += ProductNameChanged; + product.Name = "Молоко"; + product.PriceChanged += ProductPriceChanged; + product.Price = 75; + Console.ReadKey(); + /* + * TODO #6 Назначить обработчики событий в текущем контексте + */ + + /* + * TODO #7 Выполнить с экземпляром класса Product действия, + * приводящие к возникновению описанных Вами событий + */ + } + private static void ProductNameChanged(object sender, NameChangeEventArgs args) + { + Console.WriteLine("Продукту изменили имя с {0} на {1}", args.Old, args.Current); + } + private static void ProductPriceChanged(object sender, PriceChangeEventArgs args) + { + Console.WriteLine("Продукту изменили цену с {0} на {1}", args.Old, args.Current); + } + /* + * TODO #8 Добавить определение обработчиков событий + */ + + } +}