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

[Video] 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

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



#ClassRoom.cs


using System;
using System.Collections.Generic;

namespace Student
{
    [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("Nhap ten lop: ");
            Name = Console.ReadLine();
            Console.WriteLine("Nhap dia chi: ");
            Address = Console.ReadLine();
            Console.WriteLine("Nhap so sinh vien: ");
            int N = int.Parse(Console.ReadLine());

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

                StudentList.Add(std);
            }
        }

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


#Program.cs


using System;
using System.Collections.Generic;
using System.IO;

namespace Student
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ClassRoom> classRooms = new List<ClassRoom>();
            int choose;

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

                switch(choose) {
                    case 1:
                        classRooms = ImportJSON();
                        break;
                    case 2:
                        Display(classRooms);
                        break;
                    case 3:
                        SaveFiles(classRooms);
                        break;
                    case 4:
                        Console.WriteLine("Thoat chuong trinh");
                        break;
                    default:
                        Console.WriteLine("Nhap sai!!!");
                        break;
                }
            } while (choose != 4);

        }

        static void SaveFiles(List<ClassRoom> classRooms) {
            foreach(ClassRoom classroom in classRooms) {
                //luu tung object classroom vao file Name.obj
                using (Stream stream = File.Open(classroom.Name+".obj", FileMode.Create))
                {
                    var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    binaryFormatter.Serialize(stream, classroom);
                }
            }
            //read file => object
            /**using (Stream stream = File.Open(filePath, FileMode.Open))
            {
                var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                return (T)binaryFormatter.Deserialize(stream);
            }*/
        }

        static List<ClassRoom> ImportJSON() {
            //B1. Doc noi dung data.json
            var content = System.IO.File.ReadAllText(@"data.json");
            //Console.WriteLine(content);
            //B2. Convert JSON thanh Array Class Object trong C#
            List<ClassRoom> classRooms = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(content);

            Console.WriteLine("Length: " + classRooms.Count);

            Display(classRooms);

            return classRooms;
        }

        static void Display(List<ClassRoom> classRooms) {
            foreach(ClassRoom classroom in classRooms) {
                classroom.Display();
            }
        }

        static void ShowMenu() {
            Console.WriteLine("1. Nhap du lieu tu file data.json");
            Console.WriteLine("2. Hien thi thong tin lop hoc");
            Console.WriteLine("3. Luu");
            Console.WriteLine("4. Thoat");
            Console.WriteLine("Choose: ");
        }
    }
}


#Student.cs


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

        public Student()
        {
        }

        public void Input() {
            Console.WriteLine("Nhap ten: ");
            Fullname = Console.ReadLine();
            Console.WriteLine("Nhap ngay sinh(dd-mm-yyyy): ");
            Birthday = Console.ReadLine();
            Console.WriteLine("Nhap email: ");
            Email = Console.ReadLine();
            Console.WriteLine("Nhap gioi tinh: ");
            Gender = Console.ReadLine();
            Console.WriteLine("Nhap dia chi: ");
            Address = Console.ReadLine();
        }

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


#data.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"
      }
    ]
  }
]


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

5

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