By GokiSoft.com| 09:50 15/10/2021|
C Sharp

[Video] Bài tập C Sharp - Chương trình quản lý cán bộ C# - C Sharp - C2010G


Link Video Bài Giảng

Bài tập C Sharp - Chương trình quản lý cán bộ C# - C Sharp

#Program.cs


using System;
using System.Collections.Generic;
using System.IO;
using BT2479.Models;

namespace BT2479
{
    class Program
    {
        static List<CanBo> danhSachCanBo;

        static void Main(string[] args)
        {
            danhSachCanBo = new List<CanBo>();
            int choose;

            do
            {
                ShowMenu();
                choose = int.Parse(Console.ReadLine());

                switch(choose)
                {
                    case 1:
                        Input();
                        break;
                    case 2:
                        Display();
                        break;
                    case 3:
                        SearchByName();
                        break;
                    case 4:
                        SaveFile();
                        break;
                    case 5:
                        ReadFile();
                        break;
                    case 6:
                        Console.WriteLine("Thoat!!!");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 6);
        }

        private static void ReadFile()
        {
            using (Stream stream = File.Open(@"db.dat", FileMode.Open))
            {
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                danhSachCanBo = (List<CanBo>)bformatter.Deserialize(stream);
            }
        }

        private static void SaveFile()
        {
            using (Stream stream = File.Open(@"db.dat", FileMode.Create))
            {
                var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                bformatter.Serialize(stream, danhSachCanBo);
            }
            Console.WriteLine("Luu thanh cong!!!");
        }

        private static void SearchByName()
        {
            Console.WriteLine("Nhap ten can bo can tim: ");
            string nameSearch = Console.ReadLine();

            foreach (CanBo canBo in danhSachCanBo)
            {
                if(canBo.Fullname == nameSearch)
                {
                    canBo.Display();
                }
            }
        }

        private static void Display()
        {
            Console.WriteLine("Danh sach thong tin can bo");
            foreach(CanBo canBo in danhSachCanBo)
            {
                canBo.Display();
            }
        }

        private static void Input()
        {
            Console.WriteLine("Nhap so can bo can them: ");
            int N = int.Parse(Console.ReadLine());
            int choose;

            for(int i=0;i<N;i++)
            {
                Console.WriteLine("Chon doi tuong khoi tao: ");
                Console.WriteLine("1. Nhan vien");
                Console.WriteLine("2. Cong nhan");
                Console.WriteLine("3. Ky su");
                Console.WriteLine("Chon: ");
                choose = int.Parse(Console.ReadLine());

                CanBo canbo;

                switch(choose)
                {
                    case 1:
                        canbo = new NhanVien();
                        break;
                    case 2:
                        canbo = new CongNhan();
                        break;
                    default:
                        canbo = new KySu();
                        break;
                }

                canbo.Input();

                danhSachCanBo.Add(canbo);
            }
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap thong tin can bo");
            Console.WriteLine("2. Hien thi thong tin can bo");
            Console.WriteLine("3. Tim kiem theo ten");
            Console.WriteLine("4. Luu db.dat");
            Console.WriteLine("5. Doc db.dat");
            Console.WriteLine("6. Thoat");
        }
    }
}


#Models/NhanVien.cs


using System;
namespace BT2479.Models
{
    [Serializable]
    public class NhanVien : CanBo
    {
        public string Work { get; set; }

        public NhanVien()
        {
        }

        public NhanVien(string work, string fullname,
            string birthday, string gender, string address) :
            base(fullname, birthday, gender, address)
        {
            Work = work;
        }

        public override void Input()
        {
            base.Input();
            Console.WriteLine("Nhap cong viec: ");
            Work = Console.ReadLine();
        }

        public override string ToString()
        {
            return string.Format("{0}, cong viec: {1}", base.ToString(), Work);
        }
    }
}


#Models/KySu.cs


using System;
namespace BT2479.Models
{
    [Serializable]
    public class KySu : CanBo
    {
        public string SubjectTraining { get; set; }

        public KySu()
        {
        }

        public KySu(string subjectTraining, string fullname,
            string birthday, string gender, string address) :
            base(fullname, birthday, gender, address)
        {
            SubjectTraining = subjectTraining;
        }

        public override void Input()
        {
            base.Input();
            Console.WriteLine("Nhap nganh dao tao: ");
            SubjectTraining = Console.ReadLine();
        }

        public override string ToString()
        {
            return string.Format("{0}, nganh dao tao: {1}", base.ToString(), SubjectTraining);
        }
    }
}


#Models/CongNhan.cs


using System;
namespace BT2479.Models
{
    [Serializable]
    public class CongNhan : CanBo
    {
        public string Level { get; set; }

        public CongNhan()
        {
        }

        public CongNhan(string level, string fullname,
            string birthday, string gender, string address):
            base(fullname, birthday, gender, address)
        {
            Level = level;
        }

        public override void Input()
        {
            base.Input();
            Console.WriteLine("Nhap bac luong (3/7, 4/7, ...): ");
            Level = Console.ReadLine();
        }

        public override string ToString()
        {
            return string.Format("{0}, bac luong: {1}", base.ToString(), Level);
        }
    }
}


#Models/CanBo.cs


using System;
namespace BT2479.Models
{
    [Serializable]
    public class CanBo
    {
        public string Fullname { get; set; }
        public string Birthday { get; set; }
        public string Gender { get; set; }
        public string Address { get; set; }

        public CanBo()
        {
        }

        public CanBo(string fullname, string birthday, string gender, string address)
        {
            Fullname = fullname;
            Birthday = birthday;
            Gender = gender;
            Address = address;
        }

        public virtual void Input()
        {
            Console.WriteLine("Nhap ho & ten: ");
            Fullname = Console.ReadLine();

            Console.WriteLine("Nhap ngay sinh: ");
            Birthday = Console.ReadLine();

            Console.WriteLine("Nhap gioi tinh: ");
            Gender = Console.ReadLine();

            Console.WriteLine("Nhap dia chi: ");
            Address = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine(this);
        }

        public override string ToString()
        {
            return string.Format("Ten: {0}, ngay sinh: {1}, gioi tinh: {2}, dia chi: {3}",
                Fullname, Birthday, Gender, Address);
        }
    }
}




Phản hồi từ học viên

5

(Dựa trên đánh giá ngày hôm nay)

Đăng nhập để làm bài kiểm tra

Chưa có kết quả nào trước đó