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
162 changes: 87 additions & 75 deletions Product.cs
Original file line number Diff line number Diff line change
@@ -1,75 +1,87 @@
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 NameChangeEventArgs(name, value);
name = value;
if (NameChanged != null)
{
NameChanged(this, args);
}
}
}
/// <summary>
/// Стоимость
/// </summary>
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<NameChangeEventArgs> NameChanged;
public event EventHandler<PriceChangeEventArgs> PriceChanged;

#endregion

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

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

class NameChangeEventArgs : ProductEventArgs
{
/// <summary>
/// Имя до события.
/// </summary>
public string Old { get; }

/// <summary>
/// Имя, после события.
/// </summary>
public string Current { get; }

public NameChangeEventArgs(string old, string current)
{
Old = old;
Current = current;
}
}

class PriceChangeEventArgs : ProductEventArgs
{
/// <summary>
/// Имя до события.
/// </summary>
public decimal Old { get; }

/// <summary>
/// Имя, после события.
/// </summary>
public decimal Current { get; }

public PriceChangeEventArgs(decimal old, decimal current)
{
Old = old;
Current = current;
}
}
}
94 changes: 57 additions & 37 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,57 @@
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.NameChanged += ProductNameChanged;
// ...
product.Name = "Plate";

product.PriceChanged += ProductPriceChanged;
// ...
product.Price = 55;
Console.Read();
/*
* TODO #7 Выполнить с экземпляром класса Product действия,
* приводящие к возникновению описанных Вами событий
*/
}

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

private static void ProductPriceChanged(object sender, PriceChangeEventArgs args)
{
Console.WriteLine("Цена продукта изменена с '{0}' на '{1}'", args.Old, args.Current);
}

}
}