Created
March 2, 2017 19:54
-
-
Save Soulstorm50/a2518b22459cd92655d61b63b2beb43d to your computer and use it in GitHub Desktop.
IComparable / IEnumerable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Collections; | |
namespace ConsoleApplication15 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Group group = new Group(0); | |
group.addStudent(new Student("Sidor", "Sidorovitch", "Sidorov")); | |
group.addStudent(new Student("Ivan", "Ivanovitch", "Ivanov")); | |
group.addStudent(new Student("Semen", "Semenovith", "Semenov")); | |
group.addStudent(new Student("Alexander", "Alexandrovitch", "Alexandrov")); | |
Console.WriteLine("-----------------------------------------------------"); | |
foreach (Student item in group) | |
{ | |
Console.WriteLine(item.Name + " | " + item.FatherName + " | " + item.Surname); | |
} | |
Console.WriteLine("-----------------------------------------------------"); | |
Group.Print(group); | |
Console.WriteLine("-----------------------------------------------------"); | |
group.Sort(new Student.SortByName()); | |
Group.Print(group); | |
Console.WriteLine("-----------------------------------------------------"); | |
Student st = group.Search(new Group.SearchStudentByName("Alexander")); | |
if (st != null) | |
{ | |
Console.WriteLine(st.Name + " | " + st.FatherName + " | " + st.Surname); | |
} | |
} | |
} | |
public class ObjectIsNotStudent : Exception | |
{ | |
public ObjectIsNotStudent() : base() { } | |
} | |
public interface IStudentSearcher | |
{ | |
void SetStudents(Student[] students); | |
Student Search(); | |
} | |
public class Student : IComparable | |
{ | |
private String name; | |
private String fatherName; | |
private String surname; | |
private DateTime birthday; | |
private String address; | |
private String phoneNumber; | |
private static int courseWorkCount = 5; | |
private static int creditCount = 5; | |
private static int examCount = 5; | |
private int[] courseWorkMarks = new int[courseWorkCount]; | |
private int[] creditMarks = new int[creditCount]; | |
private int[] examMarks = new int[examCount]; | |
public int CompareTo(Object obj) | |
{ | |
Student temp = obj as Student; | |
if (temp == null) | |
{ | |
throw new ObjectIsNotStudent(); | |
} | |
int diff = 0; | |
diff = Birthday.Year - temp.Birthday.Year; | |
if (diff != 0) | |
return diff; | |
diff = Birthday.Month - temp.Birthday.Month; | |
if (diff != 0) | |
return diff; | |
diff = Birthday.Day - temp.Birthday.Day; | |
return diff; | |
} | |
public class SortByName : IComparer | |
{ | |
public int Compare(Object obj1, Object obj2) | |
{ | |
Student s1 = obj1 as Student; | |
Student s2 = obj2 as Student; | |
if (s1 == null) | |
{ | |
throw new ObjectIsNotStudent(); | |
} | |
if (s2 == null) | |
{ | |
throw new ObjectIsNotStudent(); | |
} | |
return String.Compare(s1.Name, s2.Name); | |
} | |
} | |
public class SortBySurname : IComparer | |
{ | |
public int Compare(Object obj1, Object obj2) | |
{ | |
Student s1 = obj1 as Student; | |
Student s2 = obj2 as Student; | |
if (s1 == null) | |
{ | |
throw new ObjectIsNotStudent(); | |
} | |
if (s2 == null) | |
{ | |
throw new ObjectIsNotStudent(); | |
} | |
return String.Compare(s1.Surname, s2.Surname); | |
} | |
} | |
public class SortByFatherName : IComparer | |
{ | |
public int Compare(Object obj1, Object obj2) | |
{ | |
Student s1 = obj1 as Student; | |
Student s2 = obj2 as Student; | |
if (s1 == null) | |
{ | |
throw new ObjectIsNotStudent(); | |
} | |
if (s2 == null) | |
{ | |
throw new ObjectIsNotStudent(); | |
} | |
return String.Compare(s1.FatherName, s2.FatherName); | |
} | |
} | |
public String Name | |
{ | |
get | |
{ | |
return name; | |
} | |
set | |
{ | |
setName(value); | |
} | |
} | |
public String FatherName | |
{ | |
get | |
{ | |
return fatherName; | |
} | |
set | |
{ | |
setFatherName(value); | |
} | |
} | |
public String Surname | |
{ | |
get | |
{ | |
return surname; | |
} | |
set | |
{ | |
setSurname(value); | |
} | |
} | |
public DateTime Birthday | |
{ | |
get | |
{ | |
return getBirthday(); | |
} | |
set | |
{ | |
setBirthday(value); | |
} | |
} | |
public String Address | |
{ | |
get { return getAddress(); } | |
set { setAddress(value); } | |
} | |
public String Phone | |
{ | |
get { return getPhoneNumber(); } | |
set { setPhoneNumber(value); } | |
} | |
public Student(String name, String fatherName, String surname, | |
DateTime birthday, String address, String phoneNumber) | |
{ | |
setName(name); | |
setFatherName(fatherName); | |
setSurname(surname); | |
setBirthday(birthday); | |
setAddress(address); | |
setPhoneNumber(phoneNumber); | |
fillDefaultMarkValues(); | |
} | |
public Student() : this("Unknown", "Unknown", "Unknown", new DateTime(1900, 1, 1), "", "") { } | |
public Student(String name, String fatherName, String surname) : | |
this(name, fatherName, surname, new DateTime(1900, 1, 1), "", "") | |
{ } | |
public void setName(String name) | |
{ | |
this.name = name; | |
} | |
public void setFatherName(String fatherName) | |
{ | |
this.fatherName = fatherName; | |
} | |
public void setSurname(String surname) | |
{ | |
this.surname = surname; | |
} | |
public void setBirthday(DateTime birthday) | |
{ | |
this.birthday = birthday; | |
} | |
public void setAddress(String address) | |
{ | |
this.address = address; | |
} | |
public void setPhoneNumber(String phoneNumber) | |
{ | |
this.phoneNumber = phoneNumber; | |
} | |
public void setCourseWorkMark(int mark, int index) | |
{ | |
if ((index > courseWorkCount - 1) || (index < 0)) | |
{ | |
return;//an exception should be here | |
} | |
courseWorkMarks[index] = mark; | |
} | |
public void setCreditkMark(int mark, int index) | |
{ | |
if ((index > creditCount - 1) || (index < 0)) | |
{ | |
return;//an exception should be here | |
} | |
creditMarks[index] = mark; | |
} | |
public void setExamMark(int mark, int index) | |
{ | |
if ((index > examCount - 1) || (index < 0)) | |
{ | |
return;//an exception should be here | |
} | |
examMarks[index] = mark; | |
} | |
public String getName() | |
{ | |
return name; | |
} | |
public String getFatherName() | |
{ | |
return fatherName; | |
} | |
public String getSurname() | |
{ | |
return surname; | |
} | |
public DateTime getBirthday() | |
{ | |
return birthday; | |
} | |
public String getAddress() | |
{ | |
return address; | |
} | |
public String getPhoneNumber() | |
{ | |
return phoneNumber; | |
} | |
public int getCourseWorkMark(int index) | |
{ | |
if ((index > courseWorkCount - 1) || (index < 0)) | |
{ | |
return 0;//an exception should be here | |
} | |
return courseWorkMarks[index]; | |
} | |
public int getCreditMark(int index) | |
{ | |
if ((index > examCount - 1) || (index < 0)) | |
{ | |
return 0;//an exception should be here | |
} | |
return examMarks[index]; | |
} | |
public int getExamMark(int index) | |
{ | |
if ((index > examCount - 1) || (index < 0)) | |
{ | |
return 0;//an exception should be here | |
} | |
return examMarks[index]; | |
} | |
public void showStudentInfo() | |
{ | |
Console.WriteLine("Name: " + name); | |
Console.WriteLine("Father name: " + fatherName); | |
Console.WriteLine("Surname: " + surname); | |
Console.WriteLine("Birthday: " + birthday.ToString()); | |
Console.WriteLine("Address: " + address); | |
Console.WriteLine("Phone number: " + phoneNumber); | |
showMarks(); | |
} | |
public int calculateAvarageMark() | |
{ | |
int summMark = 0; | |
int divider = 0; | |
calculateCreditMarkSumm(ref summMark, ref divider); | |
calculateCourseWorkMarkSumm(ref summMark, ref divider); | |
calculateExamMarkSumm(ref summMark, ref divider); | |
if (divider == 0) | |
return 0; | |
else | |
return summMark / divider; | |
} | |
public void fillCreditMarkArrayWithMark(int mark) | |
{ | |
for (int i = 0; i < creditMarks.Length; i++) | |
{ | |
setCreditkMark(mark, i); | |
} | |
} | |
public void fillCourseWorkMarkArrayWithMark(int mark) | |
{ | |
for (int i = 0; i < courseWorkMarks.Length; i++) | |
{ | |
setCourseWorkMark(mark, i); | |
} | |
} | |
public void fillExamMarkArrayWithMark(int mark) | |
{ | |
for (int i = 0; i < examMarks.Length; i++) | |
{ | |
setExamMark(mark, i); | |
} | |
} | |
private void calculateCreditMarkSumm(ref int summMark, ref int divider) | |
{ | |
divider++; | |
for (int i = 0; i < creditMarks.Length; i++) | |
{ | |
summMark += creditMarks[i]; | |
} | |
} | |
private void calculateCourseWorkMarkSumm(ref int summMark, ref int divider) | |
{ | |
divider++; | |
for (int i = 0; i < courseWorkMarks.Length; i++) | |
{ | |
summMark += courseWorkMarks[i]; | |
} | |
} | |
private void calculateExamMarkSumm(ref int summMark, ref int divider) | |
{ | |
divider++; | |
for (int i = 0; i < examMarks.Length; i++) | |
{ | |
summMark += examMarks[i]; | |
} | |
} | |
private void showMarks() | |
{ | |
showCourseWorkMarks(); | |
showCreditkMarks(); | |
showExamMarks(); | |
} | |
private void showCourseWorkMarks() | |
{ | |
Console.Write("Couse work marks: "); | |
for (int i = 0; i < courseWorkMarks.Length; i++) | |
{ | |
Console.Write("{0, 4}", courseWorkMarks[i]); | |
} | |
Console.WriteLine(); | |
} | |
private void showCreditkMarks() | |
{ | |
Console.Write("Credit marks: "); | |
for (int i = 0; i < creditMarks.Length; i++) | |
{ | |
Console.Write("{0, 4}", creditMarks[i]); | |
} | |
Console.WriteLine(); | |
} | |
private void showExamMarks() | |
{ | |
Console.Write("Exam marks: "); | |
for (int i = 0; i < examMarks.Length; i++) | |
{ | |
Console.Write("{0, 4}", examMarks[i]); | |
} | |
Console.WriteLine(); | |
} | |
private void fillDefaultMarkValues() | |
{ | |
fillArrayWithZero(courseWorkMarks); | |
fillArrayWithZero(creditMarks); | |
fillArrayWithZero(examMarks); | |
} | |
private void fillArrayWithZero(int[] markArray) | |
{ | |
for (int i = 0; i < markArray.Length; i++) | |
markArray[i] = 0; | |
} | |
} | |
//public class SurnameCompare : IComparer | |
//{ | |
// public int Compare(object a, object b) | |
// { | |
// Student studentA = (Student)a; | |
// Student studentB = (Student)b; | |
// return studentA.getSurname().CompareTo(studentB.getSurname()); | |
// } | |
//} | |
public class Group : IEnumerable | |
{ | |
private Student[] students; | |
private int studentCount; | |
private String groupName; | |
private String groupSpecialization; | |
private int courseNumber; | |
public int StudentCount | |
{ | |
get { return getStudnetCount(); } | |
set { setStudentCount(value); } | |
} | |
public String GroupName | |
{ | |
get | |
{ | |
return getGroupName(); | |
} | |
set | |
{ | |
setGroupName(value); | |
} | |
} | |
public String GroupSpecialization | |
{ | |
get | |
{ | |
return getGroupSpecialization(); | |
} | |
set | |
{ | |
setGroupSpecialization(value); | |
} | |
} | |
public int CourseNumber | |
{ | |
get | |
{ | |
return getCourseNumber(); | |
} | |
set | |
{ | |
setCourseNumber(value); | |
} | |
} | |
public class SearchStudentByName : IStudentSearcher | |
{ | |
private Student[] students; | |
private String name; | |
public SearchStudentByName(String name) | |
{ | |
this.name = name; | |
} | |
public Student Search() | |
{ | |
if (students == null) | |
{ | |
return null; | |
} | |
foreach (Student item in students) | |
{ | |
if (item.Name.Equals(name)) | |
{ | |
return item; | |
} | |
} | |
return null; | |
} | |
public void SetStudents(Student[] students) | |
{ | |
this.students = students; | |
} | |
} | |
public class SearchStudentBySurname : IStudentSearcher | |
{ | |
private Student[] students; | |
private String surname; | |
public SearchStudentBySurname(String surname) | |
{ | |
this.surname = surname; | |
} | |
public Student Search() | |
{ | |
if (students == null) | |
{ | |
return null; | |
} | |
foreach (Student item in students) | |
{ | |
if (item.Surname.Equals(surname)) | |
{ | |
return item; | |
} | |
} | |
return null; | |
} | |
public void SetStudents(Student[] students) | |
{ | |
this.students = students; | |
} | |
} | |
public Student Search(IStudentSearcher criteria) | |
{ | |
criteria.SetStudents(students); | |
return criteria.Search(); | |
} | |
public void Sort(IComparer criteria) | |
{ | |
Array.Sort(students, criteria); | |
} | |
public static void Print(Group group) | |
{ | |
foreach (Student item in group) | |
{ | |
Console.WriteLine(item.Name + " | " + item.FatherName + " | " + item.Surname); | |
} | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return (IEnumerator)GetEnumerator(); | |
} | |
public GroupEum GetEnumerator() | |
{ | |
return new GroupEum(students); | |
} | |
public void addStudent(Student new_student) | |
{ | |
Student[] prev_students = this.students; | |
students = new Student[studentCount + 1]; | |
if (prev_students != null) | |
{ | |
for (int i = 0; i < prev_students.Length; i++) | |
{ | |
students[i] = prev_students[i]; | |
} | |
} | |
students[studentCount] = new_student; | |
studentCount++; | |
sortStudentsBySurname(); | |
} | |
public Student getStudentByIndex(int index) | |
{ | |
if (index < 0 || index > students.Length - 1) | |
return null;//must be exception; | |
else | |
return students[index]; | |
} | |
public void deleteStudentByIndex(int index) | |
{ | |
if (index < 0 || index > students.Length - 1) | |
return;//must be exception; | |
Student[] prev_students = this.students; | |
students = new Student[studentCount - 1]; | |
int shift = 0; | |
for (int i = 0; i < prev_students.Length; i++) | |
{ | |
if (i == index) | |
{ | |
shift++; | |
continue; | |
} | |
students[i - shift] = prev_students[i]; | |
} | |
studentCount--; | |
sortStudentsBySurname(); | |
} | |
public void setStudentCount(int studentCount) | |
{ | |
this.studentCount = studentCount; | |
} | |
public int getStudnetCount() | |
{ | |
return studentCount; | |
} | |
public void setGroupName(String groupName) | |
{ | |
this.groupName = groupName; | |
} | |
public String getGroupName() | |
{ | |
return groupName; | |
} | |
public void setGroupSpecialization(String groupSpecialization) | |
{ | |
this.groupSpecialization = groupSpecialization; | |
} | |
public String getGroupSpecialization() | |
{ | |
return groupSpecialization; | |
} | |
public void setCourseNumber(int courseNumber) | |
{ | |
this.courseNumber = courseNumber; | |
} | |
public int getCourseNumber() | |
{ | |
return courseNumber; | |
} | |
public void showGroup() | |
{ | |
Console.WriteLine("Group name: " + groupName); | |
Console.WriteLine("Group spesialization: " + groupSpecialization); | |
Console.WriteLine("Course number: " + courseNumber.ToString()); | |
Console.WriteLine("Student count: " + studentCount.ToString()); | |
Console.WriteLine("----------------------------------------"); | |
for (int i = 0; i < students.Length; i++) | |
{ | |
Console.WriteLine((i + 1).ToString() + ". " + students[i].getSurname() + " " + students[i].getName() + | |
" " + students[i].getFatherName() + ", " + students[i].calculateAvarageMark().ToString()); | |
} | |
} | |
public Group() | |
{ | |
fillGroupAttributesWithDefaultValues(); | |
addStudentAutomaticaly(10); | |
} | |
public Group(int studentCount) | |
{ | |
fillGroupAttributesWithDefaultValues(); | |
addStudentAutomaticaly(studentCount); | |
} | |
public Group(Student[] students) | |
{ | |
fillGroupAttributesWithDefaultValues(); | |
foreach (var item in students) | |
{ | |
addStudent(item); | |
} | |
} | |
public Group(String groupName, String groupSpecialization, int courseNumber) | |
{ | |
setGroupName(groupName); | |
setGroupSpecialization(groupSpecialization); | |
setCourseNumber(courseNumber); | |
} | |
public void deleteStudentWithLowestMark() | |
{ | |
if (studentCount == 0) | |
{ | |
return; | |
} | |
int index = 0; | |
int lowestMark = 1000; | |
for (int i = 0; i < students.Length; i++) | |
{ | |
if (students[i].calculateAvarageMark() < lowestMark) | |
{ | |
lowestMark = students[i].calculateAvarageMark(); | |
index = i; | |
} | |
} | |
deleteStudentByIndex(index); | |
} | |
public void setStudentData(int studentIndex, String name, String fatherName, String surname) | |
{ | |
Student currentStudent = getStudentByIndex(studentIndex); | |
currentStudent.setName(name); | |
currentStudent.setFatherName(fatherName); | |
currentStudent.setSurname(surname); | |
sortStudentsBySurname(); | |
} | |
public void moveStudentToAnotherGroup(int studentIndex, Group newGroup) | |
{ | |
newGroup.addStudent(getStudentByIndex(studentIndex)); | |
deleteStudentByIndex(studentIndex); | |
} | |
private void fillGroupAttributesWithDefaultValues() | |
{ | |
setGroupName("Unknown"); | |
setGroupSpecialization("Unknown"); | |
setCourseNumber(0); | |
} | |
private void addStudentAutomaticaly(int count) | |
{ | |
for (int i = 0; i < count; i++) | |
{ | |
Student newStudent = new Student("Unknown" + i.ToString(), "Unknown" + i.ToString(), "Unknown" + i.ToString()); | |
newStudent.fillCourseWorkMarkArrayWithMark(i + 1); | |
newStudent.fillCreditMarkArrayWithMark(i + 1); | |
newStudent.fillExamMarkArrayWithMark(i + 1); | |
addStudent(newStudent); | |
} | |
} | |
private void sortStudentsBySurname() | |
{ | |
//IComparer comparer = new SurnameCompare(); | |
//Array.Sort(students, comparer); | |
} | |
} | |
public class GroupEum : IEnumerator | |
{ | |
public Student[] _students; | |
int position = -1; | |
public GroupEum(Student[] list) | |
{ | |
_students = list; | |
} | |
public bool MoveNext() | |
{ | |
position++; | |
return (position < _students.Length); | |
} | |
public void Reset() | |
{ | |
position = -1; | |
} | |
object IEnumerator.Current | |
{ | |
get | |
{ | |
return Current; | |
} | |
} | |
public Student 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