Skip to content

Instantly share code, notes, and snippets.

@Soulstorm50
Created March 11, 2017 19:21
Show Gist options
  • Save Soulstorm50/5678d1846fb215836618948c93887ad3 to your computer and use it in GitHub Desktop.
Save Soulstorm50/5678d1846fb215836618948c93887ad3 to your computer and use it in GitHub Desktop.
C# Класс Student with events
using System;
using System.Text.RegularExpressions;
using System.Collections;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Student s = new Student();
s.Late += Student_Late;
s.Slept += Student_Slept;
s.NotPaid += Student_NotPaid;
s.RUN();
}
static void Student_Late(object sender, MyEventArgs e)
{
Console.WriteLine("{0} " + e.SomeTextData, (sender as Student).FName);
}
static void Student_Slept(object sender, MyEventArgs e)
{
Console.WriteLine("{0} " + e.SomeTextData, (sender as Student).FName);
}
static void Student_NotPaid(object sender, MyEventArgs e)
{
Console.WriteLine("{0} " + e.SomeTextData, (sender as Student).FName);
}
}
public class MyEventArgs : EventArgs
{
public String SomeTextData
{
get;
private set;
}
public MyEventArgs(string s)
{
SomeTextData = s;
}
}
class Student : IComparable
{
public delegate void Del(object sender, MyEventArgs e);
public event Del Late;
public event Del Slept;
public event Del NotPaid;
public void RUN()
{
Thread.Sleep(1000);
if (Late != null) Late(this, new MyEventArgs("Опоздал!"));
Thread.Sleep(1000);
if (Slept != null) Slept(this, new MyEventArgs("Проспал!"));
Thread.Sleep(1000);
if (NotPaid != null) NotPaid(this, new MyEventArgs("Не оплатил!"));
}
public class SortByLName : IComparer
{
public int Compare(object x, object y)
{
Student s1 = x as Student;
Student s2 = y as Student;
return String.Compare(s1.LName, s2.LName);
}
}
public class SortByBirthday : IComparer
{
public int Compare(object x, object y)
{
Student s1 = x as Student;
Student s2 = y as Student;
return DateTime.Compare(s1.BirthDay, s2.BirthDay);
}
}
public class SortByRating : IComparer
{
public int Compare(object x, object y)
{
Student s1 = x as Student;
Student s2 = y as Student;
return s1.rating >= s2.rating ? 1 : -1;
}
}
String fname;
String sname;
String lname;
String adress;
String phone;
DateTime birthday;
int[] mark = { 0 };
int[] cwork = { 0 };
int[] exam = { 0 };
int rating;
int age = 18;
public String FName
{
get
{
return fname;
}
set
{
if (value.Length > 2)
fname = value;
}
}
public String LName
{
get
{
return lname;
}
set
{
if (value.Length > 2)
lname = value;
}
}
public String SName
{
get
{
return sname;
}
set
{
if (value.Length > 2)
sname = value;
}
}
public String Adress
{
get
{
return adress;
}
set
{
if (value.Length > 2)
adress = value;
}
}
public int Age
{
get
{
return age;
}
set
{
if (value > 17 && value < 100)
age = value;
}
}
public String Phone
{
get
{
return phone;
}
set
{
string pattern = "^(\\+\\d{2}\\s+)*\\d{3}\\s*-?(\\d{3}\\s*-?\\d{2}\\s*-?\\d{2}|\\d{2}\\s*-?\\d{2}\\s*-?\\d{3})$";
Regex regex = new Regex(pattern);
Match match = regex.Match(value);
if (match.Success)
this.phone = value;
else this.phone = "NULL";
}
}
public DateTime BirthDay
{
get
{
return birthday;
}
set
{
int temp = DateTime.Now.Year - value.Year;
if (temp > 16 && temp < 100)
birthday = value;
}
}
public Student()
{
SetAll("Test", "Test");
}
public Student(string fname, string lname)
{
SetAll(fname, lname);
}
public Student(string fname, string lname, string sname, string adress, string phone, DateTime birthday)
{
SetAll(fname, lname, sname, adress, phone);
SetBirthday(birthday);
}
private void SetAll(string fname, string lname, string sname = @"", string adress = @"", string phone = @"")
{
SetField(ref this.fname, fname);
SetField(ref this.lname, lname);
SetField(ref this.sname, sname);
SetField(ref this.adress, adress);
SetPhone(phone);
}
public Student(Student origin)
{
SetAll(origin.fname, origin.lname, origin.sname, origin.adress, origin.phone);
SetBirthday(origin.birthday);
mark = origin.GetMarks();
cwork = origin.GetCWorks();
exam = origin.GetExams();
rating = origin.rating;
}
public Student Copy()
{
return new Student(this);
}
private void SetField(ref String field, String value)
{
if (value.Length > 2)
field = value;
else field = "NULL";
}
public int GetRating()
{
return rating;
}
public void SetPhone(string phone)
{
string pattern = "^(\\+\\d{2}\\s+)*\\d{3}\\s*-?(\\d{3}\\s*-?\\d{2}\\s*-?\\d{2}|\\d{2}\\s*-?\\d{2}\\s*-?\\d{3})$";
Regex regex = new Regex(pattern);
Match match = regex.Match(phone);
if (match.Success)
this.phone = phone;
else this.phone = "NULL";
}
public void SetFName(string fname)
{
SetField(ref this.fname, fname);
}
public void SetLName(string lname)
{
SetField(ref this.lname, lname);
}
public void SetSName(string sname)
{
SetField(ref this.sname, sname);
}
public void SetAdress(string adress)
{
SetField(ref this.adress, adress);
}
public void SetBirthday(DateTime birthday)
{
int value = DateTime.Now.Year - birthday.Year;
if (value > 17 && value < 66)
this.birthday = birthday;
}
public String GetFname()
{
return fname;
}
public String GetLname()
{
return lname;
}
public String GetSname()
{
return sname;
}
public String GetAdress()
{
return adress;
}
public String GetPhone()
{
return phone;
}
public DateTime GetBirthday()
{
return birthday;
}
public int[] GetMarks()
{
int[] temp = new int[mark.Length];
Array.Copy(mark, temp, mark.Length);
return temp;
}
public int[] GetCWorks()
{
int[] temp = new int[cwork.Length];
Array.Copy(cwork, temp, cwork.Length);
return temp;
}
public int[] GetExams()
{
int[] temp = new int[exam.Length];
Array.Copy(exam, temp, exam.Length);
return temp;
}
public void SetMark(int value, int index)
{
AddMark(ref mark, value, index);
UpdateRating();
}
public void AddHomeWork()
{ // идея по типу майстата
Addlen(ref mark);
UpdateRating();
}
public int GetMark(int index)
{
if (index < mark.Length)
return mark[index];
return -1;
}
public void SetCourseWork(int value, int index)
{
AddMark(ref cwork, value, index);
UpdateRating();
}
public void AddCourseWork()
{
Addlen(ref cwork);
UpdateRating();
}
public void SetExam(int value, int index)
{
AddMark(ref exam, value, index);
UpdateRating();
}
public void AddExam()
{
Addlen(ref exam);
UpdateRating();
}
private void Addlen(ref int[] sourse)
{
int[] temp = new int[sourse.Length + 1];
Array.Copy(sourse, temp, mark.Length);
sourse = temp;
}
private void AddMark(ref int[] field, int value, int index)
{
if (index < field.Length && (value > 0 && value < 13))
field[index] = value;
}
private void UpdateRating()
{
double rating = 100 / 12;
double valueM = 0;
for (int i = 0; i < mark.Length; i++)
{
valueM += mark[i];
}
valueM = valueM / mark.Length;
double valueCW = 0;
for (int i = 0; i < cwork.Length; i++)
{
valueCW += cwork[i];
}
valueCW = valueCW / cwork.Length;
if (valueCW == 0) valueCW = 1;
double valueEX = 0;
for (int i = 0; i < exam.Length; i++)
{
valueEX += exam[i];
}
valueEX = valueEX / exam.Length;
if (valueEX == 0) valueEX = 1;
this.rating = (int)(rating * valueM * valueCW * valueEX);
}
public void PrintState()
{
Console.WriteLine("First name: " + fname);
Console.WriteLine("Last name: " + lname);
Console.WriteLine("Second name: " + sname);
Console.Write("Birthday: :");
if (birthday.Year != 1)
Console.WriteLine("{0}.{1}.{2}", birthday.Day, birthday.Month, birthday.Year);
else Console.WriteLine("NULL");
Console.WriteLine("Adress: " + adress);
Console.WriteLine("Phone: " + phone);
Console.Write("Homeworks: ");
foreach (int a in mark)
Console.Write(a + " ");
Console.WriteLine();
Console.Write("Courseworks: ");
foreach (int a in cwork)
if (a == 0) Console.Write("NULL");
else Console.Write(a + " ");
Console.WriteLine();
Console.Write("Exams: ");
foreach (int a in exam)
if (a == 0) Console.Write("NULL");
else Console.Write(a + " ");
Console.WriteLine();
Console.WriteLine("Rating: " + rating);
}
public int CompareTo(object obj)
{
Student temp = obj as Student;
return this.lname.CompareTo(temp.lname);
}
}
class Group : IEnumerable
{
public String name
{
get
{
return name;
}
set
{
if (value.Length > 2)
name = value;
}
}
public int course
{
get
{
return course;
}
set
{
if (value > 0 && value < 6)
course = value;
}
}
public String specialization
{
get
{
return specialization;
}
set
{
if (specialization.Length > 2)
specialization = value;
}
}
public int count
{
get
{
return count;
}
private set
{
count = students.Length;
}
}
public Student[] students;
public Group()
{
students = new Student[10];
for (int i = 0; i < students.Length; i++)
{
students[i] = new Student();
}
Array.Sort(students);
}
public Group(int amount)
{
students = new Student[amount];
}
public Group(Student[] sourse)
{
CopyStudentsArraylist(sourse);
}
public Group(Group origin)
{
CopyStudentsArraylist(origin.students);
this.name = origin.name;
this.course = origin.course;
this.specialization = origin.specialization;
}
private void CopyStudentsArraylist(Student[] sourse)
{
students = new Student[sourse.Length];
for (int i = 0; i < sourse.Length; i++)
{
students[i] = sourse[i].Copy();
}
}
public void PrintStudents()
{
int i = 0;
foreach (Student a in students)
{
Console.WriteLine("Student {0}:", i);
a.PrintState();
Console.WriteLine();
i++;
}
}
public void PrintGroupState()
{
Console.WriteLine("Group name: " + name);
Console.WriteLine("Group specialization: " + specialization);
Console.WriteLine("Course: " + course);
PrintStudents();
}
public void PrintStudent(int index)
{
if (index < students.Length)
students[index].PrintState();
}
public void AddStudent(String fname, String lname)
{
Student[] temp = new Student[students.Length + 1];
for (int i = 0; i < students.Length; i++)
{
temp[i] = students[i];
}
temp[students.Length] = new Student(fname, lname);
students = temp;
Array.Sort(students);
}
public void AddStudent(Student origin)
{
Student[] temp = new Student[students.Length + 1];
for (int i = 0; i < students.Length; i++)
{
temp[i] = students[i];
}
temp[students.Length] = new Student(origin);
students = temp;
Array.Sort(students);
}
public void DeleteStudent(int index)
{
if (students.Length < 2 || index < 0 || index >= students.Length) return;
Student[] temp = new Student[students.Length - 1];
for (int i = 0; i < index; i++)
{
temp[i] = students[i];
}
for (int i = index; i < students.Length - 1; i++)
{
temp[index] = students[index + 1];
}
students = temp;
}
public void TransferStudent(int index, Group into)
{
into.AddStudent(students[index]);
Array.Sort(into.students);
DeleteStudent(index);
}
public void DeleteLoser()
{
int min = 0;
for (int i = 0; i < students.Length; i++)
{
if (students[min].GetRating() > students[i].GetRating()) min = 0;
}
DeleteStudent(min);
}
public void DeleteAllLosers()
{
int minrating = 50;
for (int i = 0; i < students.Length; i++)
{
if (students[i].GetRating() < minrating)
{
DeleteStudent(i);
i--;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public StudentEnum GetEnumerator()
{
return new StudentEnum(students);
}
}
class StudentEnum : IEnumerator
{
public Student[] _students;
int position = -1;
public StudentEnum(Student[] list)
{
_students = list;
}
public bool MoveNext()
{
position++;
return (position < _students.Length);
}
public void Reset()
{
position = -1;
}
public Student Current
{
get
{
try
{
return _students[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
object IEnumerator.Current
{
get
{
try
{
return _students[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment