By GokiSoft.com| 10:17 19/10/2021|
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

I. Tạo lớp đối tượng sinh viên (Student) gồm các thuộc tính : fullname, birthday, email, address, gender

- Tạo hàm tạo ko đối và có đầy đủ đối số

- Tạo hàm getter & setter

- Tạo hàm nhập thông tin sinh và hiển thị thông tin sinh viên

II. Tạo lớp đối tượng lớp học (ClassRoom) gồm các thuôc tính : name, address và mảng danh sach sinh viên (List<Student> studentList)

- Tạo hàm tạo ko đối

- Tạo getter/setter

- Tạo hàm nhập và hiển thị thông tin sinh viên

III. Tạo lớp Main chứa mảng đối tượng ClassRoom

List<ClassRoom> classList = new ArrayList<>()

IV. Xây dựng menu chương trình sau

1. Nhập thông tin sinh viên từ file json (data.json -> xem nội dụng file đc import ở dưới)

2. Hiển thị thông tin sinh viên

3. Lưu thông tin mỗi lớp học vào 1 file ten_lop.obj

Chú thích :

Khi người dùng chọn 1 : thực hiện đọc dữ liệu từ file data.json và lưu thông tin đọc vào mảng classList

Khi người dùng chọn 2 : Hiển thị thông tin lớp học từ mảng classList

Khi người dùng chọn 3 : thực hiện lưu thông tin từng lớp học vào 1 file tương ứng (Ví dụ : T1801A.obj, ...)

JSON File

[
  {
    "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)

Đỗ Văn Huấn [T1907A]
Đỗ Văn Huấn

2020-06-02 01:25:27



using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; 

namespace BaiTapNgay01_06_2020
{
    class Program
    {

        static void Main(string[] args)
        {
            List<ClassRoom> classList = new List<ClassRoom>();
            int choose;
            do {
                Console.WriteLine("1. Nhập thông tin sinh viên từ file json");
                Console.WriteLine("2. Hiển thị thông tin sinh viên");
                Console.WriteLine("3. Lưu thông tin mỗi lớp học vào 1 file ten_lop.obj");
                Console.WriteLine("4. thoat");
                Console.Write("Enter choose: ");
                choose = int.Parse(Console.ReadLine());

                switch (choose) 
                {
                    case 1:
                       classList = ReadData();
                        break;
                    case 2:
                        foreach(ClassRoom cr in classList)
                        {
                            cr.display();
                        }
                        break;
                    case 3:
                        SaveFiles(classList);
                        break;
                    case 4:
                        Console.WriteLine("Exit");
                        break;
                    default:
                        Console.WriteLine("error");
                        break;
                }
            
            } while (choose != 4);
            Console.ReadLine();
        }
        static List<ClassRoom> ReadData( )
        {
            //B1. Doc noi dung data.json
            var content = System.IO.File.ReadAllText(@"D:\Admin\Documents\XML_JSON\JSON\data.json");

           
            //B2. Convert JSON thanh Array Class Object trong C#
            List<ClassRoom> classRooms = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(content);

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


            foreach(ClassRoom cr in classRooms)
            {
                cr.display();
            }
            return classRooms;

            //  Console.WriteLine(content);
        }

        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.nameClass + ".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);
            }*/
        }

    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTapNgay01_06_2020
{
   [Serializable]
     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)
        {
            this.fullname = fullname;
            this.birthday = birthday;
            this.email = email;
            this.address = address;
            this.gender = gender;
        }


        public void input()
        {
            Console.Write("Enter student name: ");
            fullname = Console.ReadLine();
            Console.Write("Enter student birthday: ");
            birthday = Console.ReadLine();

            Console.Write("Enter student email: ");
            email = Console.ReadLine();

            Console.Write("Enter student address: ");
            address = Console.ReadLine();

            Console.Write("Enter student gender: ");
            gender = Console.ReadLine();

        }
        public void display()
        {
            Console.WriteLine("name: {0}, birthday: {1}, email: {2}, address: {3}, gender: {4}", fullname, birthday, email, address, gender);
        }

      
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTapNgay01_06_2020
{
    [Serializable]
    class ClassRoom
    {
        public string nameClass { get; set; }
        public string addressClass { get; set; }

        List<Student> listStudent { get; set; }

        public ClassRoom() {
            listStudent = new List<Student>();
        }
        public ClassRoom(string nameClass, string addressClass, List<Student> listStudent)
        {
            this.nameClass = nameClass;
            this.addressClass = addressClass;
            this.listStudent = listStudent;
        }

        public void input()
        {
            Console.WriteLine("Enter name class: ");
            nameClass = Console.ReadLine();
            Console.WriteLine("Enter address class: ");
            addressClass = Console.ReadLine();

            for(; ; )
            {
                Student std = new Student();
                std.input();
                listStudent.Add(std);
                Console.Write("Do you want to continue ? (y/n)");
                String a = Console.ReadLine();
                if (a.Equals("n"))
                {
                    break;
                }
            }

        }

        public void display()
        {
            Console.WriteLine("Name class: {0}, address class: {1}", nameClass, addressClass);
            foreach(Student std in listStudent)
            {
                std.display();
            }
        }
    }
}



Trương Công Vinh [T1907A]
Trương Công Vinh

2020-06-01 13:46:54



using System;
using System.Collections.Generic;
using System.Text;

namespace Student
{
    class Student
    {
        public string fullName { get; set; }
        public DateTime DOB { get; set; }
        public string email { get; set; }
        public string address { get; set; }
        public string gender { get; set; }
        public Student()
        {

        }
        public Student(string fullName, DateTime DOB, string email, string address, string gender)
        {
            this.address = address;
            this.DOB = DOB;
            this.email = email;
            this.fullName = fullName;
            this.gender = gender;
        }

        public void input()
        {
            Console.WriteLine("Enter student name : ");
            fullName = Console.ReadLine();
            Console.WriteLine("Enter student birthday :");
            string birthday = Console.ReadLine();
            DOB = DateTime.ParseExact(birthday, "dd/MM/yyyy", null);
            Console.WriteLine("Enter student email :");
            email = Console.ReadLine();
            Console.WriteLine("Enter student address : ");
            address = Console.ReadLine();
            Console.WriteLine("Enter student gender :");
            gender = Console.ReadLine();
        }
        public void display()
        {
            Console.WriteLine("Fullname: {0} , birtday :  {1} , Email : {2} , Address : {3} , Gender : {4}", fullName, DOB, email, address, gender);
        }
        
    }
}



using System;
using System.Collections.Generic;
using System.Text;

namespace Student
{
    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("Enter classroom name : ");
            name = Console.ReadLine();
            Console.WriteLine("Enter address classroom :");
            address = Console.ReadLine();
            inputStudent();
            
        }
        public void display()
        {
            Console.WriteLine("Classname: {0} , Address :  {1} ", name, address);
            foreach (var item in studentList)
            {
                item.display();
            }
        }
        private void inputStudent()
        {
            while (true)
            {
                Student student = new Student();
                student.input();
                studentList.Add(student);
                Console.WriteLine("Add new Student (y/n):");
                string option = Console.ReadLine();
                if (option.Equals("n")|| option.Equals("N"))
                {
                    break;
                }
            }
        }
      
    }
}



