Lập trình C# - Quản lý sinh viên: delegate & event

Viết chương trình quản lý sinh viên + delegate & event

Thông tin sinh viên gồm các thuộc tính sau : tên, tuổi, mã sinh viên, địa chỉ, email, sđt, mã phụ huynh -> Viết hàm nhập và hàm hiển thị

Thông tin phụ huynh gồm các thuộc tính : tên, địa chỉ, sđt, mã phụ huynh -> Mỗi sinh viên sẽ được liên kết vs một phụ huynh -> Viết hàm nhập và hàm hiển thị

(Chú ý 1 phụ huynh có thể được liên kết vs nhiều sv)

Yêu cầu tạo menu chương trình

1. Nhập thông tin N phụ huynh

2. Nhập thông tin M sinh viên

3. Nhập tên phu huynh -> In ra thông tin các sinh viên được tham chiếu bởi phụ huynh này

4. Nhập thông tinh sv -> In ra thông tin tra cứu phụ huynh

5. Tạo delegate OnAddressPrint và sự kiện EventAddressPrint -> Thực hiện in thông tin sinh viên theo địa chỉ nhập vào.


Bước 1Trong project chúng ta tạo 1 class Parent.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace StudentDelegate
{
    class Parent
    {
        public string name { get; set; }
        public string address { get; set; }
        public string phoneNum { get; set; }
        public string parentKey { get; set; }
        public Parent()
        {

        }
        public virtual void input()
        {
            Console.WriteLine("Nhập tên phụ huynh:");
            name = Console.ReadLine();
            Console.WriteLine("Nhập địa chỉ phụ huynh:");
            address = Console.ReadLine();
            Console.WriteLine("Nhập số điện thoại:");
            phoneNum = Console.ReadLine();
            Console.WriteLine("Nhập mã phụ huynh:");
            parentKey = Console.ReadLine();
        }
        public virtual void display()
        {
            Console.WriteLine("Tên phụ huynh: {0},Địa chỉ: {1}, SDT: {2}, Mã phụ huynh: {3}", name, address, phoneNum, parentKey);
        }
    }
}

Bước 2Chúng ta tạo 1 class Student.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace StudentDelegate
{
    class Student : Parent
    {
        public int age { get; set; }
        public string rollno { get; set; }
        public string email { get; set; }
        public Student()
        {

        }
        public override void input()
        {
            Console.WriteLine("Nhập tên sinh viên:");
            name = Console.ReadLine();
            Console.WriteLine("Tuổi:");
            age = int.Parse(Console.ReadLine());
            Console.WriteLine("Mã sinh viên:");
            rollno = Console.ReadLine();
            Console.WriteLine("Nhập địa chỉ sinh viên:");
            address = Console.ReadLine();
            Console.WriteLine("Nhâp email sinh viên:");
            email = Console.ReadLine();
            Console.WriteLine("Nhập số điện thoại:");
            phoneNum = Console.ReadLine();
        }
        public override void display()
        {
            Console.WriteLine("Tên sinh viên: {0}, Tuổi: {1}, MSV: {2}, Địa chỉ: {3}, Email: {4}, SDT: {5}, Mã phụ huynh: {6}", name, age, rollno, address, email, phoneNum, parentKey);
        }
    }
}

Bước 3Mã nguồn chương trình trong Program.cs

using System;
using System.Collections.Generic;

namespace StudentDelegate
{
    public delegate void OnAddressPrint(string address);
    class Program
    {
        static event OnAddressPrint EventAddressPrint = null;
        static List<Parent> parentList = new List<Parent>();
        static List<Student> studentList = new List<Student>();
        static void Main(string[] args)
        {
            Console.OutputEncoding = System.Text.Encoding.UTF8;
            int choose = 0;
            do
            {
                showMenu();
                choose = int.Parse(Console.ReadLine());
                switch (choose)
                {
                    case 1:
                        Console.WriteLine("Nhập số lượng phụ huynh muốn thêm:");
                        int N = int.Parse(Console.ReadLine());
                        for (int i = 0; i < N; i++)
                        {
                            Console.WriteLine("Nhập thôngg tin phụ huynh thứ: ", parentList.Count + i + 1);
                            Parent parent = new Parent();
                            parent.input();
                            parentList.Add(parent);
                        }
                        break;
                    case 2:
                        Console.WriteLine("Nhập số lượng sinh viên muốn thêm:");
                        int M = int.Parse(Console.ReadLine());
                        for (int i = 0; i < M; i++)
                        {
                            Console.WriteLine("Nhập thônng tin sinh viên thứ: ", studentList.Count + i + 1);
                            Student student = new Student();
                            Console.WriteLine("Nhập mã phụ huynh:");
                            while (true)
                            {
                                student.parentKey = Console.ReadLine();
                                int check = 0;
                                foreach (Parent parent in parentList)
                                {
                                    if (parent.parentKey == student.parentKey)
                                    {
                                        check++;
                                        break;
                                    }
                                }
                                if (check == 0) Console.WriteLine("Không có phụ huynh hợp lệ, yêu cầu nhập lại!!");
                                else break;
                            }
                            student.input();
                            studentList.Add(student);
                        }
                        break;
                    case 3:
                        Console.WriteLine("Nhập tên phụ huynh:");
                        string name = Console.ReadLine();
                        foreach (Parent parent in parentList)
                        {
                            if (parent.name == name)
                            {
                                Console.WriteLine("Phụ huynh có tên {0} và mã Phụ huynh la {1}, có các sinh viên là:", parent.name, parent.parentKey);
                                int check = 0;
                                foreach (Student student in studentList)
                                {
                                    if (student.parentKey == parent.parentKey)
                                    {
                                        student.display();
                                        check++;
                                    }
                                }
                                if (check == 0) Console.WriteLine("Không có sinh viên nào");
                            }
                        }
                        break;
                    case 4:
                        Console.WriteLine("Nhập mã sinh viên:");
                        string rollno = Console.ReadLine();
                        foreach (Student std in studentList)
                        {
                            if (std.rollno == rollno)
                            {
                                Console.WriteLine("Sinh viên có tên {0} và mã sinh viên la {1}, có phụ huynh là:", std.name, std.rollno);
                                int check = 0;
                                foreach (Parent p in parentList)
                                {
                                    if (std.parentKey == p.parentKey)
                                    {
                                        p.display();
                                        check++;
                                        break;
                                    }
                                }
                                if (check == 0) Console.WriteLine("Khônng có phụ huynh nào");
                            }
                        }
                        break;
                    case 5:
                        Console.WriteLine("Địa chỉ sinh viên cần tìm:");
                        string address = Console.ReadLine();
                        EventAddressPrint = showInfo;
                        EventAddressPrint(address);
                        break;
                    case 6:
                        Console.WriteLine("Tam biệt!!");
                        break;
                    default:
                        Console.WriteLine("Nhập sai!!");
                        break;
                }
            } while (choose != 6);
        }

