From d3c945394bda3243f507ed36550270b44ba5c1cc Mon Sep 17 00:00:00 2001
From: Eset35 <37243389+Eset35@users.noreply.github.com>
Date: Thu, 28 Feb 2019 21:39:26 +0300
Subject: [PATCH 1/2] Add files via upload
---
Product.cs | 160 +++++++++++++++++++++++---------------------
ProductEventArgs.cs | 50 +++++++++-----
Program.cs | 97 +++++++++++++++++----------
3 files changed, 177 insertions(+), 130 deletions(-)
diff --git a/Product.cs b/Product.cs
index f68c8c9..80848a2 100644
--- a/Product.cs
+++ b/Product.cs
@@ -1,75 +1,85 @@
-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 ProductEventArgs(name);
+ name = value;
+
+ if (event_NameChange != null)
+ event_NameChange.Invoke(this, args);
+ /*
+ * TODO #4 Инициировать уведомление об
+ * изменении наименования
+ */
+ }
+ }
+ ///
+ /// Стоимость
+ ///
+ public decimal Price
+ {
+ get { return price; }
+ set
+ {
+ var args = new ProductEventArgs(price);
+ price = value;
+ /*
+ * TODO #5 Инициировать уведомление об
+ * изменении стоимости
+ */
+ if (event_PriceChange != null)
+ event_PriceChange.Invoke(this, args);
+ }
+ }
+
+ #endregion
+
+ #region Events
+
+ /*
+ * TODO #3 Добавить определение событий
+ */
+
+ public static EventHandler event_NameChange;
+ public static EventHandler event_PriceChange;
+
+ #endregion
+
+ public Product(string name, decimal price)
+ {
+ Name = name;
+ Price = price;
+ }
+
+ }
+}
diff --git a/ProductEventArgs.cs b/ProductEventArgs.cs
index a79a6d6..96bbd86 100644
--- a/ProductEventArgs.cs
+++ b/ProductEventArgs.cs
@@ -1,18 +1,32 @@
-namespace Events
-{
- ///
- /// Класс, который служит для передачи аргументов
- /// в обработчик событий, возникающих в классе
- /// Product
- ///
- /*
- * TODO #1 Закончить определение класса ProductEventArgs
- */
- class ProductEventArgs
- {
- /*
- * TODO #2 Добавить определение необходимых компонент
- * класса ProductEventArgs
- */
- }
-}
+using System;
+
+namespace Events
+{
+ ///
+ /// Класс, который служит для передачи аргументов
+ /// в обработчик событий, возникающих в классе
+ /// Product
+ ///
+ /*
+ * TODO #1 Закончить определение класса ProductEventArgs
+ */
+ class ProductEventArgs : EventArgs
+ {
+ /*
+ * TODO #2 Добавить определение необходимых компонент
+ * класса ProductEventArgs
+ */
+ public string OldName { get; }
+ public decimal OldPrice { get; }
+
+ public ProductEventArgs(string oldName)
+ {
+ this.OldName = oldName;
+ }
+
+ public ProductEventArgs(decimal oldPrice)
+ {
+ this.OldPrice = oldPrice;
+ }
+ }
+}
diff --git a/Program.cs b/Program.cs
index b9247f7..4ab5a16 100644
--- a/Program.cs
+++ b/Program.cs
@@ -1,37 +1,60 @@
-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.event_NameChange += ProductNameChanger;
+ Product.event_PriceChange += ProductPriceChanger;
+
+ /*
+ * TODO #7 Выполнить с экземпляром класса Product действия,
+ * приводящие к возникновению описанных Вами событий
+ */
+
+ product.Name = "sadasda";
+ product.Price = 1;
+ product.Name = "test";
+ product.Name = "ttttt";
+ product.Price = 822;
+ product.Price = 5452;
+
+ Console.ReadKey();
+ }
+
+ /*
+ * TODO #8 Добавить определение обработчиков событий
+ */
+
+ static void ProductNameChanger(object sender, ProductEventArgs args)
+ {
+ var product = sender as Product;
+ Console.WriteLine("Name of {0} has changed to {1}", args.OldName, product.Name);
+ }
+
+ static void ProductPriceChanger(object sender, ProductEventArgs args)
+ {
+ var product = sender as Product;
+ Console.WriteLine("Price of {0} has changed from {1} to {2}", product.Name, args.OldPrice, product.Price);
+ }
+ }
+}
From 2be158c8d6f2b1eea12f42a6c0cd1c5b7b00a4a8 Mon Sep 17 00:00:00 2001
From: Eset35 <37243389+Eset35@users.noreply.github.com>
Date: Thu, 28 Feb 2019 21:42:57 +0300
Subject: [PATCH 2/2] Add files via upload
---
Product.cs | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/Product.cs b/Product.cs
index 80848a2..7b0b7d2 100644
--- a/Product.cs
+++ b/Product.cs
@@ -32,15 +32,15 @@ public string Name
get { return name; }
set
{
+ /*
+ * TODO #4 Инициировать уведомление об
+ * изменении наименования
+ */
var args = new ProductEventArgs(name);
name = value;
if (event_NameChange != null)
event_NameChange.Invoke(this, args);
- /*
- * TODO #4 Инициировать уведомление об
- * изменении наименования
- */
}
}
///