Создать класс public Student{} . Атрибуты класса- public string firstName, public string lastName , Public int age, public string group
Создать класс public Student{} . Атрибуты класса- public string firstName, public string lastName , Public int age, public string group. В классе Student создать метод, который будет заполнять данные о студентах с клавиатуры. public static Student GetStudent(){} Так же создать метод, который будет выводить информацию о студентах на экран. Public static void Print(Student s){} В методе main: Создать массив Student [] arr. Создать 4 объекта класса Student(), 4 разных студентов и поместить их в массив. Создать метод, который будет выбирать из массива и выводить на экран только тех студентов, возраст которых больше 18 лет.
Не знаю как создать метод GetStudents и Print, помогите разобраться что не так в коде и что нужно добавить в Main
class Program : Student
{
static void Main(string[] args)
{
Student[] students;
//Student Ivanov = new Student();
//Student Smirnov = new Student();
//Student Kotov = new Student();
//Student Ivanova = new Student();
//arr = new Student[] { Ivanov, Smirnov, Kotov, Ivanova };
//GetStudent("Ivanov", "Ivan", 16, "ИС23");
}
public static void GetAdult(Student[] students)
{
}
}
// Задание 7. Классы
public class Student
{
public static Student GetAdult(Student[] arr)
{
Student student = new Student();
foreach (var item in arr)
{
Console.WriteLine(item);
}
return student;
}
public string firstName { get; set; }
public string lastName { get; set; }
public int age { get; set; }
public string group { get; set; }
// заполняет данные о студентах с клавиатуры
public static Student GetStudent(string firstName, string lastName, int age, string group)
{
Student student = new Student();
Console.WriteLine("Заполни информацию о студенте: ");
Console.WriteLine("Введите имя: ");
firstName = Console.ReadLine();
Console.WriteLine("Введите фамилию: ");
lastName = Console.ReadLine();
Console.WriteLine("Введите возраст: ");
age = Int32.Parse(Console.ReadLine());
Console.WriteLine("Введите группу: ");
group = Console.ReadLine();
return student;
}
// выводит информацию о студентах
public static Student Print(Student s)
{
Console.WriteLine("Имя: " + s.firstName);
Console.WriteLine("Фамилия: " + s.lastName);
Console.WriteLine("Возраст: " + s.age);
Console.WriteLine("Группа: " + s.group);
return s;
}
}
Ответы (1 шт):
using System;
using System.Linq;
using System.Collections.Generic;
#pragma warning disable
internal static class Application
{
private static void Main()
{
Student[] students = new Student[4];
for (int iterator = 0; iterator < students.Length; iterator++)
students[iterator] = new Student().MakeStudent();
students.Where(student => student.Age > 18).ForEach();
}
private static void ForEach(this IEnumerable<IPrintable> sources)
{
foreach (IPrintable source in sources)
source.Print();
}
}
internal class Student : IPrintable
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public int Age { get; private set; }
public string Group { get; private set; }
public Student MakeStudent()
{
Student student = new Student();
Console.WriteLine("Заполни информацию о студенте: ");
Console.Write("Введите имя: ");
FirstName = Console.ReadLine() ?? string.Empty;
Console.Write("Введите фамилию: ");
LastName = Console.ReadLine() ?? string.Empty;
Console.Write("Введите возраст: ");
if (int.TryParse(Console.ReadLine() ?? string.Empty, out int result))
Age = result;
Console.Write("Введите группу: ");
Group = Console.ReadLine() ?? string.Empty;
return student;
}
public void Print()
{
Console.WriteLine($"Имя: {FirstName}");
Console.WriteLine($"Фамилия: {LastName}");
Console.WriteLine($"Возраст: {Age}");
Console.WriteLine($"Группа: {Group}");
}
}
public interface IPrintable
{
void Print();
}