        private static void showMenu()
        {
            Console.WriteLine("\nCHƯƠNG TRÌNH QUẢN LÝ SINH VIÊN C#");
            Console.WriteLine("*********************************MENU************************************");
            Console.WriteLine("**               1.Nhập số lượng phụ huynh                             **");
            Console.WriteLine("**               2.Nhập số lượng sinh viên                             **");
            Console.WriteLine("**               3.Nhập tên phụ huynh -> tìm sinh viên                 **");
            Console.WriteLine("**               4.Nhập mã sinh viên -> tim phụ huynh                  **");
            Console.WriteLine("**               5.In thông tin sinh viên tìm theo delegate            **");
            Console.WriteLine("**               6.Thóat                                               **");
            Console.WriteLine("*************************************************************************");
        }
        private static void showInfo(string address)
        {
            int check = 0;
            foreach (Student student in studentList)
            {
                if (student.address == address)
                {
                    check++;
                    student.display();
                }
            }
            if (check == 0) Console.WriteLine("Không có sinh viên nào hợp lệ!!");
        }
    }
}

Xem ví dụ

Bước 3: Nhấn Ctrl+F5 để chạy và xem kết quả

CHƯƠNG TRÌNH QUẢN LÝ SINH VIÊN C#
*********************************MENU************************************
**               1.Nhập số lượng phụ huynh                             **
**               2.Nhập số lượng sinh viên                             **
**               3.Nhập tên phụ huynh -> tìm sinh viên                 **
**               4.Nhập mã sinh viên -> tim phụ huynh                  **
**               5.In thông tin sinh viên tìm theo delegate            **
**               6.Thóat                                               **
*************************************************************************

Chọn 1 "1.Nhập số lượng phụ huynh "

1
Nhập số lượng phụ huynh muốn thêm:
2
Nhập thôngg tin phụ huynh thứ:
Nhập tên phụ huynh:
Thich Dam Dao
Nhập địa chỉ phụ huynh:
77 Tran Van Long
Nhập số điện thoại:
989977
Nhập mã phụ huynh:
001
Nhập thôngg tin phụ huynh thứ:
Nhập tên phụ huynh:
Thích Hoc Gioi
Nhập địa chỉ phụ huynh:
78 Nguyen Van Nen
Nhập số điện thoại:
9777772
Nhập mã phụ huynh:
002

Chọn 2 "2.Nhập số lượng sinh viên"

Nhập số lượng sinh viên muốn thêm:
2
Nhập thônng tin sinh viên thứ:
Nhập mã phụ huynh:
001
Nhập tên sinh viên:
Thích Qua Mon
Tuổi:
19
Mã sinh viên:
003
Nhập địa chỉ sinh viên:
77 Tran Van Long
Nhâp email sinh viên:
test@gmail.com
Nhập số điện thoại:
877789
Nhập thônng tin sinh viên thứ:
Nhập mã phụ huynh:
002
Nhập tên sinh viên:
Thich Thi Lai
Tuổi:
23
Mã sinh viên:
004
Nhập địa chỉ sinh viên:
78 Nguyen Van Long
Nhâp email sinh viên:
long@gmail.com
Nhập số điện thoại:
88888

Chọn 4  " 4.Nhập mã sinh viên -> tim phụ huynh"

4
Nhập mã sinh viên:
004
Sinh viên có tên Thich Thi Lai và mã sinh viên la 004, có phụ huynh là:
Tên phụ huynh: Thích Hoc Gioi,Địa chỉ: 78 Nguyen Van Nen, SDT: 9777772, Mã phụ huynh: 002