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
163 changes: 88 additions & 75 deletions Product.cs
Original file line number Diff line number Diff line change
@@ -1,75 +1,88 @@
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<string>(name, value);
name = value;
if (NameChanged != null)
{
NameChanged(this, args);
}
}
}
/// <summary>
/// Стоимость
/// </summary>
public decimal Price
{
get { return price; }
set
{
/*
* TODO #5 Инициировать уведомление об
* изменении стоимости
*/
var args = new ProductEventArgs<decimal>(price, value);
price = value;
if (PriceChanged != null)
{
PriceChanged(this, args);
}
}
}

#endregion

#region Events

/*
* TODO #3 Добавить определение событий
*/
public event EventHandler<ProductEventArgs<decimal>> PriceChanged;
public event EventHandler<ProductEventArgs<string>> NameChanged;

#endregion

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

}
}
48 changes: 30 additions & 18 deletions ProductEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
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
*/
public class ProductEventArgs<T> : EventArgs
{
/*
* TODO #2 Добавить определение необходимых компонент
* класса ProductEventArgs
*/

public T Oldvalue { get; }
public T Newvalue { get; }

public ProductEventArgs(T oldValue, T newValue)
{
Oldvalue = oldValue;
Newvalue = newValue;
}

}
}
91 changes: 54 additions & 37 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,54 @@
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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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.PriceChanged += ProductPriceChanged;
/*
* TODO #7 Выполнить с экземпляром класса Product действия,
* приводящие к возникновению описанных Вами событий
*/
product.Name = "Телевизор Sony";
product.Price = 80000;
}

/*
* TODO #8 Добавить определение обработчиков событий
*/
private static void ProductNameChanged(object sender, ProductEventArgs<string> args)
{
Console.WriteLine("Название товара '{0}' изменилось на '{1}'", args.Oldvalue, args.Newvalue);
}

private static void ProductPriceChanged(object sender, ProductEventArgs<decimal> args)
{
Console.WriteLine("Старая цена равная {0} была изменена на {1}", args.Oldvalue, args.Newvalue);
}
}
}