Вывести на экран всех студентов одной группы - C#
Формулировка задачи:
Подскажите пожалуйста как реализовать поиск.Нужно вывести на экран всех студентов одной группы.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace FirstEnum
{
class Program
{
class Student
{
public string FIO { get; set; }
public string Group { get; set; }
public string Predmet { get; set; }
public int Ocenka { get; set; }
public string Otch { get; set; }
public override string ToString()
{
return string.Format("Фамилия:{0},Имя и Отчество:{1},Группа:{2},Предмет:{3},Оценка:{4}", FIO, Otch, Group, Predmet, Ocenka );
}
}
static void Main(string[] args)
{
bool kones = true;
ArrayList al = new ArrayList();
while (kones)
{
Console.WriteLine();
PrintMessage();
int a = int.Parse(Console.ReadLine());
if (a == 0)
{
AddNewStudent(al);
}
else if (a == 1)
{
PrintAllStudent(al);
}
else if (a == 2)
{
FindStudent(al);
}
else if (a == 3)
{
FindGroup(al);
}
else if (a == 4)
{
PodschetOtl(al);
}
else if (a == 8)
{
SaveToFiles(al);
}
else kones = false;
}
}
private static void SaveToFiles(ArrayList al)
{
DirectoryInfo di = new DirectoryInfo("Data");
di.Create();
StreamWriter sw = File.CreateText("D:\\student.txt");
foreach (var item in al)
{
Student st = (Student)item;
sw.WriteLine(st.ToString());
}
sw.Close();
}
private static void FindStudent(ArrayList al)
{
Console.WriteLine("Введите фамилию:");
string findFIO = Console.ReadLine();
bool fd = false;
Student findSt = new Student();
foreach (var item in al)
{
Student st = (Student)item;
if (findFIO == st.FIO)
{
findSt = st;
fd = true;
break;
}
}
if (fd) { Console.WriteLine(findSt.ToString()); }
else { Console.WriteLine("Студент не найден"); }
}
private static void FindGroup(ArrayList al)
{
Console.WriteLine("Введите группу :");
string findGroup = Console.ReadLine();
bool ft = false;
Student findSt = new Student();
foreach (var item in al)
{
Student st = (Student)item;
if(findGroup == st.Group)
{
findSt = st;
ft = true;
break;
}
}
if (ft) { Console.WriteLine(findSt.ToString()); }
else { Console.WriteLine("Студент не найден"); }
}
private static void PodschetOtl(ArrayList al)
{
}
private static void PrintMessage()
{
Console.WriteLine("Для добавления студента нажмите на 0");
Console.WriteLine("Для получения списка студентов нажмите на 1");
Console.WriteLine("Для поиска студента по фамилии нажмите на 2");
Console.WriteLine("Для поиска группы студетов нажмите на 3");
Console.WriteLine("Для поиска для подсчета отличников нажмите на 4");
Console.WriteLine("Для сохранения нажмите на 8");
Console.WriteLine("Для выхода из приложения на 9");
}
private static void PrintAllStudent(ArrayList al)
{
FileStream file1 = new FileStream("D:\\student.txt", FileMode.Open);
StreamReader reader = new StreamReader(file1);
Console.WriteLine(reader.ReadToEnd());
reader.Close();
Console.ReadLine();
}
private static void AddNewStudent(ArrayList al)
{
string fio; string grup; string predmet; int mark; string otcestvo;
Console.WriteLine("Введите фамилию студента:");
fio = Console.ReadLine();
Console.WriteLine("Введите имя и отчество: ");
otcestvo = Console.ReadLine();
Console.WriteLine("Группа:");
grup = Console.ReadLine();
Console.WriteLine("Предмет:");
predmet = Console.ReadLine();
Console.WriteLine("Введите оценку студента:");
mark = int.Parse(Console.ReadLine());
al.Add(new Student { FIO = fio, Otch = otcestvo, Group= grup, Predmet = predmet, Ocenka = mark });
}
private static void LoadStudent(ArrayList al)
{
al.Add(new Student { FIO = "Pushkin", Otch = "Александр Сергеевич", Group = "230721", Predmet = "Математический анализ", Ocenka = 80 });
al.Add(new Student { FIO = "Lermontov", Otch = "Михаил Юрьевич", Group = "221543", Predmet = "Математический анализ", Ocenka = 59 });
al.Add(new Student { FIO = "Dostoevskii",Otch = "Федор Михайлович", Group = "232323", Predmet = "Математический анализ", Ocenka = 33 });
}
}
}Решение задачи: «Вывести на экран всех студентов одной группы»
textual
Листинг программы
private static void FindGroup(ArrayList al)
{
Console.WriteLine("Введите группу :");
string findGroup = Console.ReadLine();
//Поиск
var query = from Student n in al
where n.Group == findGroup
select n;
//Вывод на экран
foreach (var item in query)
Console.WriteLine(item.ToString());
}