On assignment you need
Describe the class "student group".
Provide for the possibility of working with a variable number of students
search for a student by any sign (for example, by name,
surnames, date of birth), adding and deleting records, sorting by different fields, accessing records by number.

Implemented a separate class Student

namespace Class1 { sealed class Student { public Student(string name, string surname, string dateOfBirthday) { Name = name; Surname = surname; DateOfBirthday = dateOfBirthday; } public void ChangeName(Student student, string name) { if (student != null && student.Name != name) { student.Name = name; } } public void ChangeSurname(Student student, string surname) { if (student != null && student.Name != surname) { student.Surname = surname; } } public void ChangeDateOfBirthday(Student student, string dateOfBirthday) { if (student != null && student.Name != dateOfBirthday) { student.DateOfBirthday = dateOfBirthday; } } public string Name { get; set; } public string Surname { get; set; } public string DateOfBirthday { get; set; } } } 

then class StudentGroup

 using System; using System.Collections.Generic; namespace Class1 { class StudentGroup { List<Student> students = new List<Student>(); public void AddStudent(string name, string surname, string dateOfBirthday) { Student student = new Student(name, surname, dateOfBirthday); students.Add(student); } public void RemoveStudent(Student student) { students.Remove(student); } public string GetStudent(int id) { return students[id].Name + " " + students[id].Surname + " " + students[id].DateOfBirthday; } public void ShowAllStudent() { Console.WriteLine("Все студенты: "); foreach (var student in students) { Console.WriteLine(student.Name + "\t" + student.Surname + "\t" + student.DateOfBirthday); } } public int GetAmount() { return students.Count; } public string FindByName(StudentGroup studentGroup, string name) { } } } 

and in the program

 using System; namespace Class1 { class Program { static void Main(string[] args) { var studentGroup = new StudentGroup(); studentGroup.AddStudent("Masha", "Ivanova", "30.07.1986"); studentGroup.AddStudent("Petya", "Petrov", "28.06.1985"); studentGroup.AddStudent("Ivan", "Sidorov", "05.09.1987"); Console.Write("Введите id = "); int id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(studentGroup.GetStudent(id)); Console.Write("Введите имя: "); string name = Console.ReadLine(); Console.Write("Введите фамилию: "); string surname = Console.ReadLine(); Console.Write("Введите дату рождения: "); string dateOfBirthday = Console.ReadLine(); studentGroup.AddStudent(name, surname, dateOfBirthday); Console.Write("Введите id = "); id = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(studentGroup.GetStudent(id)); studentGroup.ShowAllStudent(); Console.WriteLine("Общее количество студентов в группе " + studentGroup.GetAmount()); Console.ReadKey(); } } } 

I can not figure out how to make the removal method and sorting methods?

  • If I'm not mistaken, you need to go in the direction of the Linked Lists. - ili_ne

2 answers 2

student class with interface implemented IComparable;

 sealed class Student:IComparable<Student> { public Student(string name, string surname, string dateOfBirthday) { Name = name; Surname = surname; DateOfBirthday = dateOfBirthday; } public void ChangeName(Student student, string name) { if (student != null && student.Name != name) { student.Name = name; } } public void ChangeSurname(Student student, string surname) { if (student != null && student.Name != surname) { student.Surname = surname; } } public void ChangeDateOfBirthday(Student student, string dateOfBirthday) { if (student != null && student.Name != dateOfBirthday) { student.DateOfBirthday = dateOfBirthday; } } public string Name { get; set; } public string Surname { get; set; } public string DateOfBirthday { get; set; } //реализация метода CompareTo интефейса IComparable public int CompareTo(Student that) { return String.Compare(Name, that.Name, System.StringComparison.Ordinal); } } 

now you can call Sort () on the student list, and the sheet will be sorted by name

You can also pass a delegate as an argument to the Sort () method.

  groups.Sort(delegate(Student xStudent, Student yStudent) { if (xStudent.Surname == null && yStudent.Surname == null) return 0; if (xStudent.Surname == null) return -1; if (yStudent.Surname == null) return 1; return String.Compare(xStudent.Surname, yStudent.Surname, System.StringComparison.Ordinal); }); 

You can still enter into the stupor of an old teacher by writing the same thing but with the help of lambda

  groups.Sort(( x, y) => String.Compare(x.Name, y.Name, StringComparison.Ordinal)); 

    You have already done the removal (the RemoveStudent method; there you use the List method's Remove method to remove an element by value). If you want to delete not by value, but by place in the list, use the RemoveAt method. Sorting is done using the Sort method of the List class. To tell the method how to compare objects of class Student , you need one of the following conditions:

    • Student class must implement IComparable interface;
    • when you call Sort you need to pass a comparator to it (a class that implements the IComparer interface);
    • when calling Sort you need to pass a delegate to it that compares objects of the class Student .