Class Practice
unknown
csharp
2 years ago
4.6 kB
18
Indexable
Student testStudent0 = new Student() { FirstName = "TestName0", Email = "TestEmail0" };
static void ShowStudentInfo(Student student)
{
Console.WriteLine("=====================");
Console.WriteLine(student.FirstName);
Console.WriteLine(student.LastName);
Console.WriteLine(student.Email);
Console.WriteLine(student.Address);
Console.WriteLine(student.PhoneNumber);
Console.WriteLine("=====================");
}
Student testStudent1 = new Student("TestName1", "TestLastName1");
Student testStudent2 = new Student("TestName2", "TestLastName2", "test2@gmail.com");
Student testStudent3 = new Student("TestName3", "TestLastName3", "test3@gmail.com", "Test3Address");
Student testStudent4 = new Student("TestName4", "TestLastName4", "test4@gmail.com", "Test4Address", 7771234578);
Student[] students = new Student[]
{
testStudent0,
testStudent1,
testStudent2,
testStudent3,
testStudent4,
new Student("FN", "LN")
};
while (true)
{
Console.Clear();
Console.WriteLine("Меню");
Console.WriteLine("1. Показать всех студентов");
Console.WriteLine("2. Найти студента по имени");
Console.WriteLine("3. Добавить нового студента");
switch (Convert.ToInt32(Console.ReadLine()))
{
case 1:
foreach (Student student in students)
{
ShowStudentInfo(student);
}
break;
case 2:
Console.Write("Введите имя студента: ");
string inputName = Console.ReadLine();
bool isFound = false;
foreach (Student student in students)
{
if (inputName == student.FirstName)
{
isFound = true;
ShowStudentInfo(student);
break;
}
}
if (!isFound)
{
Console.WriteLine("Такого студента нет");
}
break;
case 3:
Student newStudent = InputStudent();
AddStudent(newStudent);
break;
default:
break;
}
Console.WriteLine();
Console.WriteLine("Нажмите любую клавишу");
Console.ReadKey();
}
Student InputStudent()
{
Student newStudent = new Student();
Console.WriteLine("Создание нового студента");
Console.Write("Введите имя: ");
newStudent.FirstName = Console.ReadLine();
Console.Write("Введите фамилию: ");
newStudent.LastName = Console.ReadLine();
Console.Write("Введите эл. почту: ");
newStudent.Email = Console.ReadLine();
Console.Write("Введите адрес: ");
newStudent.Address = Console.ReadLine();
Console.Write("Введите номер телефона: ");
newStudent.PhoneNumber = Convert.ToInt64(Console.ReadLine());
return newStudent;
}
void AddStudent(Student newStudent)
{
Student[] newArray = new Student[students.Length + 1];
for (int i = 0; i < students.Length; i++)
{
newArray[i] = students[i];
}
newArray[^1] = newStudent;
students = newArray;
//newArray[newArray.Length - 1] = newStudent;
}
class Student
{
public string FirstName = "Неизвестное имя";
public string LastName = "Неизвестная фамилия";
public string Email = "Неизвестная эл. почта";
public string Address = "Неизвестный адресс";
public long PhoneNumber;
public Student()
{
}
public Student(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
public Student(string firstName, string lastName, string email)
{
FirstName = firstName;
LastName = lastName;
Email = email;
}
public Student(string firstName, string lastName, string email, string address)
{
FirstName = firstName;
LastName = lastName;
Email = email;
Address = address;
}
public Student(string firstName, string lastName, string email, string address, long phoneNumber)
{
FirstName = firstName;
LastName = lastName;
Email = email;
Address = address;
PhoneNumber = phoneNumber;
}
}Editor is loading...
Leave a Comment