ЛР2 / Лаб. 2 C# (Вариант 1)
.docxusing System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
interface IDateAndCopy
{
object DeepCopy();
DateTime Date { get; set; }
}
class Person : IDateAndCopy
{
protected string name;
protected string surname;
protected DateTime dateOfBirth;
public Person(string name, string surname, DateTime dateOfBirth)
{
this.name = name;
this.surname = surname;
this.dateOfBirth = dateOfBirth;
}
public Person()
{
name = "Name";
surname = "Surname";
dateOfBirth = DateTime.Today;
}
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
public string Surname
{
get
{
return surname;
}
set
{
surname = value;
}
}
public DateTime Date
{
get
{
return dateOfBirth;
}
set
{
dateOfBirth = value;
}
}
public int YearOfBirth
{
get
{
return dateOfBirth.Year;
}
set
{
dateOfBirth = new DateTime(value, dateOfBirth.Month, dateOfBirth.Day);
}
}
public override string ToString()
{
return $"Фамилия, имя: {surname} {name}\nДата рождения: {dateOfBirth.ToShortDateString()}";
}
/*
public virtual string ToShortString()
{
return $"Фамилия, имя: {surname} {name}";
}
*/
public override bool Equals(object obj)
{
if (obj is Person person)
{
return name.Equals(person.name) && surname.Equals(person.surname) && dateOfBirth.Equals(person.dateOfBirth);
}
else
{
return false;
}
}
public static bool operator == (Person humanOne, Person humanTwo)
{
return humanOne.name == humanTwo.name && humanOne.surname == humanTwo.surname && humanOne.dateOfBirth == humanTwo.dateOfBirth;
}
public static bool operator !=(Person humanOne, Person humanTwo)
{
return humanOne.name != humanTwo.name || humanOne.surname != humanTwo.surname || humanOne.dateOfBirth != humanTwo.dateOfBirth;
}
public override int GetHashCode()
{
Console.WriteLine($"Name HashCode: {name.GetHashCode()}");
Console.WriteLine($"Surame HashCode: {surname.GetHashCode()}");
Console.WriteLine($"Date Of Birth HashCode: {dateOfBirth.GetHashCode()}");
return name.GetHashCode() + surname.GetHashCode() + dateOfBirth.GetHashCode();
}
public virtual object DeepCopy()
{
Person human = new Person();
human.name = name;
human.surname = surname;
human.dateOfBirth = dateOfBirth;
return human;
}
}
enum Education
{
Specialist,
Bachelor,
SecondEducation
}
class Exam
{
public string subject { get; set; }
public int grade { get; set; }
public DateTime examDate { get; set; }
public Exam(string subject, int grade, DateTime examDate)
{
this.subject = subject;
this.grade = grade;
this.examDate = examDate;
}
public Exam()
{
subject = "Subject";
grade = 0;
examDate = DateTime.Today;
}
public override string ToString()
{
return $"Предмет: {subject}, Оценка: {grade}, Дата экзамена: {examDate.ToShortDateString()}";
}
}
class Student : Person, IDateAndCopy, IEnumerable
{
Education formOfEducation;
int groupNumber;
ArrayList tests;
ArrayList exams;
public Student(Person human, Education formOfEducation, int groupNumber)
{
this.Name = human.Name;
this.Surname = human.Surname;
this.Date = human.Date;
this.formOfEducation = formOfEducation;
this.groupNumber = groupNumber;
tests = new ArrayList(0);
exams = new ArrayList(0);
}
public Student()
{
Person human = new Person();
formOfEducation = Education.Bachelor;
this.groupNumber = 0;
tests = new ArrayList(0);
exams = new ArrayList(0);
}
public Person StudentData
{
get
{
return new Person(this.Name, this.Surname, this.Date);
}
set
{
this.Name = value.Name; this.Surname = value.Surname; this.Date = value.Date;
}
}
public double AverageGrade
{
get
{
if (exams.Count == 0)
{
return 0;
}
double sum = 0;
foreach (Exam exam in exams)
{
sum += exam.grade;
}
return sum / exams.Count;
}
}
public int GroupNumber
{
get
{
return groupNumber;
}
set
{
if (value <= 100 || value > 599)
{
throw new ArgumentOutOfRangeException("Номер группы должен быть в диапазоне от 101 до 599");
}
else
{
groupNumber = value;
}
}
}
public ArrayList Exams
{
get
{
return exams;
}
set
{
exams = value;
}
}
public ArrayList Tests
{
get
{
return tests;
}
set
{
tests = value;
}
}
public void AddExams(params Exam[] newExams)
{
foreach(Exam exam in newExams)
{
exams.Add(exam);
}
}
public void AddTests(params Test[] newTests)
{
foreach (Test test in newTests)
{
tests.Add(test);
}
}
public override string ToString()
{
Console.WriteLine($"Студент:\n{name} {surname} {dateOfBirth},\nОбразование: {formOfEducation},\nНомер группы: {groupNumber},\nЭкзамены: ");
for (int i = 0; i < exams.Count; i++)
{
Console.WriteLine(exams[i]);
}
Console.WriteLine("Зачёты: ");
for (int i = 0; i < tests.Count; i++)
{
Console.WriteLine(tests[i]);
}
return " ";
}
public virtual string ToShortString()
{
return $"Студент:\n{name} {surname} {dateOfBirth},\nОбразование: {formOfEducation},\nНомер группы: {groupNumber},\nСредний балл: {AverageGrade}\n";
}
public override object DeepCopy()
{
Student human = new Student();
human.name = name;
human.surname = surname;
human.dateOfBirth = dateOfBirth;
human.formOfEducation = formOfEducation;
human.groupNumber = groupNumber;
foreach (Exam exam in this.exams)
{
human.Exams.Add(exam);
}
foreach (Test test in this.tests)
{
human.Tests.Add(test);
}
return human;
}
// перебор всех зачётов и экзаменов
public IEnumerable TestExam()
{
foreach (Test test in this.tests)
{
yield return test;
}
foreach (Exam exam in this.exams)
{
yield return exam;
}
}
public IEnumerable GetExams(int grade)
{
foreach (Exam exam in exams)
{
if (exam.grade > grade)
{
yield return exam;
}
}
}
public IEnumerator GetEnumerator()
{
return new StudentEnumerator(exams, tests);
}
public IEnumerable ExamTestDone()
{
foreach(Exam exam in this.GetExams(2))
{
yield return exam;
}
foreach(Test test in this.tests)
{
if (test.status == true)
{
yield return test;
}
}
}
public IEnumerable TestWithExamDone()
{
foreach(Test test in this.tests)
{
if (test.status == true)
{
foreach(Exam exam in this.GetExams(2))
{
if (test.subject == exam.subject)
{
yield return test;
}
}
}
}
}
}
class StudentEnumerator : IEnumerator
{
ArrayList exams;
ArrayList tests;
int index;
public StudentEnumerator(ArrayList exams, ArrayList tests)
{
this.exams = exams;
this.tests = tests;
index = -1;
}
public object Current
{
get
{
return exams[index];
}
}
public bool MoveNext()
{
for (int i = index + 1; i < exams.Count; i++)
{
foreach (var test in tests)
{
if (((Test)test).subject == ((Exam)exams[i]).subject)
{
index = i;
return true;
}
}
}
return false;
}
public void Reset()
{
index = -1;
}
}
class Test
{
public string subject { get; set; }
public bool status { get; set; }
public Test(string subject, bool status)
{
this.subject = subject;
this.status = status;
}
public Test()
{
subject = "Subject";
status = true;
}
public override string ToString()
{
return $"Предмет: {subject},\nЗачёт: {status}";
}
}
namespace LAB_2_PROVERKA
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("---№1---");
Person humanOne = new Person();
Person humanTwo = new Person();
Console.WriteLine($"Ссылки на объекты: {ReferenceEquals(humanOne, humanTwo)}\n");
Console.WriteLine($"Значения объектов: {humanOne.Equals(humanTwo)}\n");
Console.WriteLine("Хэш-код первого объекта:");
Console.WriteLine(humanOne.GetHashCode());
Console.WriteLine("\nХэш-код второго объекта:");
Console.WriteLine(humanTwo.GetHashCode());
Console.WriteLine("\n---№2---");
Student studentOne = new Student(new Person("Alex", "Dark", new DateTime(2004, 4, 12)), Education.Bachelor, 200);
studentOne.AddExams(new Exam("Физика", 4, new DateTime(2022, 6, 16)), new Exam("ЭВМ", 5, new DateTime(2022, 6, 20)));
studentOne.AddTests(new Test("ЭВМ", true), new Test("Английский язык", false));
Console.WriteLine(studentOne.ToString());
Console.WriteLine("\n---№3---");
Console.WriteLine(studentOne.StudentData);
Console.WriteLine("\n---№4---");
Student studentOneCopy = studentOne.DeepCopy() as Student;
studentOne.Name = "Will";
studentOne.Date = new DateTime(2003, 7, 18);
studentOne.AddTests(new Test("Информатика", true));
Console.WriteLine("Изменённый:");
Console.WriteLine(studentOne.ToString());
Console.WriteLine("Копия:");
Console.WriteLine(studentOneCopy.ToString());
Console.WriteLine("\n---№5---");
try
{
studentOne.GroupNumber = 20;
}
catch(ArgumentOutOfRangeException exception)
{
Console.WriteLine(exception.Message + "\n");
}
Console.WriteLine("\n---№6---");
Console.WriteLine("Список всех экзаменов и зачётов:");
foreach(var testExam in studentOne.TestExam())
{
Console.WriteLine(testExam + "\n");
}
Console.WriteLine("\n---№7---");
studentOne.AddExams(new Exam("Java", 3, new DateTime(2022, 12, 31)));
Console.WriteLine(studentOne.ToString());
Console.WriteLine("\nСписок всех экзаменов с оценкой выше 3:");
foreach (Exam exam in studentOne.GetExams(3))
{
Console.WriteLine(exam + "\n");
}
Console.WriteLine("\n---№8---");
Console.WriteLine("Список предметов, которые есть как в списке зачетов, так и в списке экзаменов:");
foreach (Exam subjectName in studentOne)
{
Console.WriteLine(subjectName.subject);
}
Console.WriteLine("\n---№9---");
Console.WriteLine("Список всех сданных зачетов и сданных экзаменов:");
foreach(var subject in studentOne.ExamTestDone())
{
Console.WriteLine(subject + "\n");
}
Console.WriteLine("\n---№10---");
Console.WriteLine("Список сданных зачетов, для которых сдан и экзамен:");
foreach (Test test in studentOne.TestWithExamDone())
{
Console.WriteLine(test.subject + "\n");
}
Console.ReadKey();
}
}
}