Created
March 2, 2017 20:03
-
-
Save Soulstorm50/5b5f164ccb87f8cddd217d7793ad4935 to your computer and use it in GitHub Desktop.
на экране пара "идентификатор - значение"
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; | |
using System.Reflection; | |
namespace ConsoleApplication17 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Student s = new Student(); | |
foreach (var x in s) | |
{ | |
Console.WriteLine(x); // на экране пара "идентификатор - значение" | |
} | |
} | |
} | |
public class StrudentMemberInfo | |
{ | |
public String Name | |
{ | |
get; | |
} | |
public String Type | |
{ | |
get; | |
} | |
public String Value | |
{ | |
get; | |
} | |
public StrudentMemberInfo(String name, String type, String value) | |
{ | |
Name = name; | |
Type = type; | |
Value = value; | |
} | |
public override String ToString() | |
{ | |
return "Name: " + Name + "; Type: " + Type + "; Value: " + Value; | |
} | |
} | |
public class ObjectIsNotStudent : Exception | |
{ | |
public ObjectIsNotStudent() : base() { } | |
} | |
public class Student : IComparable, IEnumerable | |
{ | |
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]; | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return (IEnumerator)GetEnumerator(); | |
} | |
public StudentEnum GetEnumerator() | |
{ | |
return new StudentEnum(this); | |
} | |
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 StudentEnum : IEnumerator | |
{ | |
private StrudentMemberInfo[] _items; | |
private int position = -1; | |
public StudentEnum(Student student) | |
{ | |
Type classType = typeof(Student); | |
FieldInfo[] fields = classType.GetFields(BindingFlags.Public | BindingFlags.Instance); | |
PropertyInfo[] properties = classType.GetProperties(); | |
MethodInfo[] methods = classType.GetMethods(); | |
int itemCount = ((fields == null) ? 0 : fields.Length) + ((properties == null) ? 0 : properties.Length) | |
+ ((methods == null) ? 0 : methods.Length); | |
_items = new StrudentMemberInfo[itemCount]; | |
for (int i = 0; i < fields.Length; i++) | |
{ | |
_items[i] = new StrudentMemberInfo(fields[i].Name, "field", fields[i].GetValue(student).ToString()); | |
} | |
int offset = fields.Length; | |
for (int i = 0; i < properties.Length; i++) | |
{ | |
if (properties[i].CanRead) | |
{ | |
_items[i + offset] = new StrudentMemberInfo(properties[i].Name, "property", properties[i].GetValue(student).ToString()); | |
} | |
else | |
{ | |
_items[i + offset] = new StrudentMemberInfo(properties[i].Name, "property", ""); | |
} | |
} | |
offset += properties.Length; | |
for (int i = 0; i < methods.Length; i++) | |
{ | |
_items[i + offset] = new StrudentMemberInfo(methods[i].Name, "method", ""); | |
} | |
} | |
public Boolean MoveNext() | |
{ | |
position++; | |
return (position < _items.Length); | |
} | |
public void Reset() | |
{ | |
position = -1; | |
} | |
Object IEnumerator.Current | |
{ | |
get | |
{ | |
return Current; | |
} | |
} | |
public StrudentMemberInfo Current | |
{ | |
get | |
{ | |
try | |
{ | |
return _items[position]; | |
} | |
catch (IndexOutOfRangeException) | |
{ | |
throw new InvalidOperationException(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment