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
160 changes: 85 additions & 75 deletions Product.cs
Original file line number Diff line number Diff line change
@@ -1,75 +1,85 @@
using System;

namespace Events
{
/// <summary>
/// Класс должен описывать представление о товаре.
/// В рамках лабораторной работы должен являться
/// источником события
/// </summary>
class Product
{

#region Variables
/// <summary>
/// Наименование
/// </summary>
private string name;
/// <summary>
/// Стоимость
/// </summary>
private decimal price;

#endregion

#region Properties

/// <summary>
/// Наименование
/// </summary>
public string Name
{
get { return name; }
set
{
name = value;
/*
* TODO #4 Инициировать уведомление об
* изменении наименования
*/
}
}
/// <summary>
/// Стоимость
/// </summary>
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
{
/// <summary>
/// Класс должен описывать представление о товаре.
/// В рамках лабораторной работы должен являться
/// источником события
/// </summary>
class Product
{

#region Variables
/// <summary>
/// Наименование
/// </summary>
private string name;
/// <summary>
/// Стоимость
/// </summary>
private decimal price;

#endregion

#region Properties

/// <summary>
/// Наименование
/// </summary>
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);
}
}
/// <summary>
/// Стоимость
/// </summary>
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<ProductEventArgs> event_NameChange;
public static EventHandler<ProductEventArgs> event_PriceChange;

#endregion

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

}
}
50 changes: 32 additions & 18 deletions ProductEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
namespace Events
{
/// <summary>
/// Класс, который служит для передачи аргументов
/// в обработчик событий, возникающих в классе
/// <seealso cref="Product">Product</seealso>
/// </summary>
/*
* TODO #1 Закончить определение класса ProductEventArgs
*/
class ProductEventArgs
{
/*
* TODO #2 Добавить определение необходимых компонент
* класса ProductEventArgs
*/
}
}
using System;

namespace Events
{
/// <summary>
/// Класс, который служит для передачи аргументов
/// в обработчик событий, возникающих в классе
/// <seealso cref="Product">Product</seealso>
/// </summary>
/*
* 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;
}
}
}
97 changes: 60 additions & 37 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}