Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Product.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,13 @@ public string Name
get { return name; }
set
{
string oldName = name;
name = value;
/*
* TODO #4 Инициировать уведомление об
* изменении наименования
*/
OnNameChanged?.Invoke(this, new ProductEventArgs(oldName));
}
}
/// <summary>
Expand All @@ -47,11 +49,13 @@ public decimal Price
get { return price; }
set
{
decimal oldPrice = price;
price = value;
/*
* TODO #5 Инициировать уведомление об
* изменении стоимости
*/
OnPriceChanged?.Invoke(this, new ProductEventArgs(oldPrice));
}
}

Expand All @@ -62,12 +66,14 @@ public decimal Price
/*
* TODO #3 Добавить определение событий
*/
public static event EventHandler<ProductEventArgs> OnNameChanged;
public static event EventHandler<ProductEventArgs> OnPriceChanged;

#endregion

public Product(string name, decimal price)
{
Name = name;
Name = name;
Price = price;
}

Expand Down
18 changes: 16 additions & 2 deletions ProductEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Events
using System;

namespace Events
{
/// <summary>
/// Класс, который служит для передачи аргументов
Expand All @@ -8,11 +10,23 @@
/*
* TODO #1 Закончить определение класса ProductEventArgs
*/
class ProductEventArgs
class ProductEventArgs : EventArgs
{
/*
* TODO #2 Добавить определение необходимых компонент
* класса ProductEventArgs
*/

public string OldName { get; private set; }
public decimal OldPrice { get; private set; }

public ProductEventArgs(string oldName)
{
this.OldName = oldName;
}
public ProductEventArgs(decimal oldPrice)
{
this.OldPrice = oldPrice;
}
}
}
33 changes: 30 additions & 3 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
namespace Events
using System;
using System.IO;

namespace Events
{
class Program
{
Expand All @@ -17,21 +20,45 @@ internal Product Product

static void Main(string[] args)
{
Product product = new Product("Some product name", 0);
var product = new Product("Some product name", 0);

/*
* TODO #6 Назначить обработчики событий в текущем контексте
*/
Product.OnNameChanged += OnProductNameChanged;
Product.OnPriceChanged += OnProductPriceChanged;

/*
* TODO #7 Выполнить с экземпляром класса Product действия,
* приводящие к возникновению описанных Вами событий
*/
product.Name = "Plumbus";
product.Price = 911;

product.Name = "Baldezh";
product.Price = 1729;
product.Price = 2019;
}

/*
* TODO #8 Добавить определение обработчиков событий
*/
static void OnProductNameChanged(object sender, ProductEventArgs e)
{
var product = sender as Product;
Console.WriteLine(
$"The product \"{e.OldName}\" "
+ $"is now called \"{product.Name}\"."
);
}
static void OnProductPriceChanged(object sender, ProductEventArgs e)
{
var product = sender as Product;
Console.WriteLine(
$"The \"{product.Name}\" product's price has changed "
+ $"from {e.OldPrice}$ to {product.Price}$."
);
}

}
}