By GokiSoft.com| 15:05 28/10/2021|
C Sharp

[Video] Thêm chức năng mới : Chương trình quản lý sinh viên C# + Import/Export JSON + File - Lập Trình C# - Lập Trình C Sharp - C2009G


Link Video Bài Giảng

Chương trình quản lý sinh viên C# + Import/Export JSON + File - Lập Trình C# - Lập Trình C Sharp


#Program.cs


using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace BT1536
{
    delegate void SwitchCase();

    class Program
    {
        static List<ClassRoom> classList;

        static void Main(string[] args)
        {
            classList = new List<ClassRoom>();

            SwitchCase[] options = {Input, Display, SaveFileText, ReadFileText,
                SaveFileObject, ReadFileObject, SaveFileJson,
                ReadFileJson, ExitProgram};

            int choose;

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

                if(choose > 0 && choose <= options.Length)
                {
                    options[choose - 1]();
                } else
                {
                    Console.WriteLine("Nhap lai!!!");
                }
            } while (choose != 9);
        }

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

            for(int i=0;i<N;i++)
            {
                ClassRoom @class = new ClassRoom();
                @class.Input();

                classList.Add(@class);
            }
        }

        private static void Display()
        {
            Console.WriteLine("Thong tin lop hoc");
            foreach(ClassRoom @class in classList)
            {
                @class.Display();
            }
        }

        private static void SaveFileText()
        {
            //Dinh danh du lieu se luu trong students.txt
            string content = "";
            foreach(ClassRoom @class in classList)
            {
                content += @class.GetFileLine();
            }

            File.WriteAllText(@"students.txt", content);
        }

        private static void ReadFileText()
        {
            string content = File.ReadAllText(@"students.txt");
            string[] lines = content.Split("\n");

            ClassRoom currentClassRoom = null;

            foreach(string line in lines)
            {
                if (line == "") continue;
                string[] elements = line.Split(":");
                switch(elements[0])
                {
                    case "class":
                        currentClassRoom = new ClassRoom();
                        currentClassRoom.Parse(elements[1]);

                        classList.Add(currentClassRoom);
                        break;
                    case "student":
                        Student student = new Student();
                        student.Parse(elements[1]);

                        if(currentClassRoom != null)
                        {
                            currentClassRoom.StudentList.Add(student);
                        }
                        break;
                }
            }

            Display();
        }

        private static void SaveFileObject()
        {
            //serialize
            using (Stream stream = File.Open(@"students.dat", FileMode.Create))
            {
                var bformatter = new BinaryFormatter();

                bformatter.Serialize(stream, classList);
            }
        }

        private static void ReadFileObject()
        {
            //deserialize
            using (Stream stream = File.Open(@"students.dat", FileMode.Open))
            {
                var bformatter = new BinaryFormatter();

                List<ClassRoom> list = (List<ClassRoom>)bformatter.Deserialize(stream);

                foreach(ClassRoom @class in list)
                {
                    classList.Add(@class);
                }
            }

            Display();
        }

        private static void SaveFileJson()
        {
            //Chuyen object | list object -> json string
            string json = Newtonsoft.Json.JsonConvert.SerializeObject(classList);

            //Luu vao file
            File.WriteAllText(@"students.json", json);
        }

        private static void ReadFileJson()
        {
            //Doc noi dung tu file
            string json = File.ReadAllText(@"students.json");

            classList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(json);

            Display();
        }

        private static void ExitProgram()
        {
            Console.WriteLine("Thoat chuong trinh!!!");
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap N lop hoc");
            Console.WriteLine("2. Hien thi thong tin lop hoc");
            Console.WriteLine("3. Luu students.txt");
            Console.WriteLine("4. Doc students.txt");
            Console.WriteLine("5. Luu students.dat");
            Console.WriteLine("6. Doc students.dat");
            Console.WriteLine("7. Luu students.json");
            Console.WriteLine("8. Doc students.json");
            Console.WriteLine("9. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Student.cs


using System;
namespace BT1536
{
    [Serializable]
    public class Student
    {
        public string Fullname { get; set; }
        public string Birthday { get; set; }
        public string Email { get; set; }
        public string Address { get; set; }
        public string Gender { get; set; }

        public Student()
        {
        }

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

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

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

            Console.WriteLine("Nhap email: ");
            Email = Console.ReadLine();

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

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

        public void Display()
        {
            Console.WriteLine("Ten: {0}, ngay sinh: {1}, email: {2}, dia chi: {3}, " +
                "gioi tinh: {4}", Fullname, Birthday, Email, Address, Gender);
        }

        public string GetFileLine()
        {
            return string.Format("student:{0},{1},{2},{3},{4}\n",
                Fullname, Birthday, Email, Address, Gender);
        }

        public void Parse(string line)
        {
            string[] elements = line.Split(",");
            Fullname = elements[0];
            Birthday = elements[1];
            Email = elements[2];
            Address = elements[3];
            Gender = elements[4];
        }
    }
}


#ClassRoom.cs


using System;
using System.Collections.Generic;

namespace BT1536
{
    [Serializable]
    public class ClassRoom
    {
        public string Name { get; set; }
        public string Address { get; set; }
        public List<Student> StudentList { get; set; }

        public ClassRoom()
        {
            StudentList = new List<Student>();
        }

        public void Input()
        {
            Console.WriteLine("================================");
            Console.WriteLine("Nhap ten: ");
            Name = Console.ReadLine();

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

            Console.WriteLine("Nhap so sinh vien trong lop: ");
            int N = int.Parse(Console.ReadLine());

            for(int i=0;i<N;i++)
            {
                Student student = new Student();
                student.Input();

                StudentList.Add(student);
            }
        }

        public void Display()
        {
            Console.WriteLine("Ten lop: {0}, dia chi: {1}", Name, Address);
            foreach(Student student in StudentList)
            {
                student.Display();
            }
        }

        public string GetFileLine()
        {
            string content = string.Format("class:{0},{1}\n", Name, Address);
            foreach(Student student in StudentList)
            {
                content += student.GetFileLine();
            }
            return content;
        }

        public void Parse(string line)
        {
            string[] elements = line.Split(",");
            Name = elements[0];
            Address = elements[1];
        }
    }
}


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

5

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