By GokiSoft.com|
15:27 16/10/2021|
C Sharp
[Share Code] Viết chương trình quản lý sinh viên + delegate & event trong C# - Lập Trình C# - Lập Trình C Sharp - AAHN C2009G2
Viết chương trình quản lý sinh viên + delegate & event trong C# - Lập Trình C# - Lập Trình C Sharp
#Program.cs
using System;
using System.Collections.Generic;
namespace BT1523
{
delegate void OnAddressPrint();//anonymous method
class Program
{
static List<Parent> parentList;
static List<Student> studentList;
static event OnAddressPrint EventAddressPrint;//gia lap event
static void Main(string[] args)
{
parentList = new List<Parent>();
studentList = new List<Student>();
int choose;
do
{
ShowMenu();
choose = int.Parse(Console.ReadLine());
switch(choose)
{
case 1:
InputParent();
break;
case 2:
InputStudent();
break;
case 3:
FindStudent();
break;
case 4:
FindParent();
break;
case 5:
TestDelegate();
break;
case 6:
Console.WriteLine("Thoat!!!");
break;
default:
Console.WriteLine("Nhap sai!!!");
break;
}
} while (choose != 6);
}
private static void TestDelegate()
{
//Delegate & Event
EventAddressPrint = ShowStudentInfo;
EventAddressPrint();
//Su dung dung ban chat Delegate & Event
//Delegate => su dung tao anonymous method
//Event => gia lap event.
OnAddressPrint print = () =>
{
Console.WriteLine("Nhap dia chi can tim: ");
string address = Console.ReadLine();
foreach (Student std in studentList)
{
if (std.Address == address)
{
std.Display();
}
}
};
EventAddressPrint = print;
EventAddressPrint();
}
static void ShowStudentInfo()
{
Console.WriteLine("Nhap dia chi can tim: ");
string address = Console.ReadLine();
foreach(Student std in studentList)
{
if(std.Address == address)
{
std.Display();
}
}
}
private static void FindParent()
{
Console.WriteLine("Nhap ten sinh vien can tra cuu: ");
string studentName = Console.ReadLine();
string parentNo = null;
foreach (Student std in studentList)
{
if (std.Fullname == studentName)
{
parentNo = std.ParentNo;
break;
}
}
if (parentNo != null)
{
Console.WriteLine("Phu huynh can tim: ");
foreach (Parent parent in parentList)
{
if (parent.ParentNo == parentNo)
{
parent.Display();
break;
}
}
}
else
{
Console.WriteLine("Khong tim thay thong tin sinh vien");
}
}
private static void FindStudent()
{
Console.WriteLine("Nhap ten phu huynh can tra cuu: ");
string parentName = Console.ReadLine();
string parentNo = null;
foreach(Parent parent in parentList)
{
if(parent.Fullname == parentName)
{
parentNo = parent.ParentNo;
break;
}
}
if(parentNo != null) {
Console.WriteLine("Danh sach sinh vien can tim: ");
foreach(Student std in studentList)
{
if(std.ParentNo == parentNo)
{
std.Display();
}
}
} else
{
Console.WriteLine("Khong thay phu huynh co ten {0}", parentName);
}
}
private static void InputStudent()
{
Console.WriteLine("Nhap so sinh vien can them N = ");
int N = int.Parse(Console.ReadLine());
for (int i = 0; i < N; i++)
{
Student student = new Student();
student.Input();
studentList.Add(student);
}
}
private static void InputParent()
{
Console.WriteLine("Nhap so phu huynh can them N = ");
int N = int.Parse(Console.ReadLine());
for(int i=0;i<N;i++)
{
Parent parent = new Parent();
parent.Input();
parentList.Add(parent);
}
}
static void ShowMenu()
{
Console.WriteLine("1. Nhap N phu huynh");
Console.WriteLine("2. Nhap M sinh vien");
Console.WriteLine("3. Tra cuu phu huynh theo sinh sinh");
Console.WriteLine("4. Tra cuu sinh vien theo phu huynh");
Console.WriteLine("5. Delegate & Event");
Console.WriteLine("6. Thoat");
Console.WriteLine("Chon: ");
}
}
}
#Parent.cs
using System;
namespace BT1523
{
public class Parent
{
public string Fullname { get; set; }
public string Address { get; set; }
public string Phone { get; set; }
public string ParentNo { get; set; }
public Parent()
{
}
public void Input()
{
Console.WriteLine("Nhap ten: ");
Fullname = Console.ReadLine();
Console.WriteLine("Nhap dia chi: ");
Address = Console.ReadLine();
Console.WriteLine("Nhap SDT: ");
Phone = Console.ReadLine();
Console.WriteLine("Nhap ma phu huynh: ");
ParentNo = Console.ReadLine();
}
public void Display()
{
Console.WriteLine("Ten: {0}, dia chi: {1}, sdt: {2}, ma phu huynh: {3}",
Fullname, Address, Phone, ParentNo);
}
}
}
#Student.cs
using System;
namespace BT1523
{
public class Student
{
public string Fullname { get; set; }
public int Age { get; set; }
public string RollNo { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
public string ParentNo { get; set; }
public Student()
{
}
public void Input()
{
Console.WriteLine("Nhap ten: ");
Fullname = Console.ReadLine();
Console.WriteLine("Nhap dia chi: ");
Address = Console.ReadLine();
Console.WriteLine("Nhap SDT: ");
Phone = Console.ReadLine();
Console.WriteLine("Nhap tuoi: ");
Age = int.Parse(Console.ReadLine());
Console.WriteLine("Nhap msv: ");
RollNo = Console.ReadLine();
Console.WriteLine("Nhap email: ");
Email = Console.ReadLine();
Console.WriteLine("Nhap ma phu huynh: ");
ParentNo = Console.ReadLine();
}
public void Display()
{
Console.WriteLine("Ten: {0}, dia chi: {1}, sdt: {2}, ma phu huynh: {3}, email: {4}, msv: {5}, tuoi: {6}",
Fullname, Address, Phone, ParentNo, Email, RollNo, Age);
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)