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

[Share Code] 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 - C2009G2

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 BT1536.Models;
using Newtonsoft.Json;

namespace BT1536
{
    class Program
    {
        static List<ClassRoom> classRooms;

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

            int choose;

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

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

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

                bformatter.Serialize(stream, classRooms);
            }*/
            foreach(ClassRoom room in classRooms)
            {
                using (Stream stream = File.Open(room.Name + ".obj", FileMode.Create))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    bformatter.Serialize(stream, room);
                }
            }
        }

        private static void ReadJsonFile()
        {
            //Lay dc content trong rooms.json
            string content = System.IO.File.ReadAllText(@"rooms.json");

            //Chuyen content -> List<ClassRoom>
            classRooms = JsonConvert.DeserializeObject<List<ClassRoom>>(content);

            Console.WriteLine("Doc noi dung json thanh cong");
        }

        private static void SaveJsonFile()
        {
            string content = JsonConvert.SerializeObject(classRooms);

            System.IO.File.WriteAllText(@"rooms.json", content);
        }

        private static void Display()
        {
            foreach(ClassRoom cRoom in classRooms)
            {
                cRoom.Display();
            }
        }

        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 classRoom = new ClassRoom();
                classRoom.Input();

                classRooms.Add(classRoom);
            }
        }

        static void ShowMenu()
        {
            Console.WriteLine("1. Nhap thong tin lop hoc");
            Console.WriteLine("2. Hien thi thong tin lop hoc");
            Console.WriteLine("3. Save data.json");
            Console.WriteLine("4. Read data.json");
            Console.WriteLine("5. Save moi lop hoc -> TEN_LOP.obj");
            Console.WriteLine("6. Thoat");
            Console.WriteLine("Chon: ");
        }
    }
}


#Models/Student.cs


using System;
using Newtonsoft.Json;

namespace BT1536.Models
{
    [Serializable]
    public class Student
    {
        [JsonProperty("fullname")]
        public string Fullname { get; set; }

        [JsonProperty("birthday")]
        public string Birthday { get; set; }

        [JsonProperty("email")]
        public string Email { get; set; }

        [JsonProperty("address")]
        public string Address { get; set; }

        [JsonProperty("gender")]
        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("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);
        }
    }
}


#Models/ClassRoom.cs


using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace BT1536.Models
{
    [Serializable]
    public class ClassRoom
    {
        [JsonProperty("name")]
        public string Name { get; set; }

        [JsonProperty("address")]
        public string Address { get; set; }

        [JsonProperty("studentList")]
        public List<Student> StudentList { get; set; }

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

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

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

            Console.WriteLine("Nhap so sinh vien can them: ");
            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("== Thong tin lop hoc > Name: {0}, dia chi: {1}", Name, Address);

            for(int i=0;i<StudentList.Count;i++)
            {
                StudentList[i].Display();
            }
        }
    }
}


#Rooms.json



[
  {
    "name": "T1801A",
    "address": "Detech Building",
    "studentList": [
      {
        "fullname": "Tran Van A",
        "birthday": "01-01-19991",
        "email": "tranvana@gmail.com",
        "address": "Nam Dinh",
        "gender": "Nam"
      },
      {
        "fullname": "Tran Van A",
        "birthday": "01-01-19991",
        "email": "tranvana@gmail.com",
        "address": "Nam Dinh",
        "gender": "Nam"
      },
      {
        "fullname": "Tran Van A",
        "birthday": "01-01-19991",
        "email": "tranvana@gmail.com",
        "address": "Nam Dinh",
        "gender": "Nam"
      }
    ]
  },
  {
    "name": "T1804M",
    "address": "Detech Building",
    "studentList": [
      {
        "fullname": "Tran Van A",
        "birthday": "01-01-19991",
        "email": "tranvana@gmail.com",
        "address": "Nam Dinh",
        "gender": "Nam"
      },
      {
        "fullname": "Tran Van A",
        "birthday": "01-01-19991",
        "email": "tranvana@gmail.com",
        "address": "Nam Dinh",
        "gender": "Nam"
      },
      {
        "fullname": "Tran Van A",
        "birthday": "01-01-19991",
        "email": "tranvana@gmail.com",
        "address": "Nam Dinh",
        "gender": "Nam"
      }
    ]
  },
  {
    "name": "C2009G2",
    "address": "285 Doi Can",
    "studentList": [
      {
        "fullname": "A",
        "birthday": "A",
        "email": "A",
        "address": "A",
        "gender": "A"
      },
      {
        "fullname": "B",
        "birthday": "B",
        "email": "B",
        "address": "B",
        "gender": "B"
      }
    ]
  }
]




Tags:



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 đó