Created
December 22, 2017 19:34
-
-
Save skhozinova/6662e13e80f864c41e640525338ff8ce to your computer and use it in GitHub Desktop.
This file contains 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 student; | |
namespace student | |
{ | |
interface IStudentsSort <Students> | |
{ | |
Students SortByYearAndLetter(bool descending); | |
Students SortByYear(bool descending); | |
Students SortByLetter(bool descending); | |
} | |
} |
This file contains 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; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Linq; | |
using student; | |
/*Описать класс УЧЕНИК (поля: ФИО, ГОД ОБУЧЕНИЯ, НАЗВАНИЯ КЛАССА (БУКВА a-д)). | |
Статический метод класса: сортировка массива учеников по паре (год обучения, название класса); | |
Функция демонстрационной программы: удаление ученика с заданной ФИО из массива. | |
Операция класса: перевод ученика в следующий класс (++) done*/ | |
namespace program | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Students students = new Students | |
{ | |
new Student(161256,"иван", "петров", 1, "a"), | |
new Student(161257,"николай", "алимов ", 2, "b"), | |
new Student(161258,"кира", "селезнева", 3, "c"), | |
new Student(161259,"мария", "поддубная", 2, "a"), | |
new Student(161260,"анна", "соколова", 1, "a"), | |
}; | |
bool q = true; | |
while (q) | |
{ | |
Console.WriteLine(@"Выберите действие: | |
1 - Вывод имеющегося списка студентов | |
2 - Добавление данных об ученике | |
3 - Вывод списка учеников на экран | |
4 - Сортировка массива учеников по паре (год обучения, название класса) | |
5 - Перевод учеников на следующий год | |
6 - Удаление ученика с заданным ID из списка | |
0 - Выход"); | |
try | |
{ | |
int d = int.Parse(Console.ReadLine()); | |
switch (d) | |
{ | |
case 0: | |
{ | |
q = false; | |
Console.WriteLine("Программа завершила работу"); break; | |
} | |
case 1: | |
{ | |
Students.Display(students); | |
break; | |
} | |
case 2: students.Add(StudentReader.UserInput()); break; | |
case 3: Students.Display(students); | |
break; | |
case 4: Students.Display(students.SortByYearAndLetter()); break; | |
case 5: students.Promote(); Students.Display(students); break; | |
case 6: | |
{ | |
Console.WriteLine("\nВведите ID ученика, данные о котором хотите удалить: "); | |
int id = int.Parse(Console.ReadLine()); | |
bool result = Students.RemoveStudent(id, students); | |
Console.WriteLine(result); | |
Students.Display(students); | |
break; | |
} | |
default: Console.WriteLine("Ошибка ввода\n"); break; | |
} // switch(d) | |
}//try | |
catch (Exception e) { Console.WriteLine(e.Message); } | |
Console.WriteLine("\nДля продолжения нажмите Enter"); | |
Console.ReadKey(); | |
} | |
//while | |
} | |
static Student[] students = new Student[0]; | |
} | |
} |
This file contains 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; | |
namespace student | |
{ | |
public class Student | |
{ | |
private int id; | |
private string name; | |
private string lastname; | |
private short year; | |
private string letter; | |
public string Name { get { return name;} set {name = Student.FirstCapital(value);}} | |
public string Lastname { get { return lastname; } set { lastname = Student.FirstCapital(value); } } | |
public string Letter { get { return letter;} set {letter = value.ToUpper();}} | |
public static string FirstCapital(string value) //берет первую букву из строки, делает ее заглавной и прибавляет оставшуюся часть строки | |
{ | |
return value[0].ToString().ToUpper() + value.Substring(1); | |
} | |
public short Year | |
{ | |
get { return year;} | |
set | |
{ | |
if (value >= 1 && value <= 11) | |
{ | |
year = value; | |
} | |
else throw new Exception("Year should be a positive number in range 1-11"); | |
} | |
} | |
public int ID | |
{ | |
get { return id; } | |
set | |
{ | |
if (value>=100000 && value <= 999999) | |
{ | |
id = value; | |
} | |
else throw new Exception("ID should be a positive number in range 100000-999999"); | |
} | |
} | |
public Student(int id, string name, string lastname, short year, string letter) | |
{ | |
ID = id; | |
Name = name; | |
Lastname = lastname; | |
Year = year; | |
Letter = letter; | |
} | |
public void Display() | |
{ | |
Console.WriteLine("Student: {0} {1} {2} {3} {4}", ID, Name, Lastname, Year, Letter); | |
} | |
public void Promote() | |
{ | |
Year = ++Year; | |
} | |
} | |
} |
This file contains 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.Collections.ObjectModel; | |
using System.Linq; | |
using student; | |
namespace student | |
{ | |
public class Students : Collection<Student>, IStudentsSort<Students>// создание специальной коллекции класса Student | |
{ | |
public Students() | |
{ | |
} | |
public Students(List<Student> list) | |
: base(list)//Определение конструктора базового класса, который должен вызываться при создании экземпляров производного класса | |
{ | |
} | |
public Students GetStudentsByYear(short year) | |
{ | |
return new Students(Items.Where(student => student.Year == year).ToList()); | |
} | |
public static Boolean RemoveStudent(int id, Students students) | |
{ | |
foreach (Student student in students.ToList()) | |
{ | |
if (student.ID == id) | |
{ | |
return students.Remove(student); | |
} | |
} | |
return false; | |
} | |
public Students SortByYear(bool descending = false) | |
{ | |
if (descending) | |
{ | |
return new Students(Items.OrderByDescending(x => x.Year).ToList()); | |
} | |
else | |
{ | |
return new Students(Items.OrderBy(x => x.Year).ToList()); | |
} | |
} | |
public Students SortByLetter(bool descending = false) | |
{ | |
if (descending) | |
{ | |
return new Students(Items.OrderByDescending(x => x.Letter).ToList()); | |
} | |
else | |
{ | |
return new Students(Items.OrderBy(x => x.Letter).ToList()); | |
} | |
} | |
public Students SortByYearAndLetter(bool descending = false) | |
{ | |
if (descending) | |
{ | |
return new Students(Items.OrderByDescending(x => x.Year).ThenByDescending(x => x.Letter).ToList()); | |
} | |
else | |
{ | |
return new Students(Items.OrderBy(x => x.Year).ThenBy(x => x.Letter).ToList()); | |
} | |
} | |
public static void Display(Students students) | |
{ | |
foreach (Student student in students) | |
{ | |
student.Display(); | |
} | |
Console.WriteLine(); | |
} | |
public Students GetByLetter(string letter) | |
{ | |
string upper_letter = letter.ToUpper(); | |
return new Students(Items.Where(student => student.Letter == upper_letter).ToList()); | |
} | |
public Students GetByYear(short year) | |
{ | |
return new Students(Items.Where(student => student.Year == year).ToList()); | |
} | |
public void Promote() | |
{ | |
foreach (Student student in Items) | |
{ | |
student.Promote(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment