Лабораторная работа 4 Вариант 2
делегаты. события
Никитина Дарья ПИН-31Д
Article.Cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DashaLabs
{
class ArticleComparer : IComparer<Article>
{
public int Compare(Article x, Article y)
{
if (x == null || y == null)
throw new ArgumentException("One or both arguments is not type of Article");
if (x.Score <= y.Score)
return x.Score == y.Score ? 0 : -1;
return 1;
}
}
class Article : IRateAndCopy, IComparable, IComparer<Article>
{
public Person Author
{
get;
set;
}
public String Name
{
get;
set;
}
public double Score
{
get;
set;
}
public Article(Person a, string b, double c)
{
Author = a;
Name = b;
Score = c;
}
public Article()
{
Author = new Person();
Name = "";
Score = 0;
}
public override string ToString()
{
return Author.ToShortString() + "\n" + "Название статьи: " + Name + "\n" + "Рейтинг " + Score.ToString() + "\n";
}
public object DeepCopy()
{
Article Result = new Article();
Result.Author = (Person)Author.DeepCopy();
Result.Name = (string)Name.Clone();
Result.Score = Score;
return (object)Result;
}
public static Article Scan()
{
Person tauthor = (Person)Person.Scan().DeepCopy();
Console.WriteLine("Введите название");
string tname = Console.ReadLine();
Console.WriteLine("Введите рейтинг статьи");
double tRating = Convert.ToDouble(Console.ReadLine());
return new Article(tauthor, tname, tRating);
}
public int CompareTo(object obj)
{
if (obj == null) return 1;
Article article = obj as Article;
if (article != null)
return Name.CompareTo(obj);
throw new ArgumentException("Argument is not type of Article");
}
public int Compare(Article x, Article y)
{
if (x == null || y == null)
throw new ArgumentException("One or both arguments is not type of Article");
return string.Compare(x.Author.LName, y.Author.LName);
}
}
}
ChangeCollectionEventType.Cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DashaLabs
{
enum ChangeCollectionEventType
{
Add, Replace, Property
}
}
Edition.Cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DashaLabs
{
class Edition: INotifyPropertyChanged
{
protected string name;
protected DateTime outDate;
protected int count;
public event PropertyChangedEventHandler PropertyChanged;
public Edition(string name, DateTime date, int count)
{
this.name = name;
outDate = date;
this.count = count;
}
public Edition()
{
name = "";
outDate = DateTime.Now;
count = 0;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
public DateTime OutDate
{
get
{
return outDate;
}
set
{
outDate = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("OutDate"));
}
}
public int Count
{
get
{
return count;
}
set
{
if (value > 0)
{
count = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Count"));
}
else
{
ArgumentOutOfRangeException MyExeption = new ArgumentOutOfRangeException("_count", "Значение должно быть положительным");
throw MyExeption;
}
}
}
public virtual object DeepCopy()
{
Edition Result = new Edition();
Result.name = (string)this.name.Clone();
Result.outDate = this.outDate;
Result.count = this.count;
return (object)Result;
}
public override bool Equals(object obj)
{
Edition a = (Edition)obj;
return (string)a.name.Clone() == (string)this.name.Clone() && a.outDate == this.outDate && a.count == this.count;
}
public static bool operator ==(Edition a, Edition b)
{
return a.Equals(b);
}
public static bool operator !=(Edition a, Edition b)
{
return !a.Equals(b);
}
public override int GetHashCode()
{
return name.GetHashCode();
}
public override string ToString()
{
return name + " " + outDate.ToShortDateString() + " " + count;
}
public static Edition Scan()
{
Console.WriteLine("Введите название");
string tname = Console.ReadLine();
Console.WriteLine("Введите дату выхода");
DateTime TDate = DateTime.Parse(Console.ReadLine());
Console.WriteLine("Введите тираж");
int tcount = Convert.ToInt32(Console.ReadLine());
return new Edition((string)tname.Clone(), TDate, tcount);
}
}
}
Frequency.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DashaLabs
{
enum Frequency
{
Weekly = 1, Monthly = 2, Yearly = 3
};
}
GenerateElement.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DashaLabs
{
delegate KeyValuePair<TKey, TValue> GenerateElement<TKey, TValue>(int j);
}
IRateAndCopy.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DashaLabs
{
interface IRateAndCopy
{
double Score
{
get;
}
object DeepCopy();
}
}
Listener.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DashaLabs
{
class Listener<TKey>
{
private List<ListEntry> collectionsList = new List<ListEntry>();
public void OnChanged (MagazinesChangedEventArgs<TKey> args)
{
ListEntry newEntry = new ListEntry(args.CollectionName, args.EventType, args.ChangedProperty, args.ElementKey.ToString());
collectionsList.Add(newEntry);
}
public override string ToString()
{
string result = "";
foreach(ListEntry entry in collectionsList)
{
result += entry.ToString() + "\n\n";
}
return result;
}
}
class ListEntry
{
public string CollectionName { get; set; }
public ChangeCollectionEventType EventType { get; set; }
public string ChangedProperty { get; set; }
public string ElementKey { get; set; }
public ListEntry(string collectionName, ChangeCollectionEventType eventType, string changedProperty, string elementKey)
{
CollectionName = collectionName;
EventType = eventType;
ChangedProperty = changedProperty;
ElementKey = elementKey;
}
public override string ToString()
{
return string.Format(
"Collection name: {0}\nEvent type: {1}\nChanged property: {2}\nElement key: {3}",
CollectionName,
EventType,
ChangedProperty,
ElementKey
);
}
}
}