By GokiSoft.com|
19:07 13/07/2020|
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
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
#Parent.cs
using System;
namespace Lession8
{
public class Parent
{
public string MaPH { get; set; }
public string Fullname { get; set; }
public Parent()
{
}
public void Input() {
Console.WriteLine("Nhap Ma PH: ");
MaPH = Console.ReadLine();
Console.WriteLine("Nhap Ten: ");
Fullname = Console.ReadLine();
}
public void Show() {
Console.WriteLine("Ma PH: {0}, Ten: {1}", MaPH, Fullname);
}
}
}
#Parent.css
body {
}
#Program.cs
using System;
using System.Collections.Generic;
namespace Lession8
{
public delegate void OnAddressPrint(List<Student> students);
class Program
{
static event OnAddressPrint EventAddressPrint;
static void Main(string[] args)
{
List<Parent> parents = new List<Parent>();
List<Student> students = new List<Student>();
int choose;
do
{
ShowMenu();
choose = int.Parse(Console.ReadLine());
switch (choose)
{
case 1:
Console.WriteLine("Nhap so phu huynh can them: ");
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++) {
Parent parent = new Parent();
parent.Input();
parents.Add(parent);
}
break;
case 2:
Console.WriteLine("Nhap so sinh vien can them: ");
int m = int.Parse(Console.ReadLine());
for (int i = 0; i < m; i++)
{
Student std = new Student();
std.Input(parents);
students.Add(std);
}
break;
case 3:
Console.WriteLine("Nhap Ma PH: ");
string maPH = Console.ReadLine();
Console.WriteLine("Danh sach sinh vien duoc tham chieu boi phu huynh: ");
foreach(Student std in students) {
if(std.MaPH.Equals(maPH)) {
std.Show();
}
}
break;
case 4:
Console.WriteLine("Nhap Ma SV: ");
string rollno = Console.ReadLine();
Console.WriteLine("Danh sach sinh vien duoc tham chieu boi phu huynh: ");
foreach (Student std in students)
{
if (std.RollNo.Equals(rollno))
{
foreach(Parent p in parents) {
if(p.MaPH.Equals(std.MaPH)) {
p.Show();
break;
}
}
break;
}
}
break;
case 5:
EventAddressPrint = new OnAddressPrint(ShowStudentByAddress);
EventAddressPrint(students);
break;
case 6:
Console.WriteLine("Exit!!!");
break;
default:
Console.WriteLine("Nhap lai!!!");
break;
}
} while (choose != 6);
Console.WriteLine("Hello World!");
}
static void ShowStudentByAddress(List<Student> students) {
Console.WriteLine("Nhap dia chi: ");
string address = Console.ReadLine();
Console.WriteLine("Thong tin sinh vien tim kiem theo dia chi: ");
foreach(Student std in students) {
if(std.Address.Contains(address)) {
std.Show();
}
}
}
static void ShowMenu() {
Console.WriteLine("1. Nhap N Phu Huynh");
Console.WriteLine("2. Nhap N Sinh Vien");
Console.WriteLine("3. Tim Kiem SV Theo PH");
Console.WriteLine("4. Tim Kiem Thong Tin PH theo SV");
Console.WriteLine("5. Event & Delegate");
Console.WriteLine("6. THoat");
Console.WriteLine("Choose: ");
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
namespace Lession8
{
public class Student
{
public string RollNo { get; set; }
public string Fullname { get; set; }
public string MaPH { get; set; }
public string Address { get; set; }
public Student()
{
}
public void Input(List<Parent> parents) {
Console.WriteLine("Nhap Ma SV: ");
RollNo = Console.ReadLine();
Console.WriteLine("Nhap Ten: ");
Fullname = Console.ReadLine();
Console.WriteLine("Nhap Dia Chi: ");
Address = Console.ReadLine();
foreach(Parent parent in parents) {
parent.Show();
}
Console.WriteLine("Nhap Ma PH: ");
for (; ;) {
MaPH = Console.ReadLine();
bool isFind = false;
foreach(Parent p in parents) {
if(p.MaPH.Equals(MaPH)) {
isFind = true;
break;
}
}
if(isFind) {
break;
}
Console.WriteLine("Nhap Lai: ");
}
}
public void Show() {
Console.WriteLine("MaSV: {0}, Ten: {1}, MaPH: {2}", RollNo, Fullname, MaPH);
}
}
}
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)