using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace Student
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.OutputEncoding = Encoding.UTF8;
            List<ClassRoom> classRooms = new List<ClassRoom>();
            int c;
            do
            {
                menu();
                c = int.Parse(Console.ReadLine());
                switch (c)
                {
                    case 1:

                        classRooms = importJSON(classRooms);

                        display(classRooms);
                        break;
                    case 2:


                        display(classRooms);
                        break;
                    case 3:
                        exportJSON(classRooms);
                        break;
                    case 4:
                        Console.WriteLine("Thoat !!");
                        break;

                    default:
                        Console.WriteLine("nhap loi !!");
                        break;
                }

            } while (c != 4);

        }
        static List<ClassRoom> importJSON(List<ClassRoom> classRooms)
        {
            var content = File.ReadAllText(@"D:/C#/Less8/Student/data.json");
            classRooms = JsonConvert.DeserializeObject<List<ClassRoom>>(content);


            return classRooms;
        }
        static void display(List<ClassRoom> classRooms)
        {
            foreach (var item in classRooms)
            {
                item.display();
            }
        }
        static void exportJSON(List<ClassRoom> classRooms)
        {
            foreach (ClassRoom classroom in classRooms)
            {
                

                using (StreamWriter file = File.CreateText(@"d:/C#/Less8/Student/" + classroom.name+".obj"))
                {
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Serialize(file, classroom);
                }
            }
          
        static void menu()
        {
            Console.WriteLine("1. Nhập thông tin sinh viên từ file json (data.json -> xem nội dụng file đc import ở dưới)");
            Console.WriteLine("2. Hiển thị thông tin sinh viên");
            Console.WriteLine("3. Lưu thông tin mỗi lớp học vào 1 file ten_lop.obj");
            Console.WriteLine("4.Thoat");
            Console.WriteLine("Chon : ");
        }



    }
}



[
  {
    
    "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"
      }
    ]
  }
]