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"
}
]
}
]
Tags:
Phản hồi từ học viên
5
(Dựa trên đánh giá ngày hôm nay)
![Đào Mạnh Dũng [C2010L]](https://www.gravatar.com/avatar/6a111fa53fd75dc87034660a8857df16.jpg?s=80&d=mm&r=g)
Đào Mạnh Dũng
2021-10-12 14:37:29
#ClassRoom.cs
using System.Collections.Generic;
namespace _1536
{
internal class ClassRoom
{
public string name { get; set; }
public string address { get; set; }
public List<Student> studentList { get; set; }
public ClassRoom()
{
}
public ClassRoom(string name, string address, List<Student> studentList)
{
this.name = name;
this.address = address;
this.studentList = studentList;
}
public void input()
{
System.Console.Write("nhap name : ");
this.name = name;
System.Console.Write("nhap address : ");
this.address = address;
System.Console.Write("nhap studentList : ");
this.studentList = studentList;
}
public void display()
{
System.Console.WriteLine(this);
foreach (var item in studentList)
{
item.display();
}
}
public override string ToString()
{
return "ClassRoom{" + "name=" + name + ", address=" + address + '}';
}
}
}
#Main.cs
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
namespace _1536
{
internal class main
{
private static List<ClassRoom> classList = new List<ClassRoom>();
private static void Main(string[] args)
{
while (true)
{
switch (menu())
{
case 1:
{
string json = "";
using (StreamReader sr = new StreamReader("./data.json"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
json += line;
}
}
classList = JsonConvert.DeserializeObject<List<ClassRoom>>(json);
break;
}
case 2:
{
foreach (var item in classList)
{
item.display();
}
break;
}
case 3:
{
foreach (var item in classList)
{
using (StreamWriter sw = new StreamWriter(item.name + ".json"))
{
sw.WriteLine(JsonConvert.SerializeObject(item));
}
}
Console.WriteLine("da tao file !");
break;
}
default:
{
Environment.Exit(0);
break;
}
}
}
}
private static int 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)\n" +
"\n" +
"2. Hiển thị thông tin sinh viên\n" +
"\n" +
"3. Lưu thông tin mỗi lớp học vào 1 file ten_lop.obj");
return Utility.ReadInt();
}
}
}
#Student.cs
namespace _1536
{
internal 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()
{
System.Console.Write("nhap fullname : ");
this.fullname = fullname;
System.Console.Write("nhap birthday : ");
this.birthday = birthday;
System.Console.Write("nhap email : ");
this.email = email;
System.Console.Write("nhap address : ");
this.address = address;
System.Console.Write("nhap gender : ");
this.gender = gender;
}
public void display()
{
System.Console.WriteLine(this);
}
public override string ToString()
{
return "Student{" + "fullname=" + fullname + ", birthday=" + birthday + ", email=" + email + ", address=" + address + ", gender=" + gender + '}';
}
}
}
#Utility.cs
using System;
namespace _1536
{
internal class Utility
{
public static int ReadInt()
{
int integer = 0;
while (true)
{
try
{
integer = int.Parse(Console.ReadLine());
break;
}
catch (Exception)
{
Console.Write("du lieu dau vao khong hop le, nhap lai : ");
}
}
return integer;
}
}
}
![Hoàng Thiện Thanh [community,AAHN-C2009G]](https://www.gravatar.com/avatar/58e377dde293f6da38c0b5168578557a.jpg?s=80&d=mm&r=g)
Hoàng Thiện Thanh
2021-10-05 17:47:18
#ClassRoom.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace FileExImportExport
{
[Serializable]
class ClassRoom
{
public string Name { get; set; }
public string Address { get; set; }
public List<Student> studentList;
public ClassRoom()
{
this.studentList = new List<Student>();
}
public ClassRoom(string Name, string Address)
{
this.Name = Name;
this.Address = Address;
this.studentList = new List<Student>();
}
public ClassRoom(string Name, string Address, List<Student> studentList)
{
this.Name = Name;
this.Address = Address;
this.studentList = studentList;
}
public void input()
{
Console.WriteLine("Input Class Name:");
Name = Console.ReadLine();
Console.WriteLine("Input Address:");
Address = Console.ReadLine();
}
public void display()
{
Console.WriteLine("Name: {0}\nAddress: {1}\nStudents List:", Name, Address);
foreach(Student s in studentList)
{
s.display();
}
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
using System.IO;
namespace FileExImportExport
{
class Program
{
public static List<ClassRoom> ClassList = new List<ClassRoom>();
public static void showMenu()
{
Console.WriteLine("1. Read Data and add Classes to list\n" +
"2. Show all classes\n" +
"3. Save classes to obj files.\n" +
"4. Exit"
);
}
public static List<ClassRoom> ImportJSON(List<ClassRoom> Class)
{
var data = System.IO.File.ReadAllText(@"data.json");
List<ClassRoom> Dump = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(data);
Class = Dump;
Console.WriteLine("Imported");
return Class;
}
public static void showClasses()
{
foreach(ClassRoom classRoom in ClassList)
{
classRoom.display();
}
}
public static void saveClassToFile()
{
foreach (ClassRoom classroom in ClassList)
{
using (Stream stream = File.Open(classroom.Name + ".json", FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, classroom);
}
}
Console.WriteLine("Saved");
}
static void Main(string[] args)
{
int choose;
do
{
showMenu();
choose = Utility.ReadInt();
switch (choose)
{
case 1:
ClassList = ImportJSON(ClassList);
break;
case 2:
showClasses();
break;
case 3:
saveClassToFile();
break;
default:
Console.WriteLine("Invalid Input");
break;
}
}
while (choose != 4);
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace FileExImportExport
{
[Serializable]
class Student
{
public string FullName { get; set; }
public DateTime Birthday { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Gender { get; set; }
public Student()
{
}
public Student(string FullName, string Birthday, string Address, string Email, string Gender)
{
this.FullName = FullName;
this.Birthday = Utility.ParseDate(Birthday);
this.Address = Address;
this.Email = Email;
this.Gender = Gender;
}
public Student(string FullName, DateTime Birthday, string Address, string Email, string Gender)
{
this.FullName = FullName;
this.Birthday = Birthday;
this.Address = Address;
this.Email = Email;
this.Gender = Gender;
}
public void input()
{
Console.WriteLine("Input Full Name:");
FullName = Console.ReadLine();
Console.WriteLine("Input Birthday:");
Birthday = Utility.ReadDate();
Console.WriteLine("Input Email:");
Email = Console.ReadLine();
Console.WriteLine("Input Address: ");
Address = Console.ReadLine();
Console.WriteLine("Input Gender:");
Gender = Utility.ReadGender();
}
public void display()
{
Console.WriteLine("Full Name: {0}\n" +
"Birthday: {1}\n" +
"Email: {2}\n" +
"Address: {3}\n" +
"Gender: {4}", FullName, Birthday, Address, Email, Gender);
}
}
}
#Utility.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace FileExImportExport
{
class Utility
{
public static DateTime ReadDate()
{
string str = Console.ReadLine();
DateTime date = new DateTime();
bool checkConvert()
{
try{
Convert.ToDateTime(str);
} catch (Exception)
{
return false;
}
return true;
}
bool checkParse()
{
try
{
DateTime.Parse(str);
}
catch (Exception)
{
return false;
}
return true;
}
while(!checkConvert() && !checkParse())
{
Console.WriteLine("Please input the right datetime in the following formats:");
Console.WriteLine("00/00/0000 or 00-00-0000");
str = Console.ReadLine();
}
if (checkConvert())
{
date = Convert.ToDateTime(str);
} else if (checkParse())
{
date = DateTime.Parse(str);
}
return date;
}
public static string ReadGender()
{
string str = Console.ReadLine();
while (!Regex.Match(str, @"\b(Male|Female|M|F|m|f|male|female|nam|nu|Nam|Nu)\b").Success)
{
Console.WriteLine("Gender input not valid");
str = Console.ReadLine();
}
return str;
}
public static DateTime ParseDate(string Date)
{
DateTime date = new DateTime();
bool checkConvert()
{
try
{
Convert.ToDateTime(Date);
}
catch (Exception)
{
return false;
}
return true;
}
bool checkParse()
{
try
{
DateTime.Parse(Date);
}
catch (Exception)
{
return false;
}
return true;
}
if (checkConvert())
{
date = Convert.ToDateTime(Date);
}
else if (checkParse())
{
date = DateTime.Parse(Date);
}
return date;
}
public static int ReadInt()
{
string str = Console.ReadLine();
bool check()
{
try { int.Parse(str); } catch (Exception) { return false; }
return true;
}
while (!check()) {
str = Console.ReadLine();
}
return int.Parse(str);
}
}
}
![Nguyễn Việt Hoàng [community,AAHN-C2009G]](https://www.gravatar.com/avatar/bdbde8074d82148dcda6927ccb016466.jpg?s=80&d=mm&r=g)
Nguyễn Việt Hoàng
2021-10-05 17:45:51
#Classroom.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss7.B2
{
[Serializable]
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 Classroom(string name , string address, List<Student> studentList)
{
this.Name = name;
this.Address = address;
this.StudentList = studentList;
}
public void input()
{
Console.WriteLine("Enter Name :");
Name = Console.ReadLine();
Console.WriteLine("Enter Address :");
Address = Console.ReadLine();
}
public void display()
{
Console.WriteLine("Name :{0}, Address :{1}", Name, Address);
foreach (Student std in StudentList)
{
std.display();
}
}
}
}
#Maint.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ss7.B2
{
class Maint
{
public static List<Classroom> ClassList = new List<Classroom>();
static void ShowMenu()
{
Console.WriteLine("1.Enter information students from JSON file");
Console.WriteLine("2.Display all information students ");
Console.WriteLine("3.Save information another class to file");
}
static List<Classroom> ReadFileJSON()
{
var content = System.IO.File.ReadAllText(@"data.json");
//Console.WriteLine(content);
ClassList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Classroom>>(content);
Console.WriteLine("Read File Success");
return ClassList;
}
static void DisplayInf(List<Classroom> ClassList)
{
foreach(Classroom c in ClassList)
{
c.display();
}
}
static void SaveInfClass()
{
foreach (Classroom classroom in ClassList)
{
bool append = false;
using (Stream stream = File.Open(classroom.Name + ".txt", append ? FileMode.Append : FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, classroom);
}
Console.WriteLine("Save File[" + classroom.Name + "]Success");
}
}
static void ListMenu()
{
int choose;
do
{
ShowMenu();
Console.WriteLine("Enter choose value to ShowMenu :");
choose = int.Parse(Console.ReadLine());
switch (choose)
{
case 1:
ClassList = ReadFileJSON();
break;
case 2:
DisplayInf(ClassList);
break;
case 3:
SaveInfClass();
break;
case 4:
Console.WriteLine("Exits");
return;
default:
Console.WriteLine("Value must be 1 -> 4");
break;
}
} while (choose != 4);
}
public static void Main(string[] args)
{
ListMenu();
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ss7.Ultitis;
namespace Ss7.B2
{
[Serializable]
class Student
{
public string FullName { get; set; }
public DateTime BirthDay { get; set; }
public string Email { get; set; }
public string Address { get; set; }
public string Gender { get; set; }
public Student()
{
}
public Student(string fullname , DateTime 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.WriteLine("Enter FullName :");
FullName = Console.ReadLine();
Console.WriteLine("Enter BirthDay :");
BirthDay = Ultility.ConvertStringToDateTime(Console.ReadLine());
Console.WriteLine("Enter Email :");
Email = Console.ReadLine();
Console.WriteLine("Enter Address :");
Address = Console.ReadLine();
Console.WriteLine("Enter Gender :");
Gender = Console.ReadLine();
}
public void display()
{
Console.WriteLine("FullName : {0}, BirthDay : {1}, Email : {2}, Address : {3}, Gender : {4}", FullName, BirthDay, Email, Address, Gender);
}
}
}
![Triệu Văn Lăng [T2008A]](https://www.gravatar.com/avatar/1348e3562c6492c26f796cb1f45982a1.jpg?s=80&d=mm&r=g)
Triệu Văn Lăng
2021-05-29 02:03:18
#ClassRoom.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace bai1536
{
[Serializable]
class ClassRoom
{
public string ClassName { get; set; }
public string ClassAddress { get; set; }
List<Student> studentList = new List<Student>();
public void input()
{
Console.WriteLine("Nhap ten lop: ");
ClassName = Console.ReadLine();
Console.WriteLine("Nhap dia chi lop: ");
ClassAddress = Console.ReadLine();
}
public void display()
{
Console.WriteLine("Ten lop: {0}, Dia chi lop: {1}", ClassName, ClassAddress);
foreach(Student std in studentList)
{
std.display();
}
}
}
}
#Program.cs
using System;
using bai1536;
using System.Collections.Generic;
using System.IO;
namespace bai1536
{
class Program
{
static void Main(string[] args)
{
List<ClassRoom> classList = new List<ClassRoom>();
int choose;
do
{
ShowMenu();
choose = Convert.ToInt32(Console.ReadLine());
switch(choose)
{
case 1:
string content = System.IO.File.ReadAllText(@"data.json");
classList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(content);
Console.WriteLine("Xong");
break;
case 2:
foreach(ClassRoom classRoom in classList)
{
classRoom.display();
}
break;
case 3:
foreach (ClassRoom classRoom in classList)
{
using (Stream stream = File.Open(classRoom.ClassName + ".obj", FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, classRoom);
}
}
Console.WriteLine("Luu xong");
break;
case 4:
Console.WriteLine("Thoat");
break;
default:
Console.WriteLine("Nhap lai");
break;
}
} while (choose != 4);
}
private static void ShowMenu()
{
Console.WriteLine("1. Nhap thog tin sinh vien tu file json");
Console.WriteLine("2. Hien thi thong tin sinh vien");
Console.WriteLine("3. Luu thong tin moi lop hoc vao 1 file ten_lop.obj");
Console.WriteLine("4. Thoat");
Console.WriteLine("Moi chon:");
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace bai1536
{
[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.WriteLine("Nhap Ten: ");
fullname = Console.ReadLine();
Console.WriteLine("Nhap birthday: ");
birthday = Console.ReadLine();
Console.WriteLine("Nhap email: ");
email = Console.ReadLine();
Console.WriteLine("Nhap address: ");
address = Console.ReadLine();
Console.WriteLine("Nhap gender: ");
gender = Console.ReadLine();
}
public void display()
{
Console.WriteLine("Ten: {0}, Birthday: {1}, Email: {2}, Addresss: {3}, Gender: {4}",
fullname, birthday, email, address, gender);
}
}
}
![vuong huu phu [T2008A]](https://www.gravatar.com/avatar/307a5cf29780afab49706dc8b15b86c6.jpg?s=80&d=mm&r=g)
vuong huu phu
2021-05-22 12:36:00
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
namespace Quanlysinhvien
{
class Program
{
static void Main(string[] args)
{
List<ClassRoom> classList = new List<ClassRoom>();
int lc;
do
{
menu();
Console.WriteLine("Nhap lua chon ");
lc = Int32.Parse(Console.ReadLine());
switch (lc) {
case 1:
String content = System.IO.File.ReadAllText(@"data.json");
classList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(content);
break;
case 2:
foreach (ClassRoom c in classList)
{
c.hienthi();
}
break;
case 3:
foreach (ClassRoom cl in classList) {
using (Stream stream = File.Open(cl.name + ".obj", FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, cl);
}
}
Console.WriteLine("Da luu xong ! ");
break;
}
} while (lc <= 3);
}
public static void them() {
}
public static void menu() {
Console.WriteLine("Nhap thong tin tu file json");
Console.WriteLine("Hien thi");
Console.WriteLine("Luu thong tin lop hoc");
Console.WriteLine("");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Quanlysinhvien
{
[Serializable]
class ClassRoom
{
public String name { set; get; }
public String address { set; get; }
public List<Student> studentList { set;get; }
public ClassRoom() {
studentList = new List<Student>();
}
public void nhap()
{
Console.WriteLine("Nhap ten lop");
name = Console.ReadLine();
Console.WriteLine("Nhap dia chi");
address = Console.ReadLine();
Console.WriteLine("Nhap so luong sv" );
int n = Int32.Parse(Console.ReadLine());
for (int i = 0; i < n; i++) {
Student s = new Student();
s.nhap();
studentList.Add(s);
}
}
public void hienthi()
{
Console.WriteLine("Ten lop : " + name);
Console.WriteLine("Ten dia chi : " + address);
foreach (Student s in studentList) {
s.hienthi();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Quanlysinhvien
{
[Serializable]
class Student
{
public String fullname { set; get; }
public String birthday { set; get; }
public String email { set; get; }
public String address { set; get; }
public String gender { set; get; }
public Student()
{
}
public Student(String fullname,String email,String address,String gender,String birthday)
{
this.fullname = fullname;
this.email = email;
this.address = address;
this.gender = gender;
this.birthday = birthday;
}
public virtual void nhap() {
Console.WriteLine("Nhap ten sinh vien");
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 virtual void hienthi()
{
Console.WriteLine( fullname + " , " + email + " , " + address + " , " + birthday + " , " + gender );
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-21 15:15:39
using System;
using System.Collections.Generic;
using Lesson4.JSON.StudentManager;
namespace Lesson4.JSON.StudentManager
{
class Test
{
static List<ClassRoom> ClassList = new List<ClassRoom>();
static void Main(string[] args)
{
int choose;
do
{
Menu();
choose = Int32.Parse(Console.ReadLine());
switch (choose)
{
case 1:
ReadData();
break;
case 2:
DisplayData();
break;
case 3:
SaveDataByClass();
break;
case 4:
Console.WriteLine("Exit....");
break;
}
} while (choose != 4);
}
static void Menu()
{
Console.WriteLine("1. Dọc dữ liệu từ file data.json và lưu thông tin đọc vào mảng classList");
Console.WriteLine("2. Hiển thị thông tin lớp học từ mảng classList");
Console.WriteLine("3. Thực hiện lưu thông tin từng lớp học vào 1 file tương ứng");
Console.WriteLine("Nhap lua chon choose = ");
}
private static void SaveDataByClass()
{
foreach (ClassRoom Element in ClassList)
{
string content = Newtonsoft.Json.JsonConvert.SerializeObject(Element.StudentList);
//Save file text
string path = Element.Name + ".json";
System.IO.File.WriteAllText(@path, content);
}
Console.WriteLine("Da luu file danh sach hoc sinh tung lop");
}
private static void DisplayData()
{
foreach (ClassRoom Element in ClassList)
{
Console.WriteLine("Ten lop: {0}, Dia chi lop: {1}", Element.Name, Element.Address);
Console.WriteLine("Danh sach Hoc sinh trong lop {0} nhuw sau", Element.Name);
foreach (Student student in Element.StudentList)
{
student.Display();
}
}
}
private static void ReadData()
{
string content = System.IO.File.ReadAllText(@"student.json");
List<ClassRoom> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(content);
ClassList = list;
Console.WriteLine("Da doc va lua thong tin");
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-21 15:15:23
using System;
using System.Collections.Generic;
using System.Text;
using Lesson4.JSON.StudentManager;
namespace Lesson4.JSON.StudentManager
{
class ClassRoom
{
public string Name { get; set; }
public string Address { get; set; }
public List<Student> StudentList { get; set; }
public ClassRoom() { }
public void Input()
{
Console.WriteLine("Nhap ten lop: ");
Name = Console.ReadLine();
Console.WriteLine("Nhap dia chi lop: ");
Address = Console.ReadLine();
}
}
}
![Do Trung Duc [T2008A]](https://www.gravatar.com/avatar/2973ac07124f066b4605c535e8d39a99.jpg?s=80&d=mm&r=g)
Do Trung Duc
2021-05-21 15:15:06
using System;
using System.Collections.Generic;
using System.Text;
using Lesson4.JSON.StudentManager;
namespace Lesson4.JSON.StudentManager
{
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.WriteLine("Nhap ten sinh vien");
FullName = Console.ReadLine();
Console.WriteLine("Nhap ngay thang nam sinh MM/DD/YYYY");
BirthDay = Console.ReadLine();
//DateTime Birthday = DateTime.ParseExact(str, "d", null);
Console.WriteLine("Nhap email sinh vien");
Email = Console.ReadLine();
Console.WriteLine("Nhap dia chi sinh vien");
Address = Console.ReadLine();
Console.WriteLine("Nhap gioi tinh sinh vien");
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);
}
}
}
![Nguyễn Anh Vũ [T2008A]](https://www.gravatar.com/avatar/8863d24ed74b396082becbc4db8331fd.jpg?s=80&d=mm&r=g)
Nguyễn Anh Vũ
2021-05-21 10:34:53
#Program
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Bai1536
{
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 : ");
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Bai1536
{
class Student
{
public string FullName { get; set; }
public string Email { get; set; }
public string Birthday { get; set; }
public string Address { get; set; }
public string Gender { get; set; }
public Student(string fullname, string email, string birthday, string address, string gender)
{
FullName = fullname;
Email = email;
Birthday = birthday;
Address = address;
Gender = gender;
}
public Student()
{
}
public void input()
{
Console.WriteLine("input fullname :");
FullName = Console.ReadLine();
Console.WriteLine("input email :");
Email = Console.ReadLine();
Console.WriteLine("input birthday :");
Birthday = Console.ReadLine();
Console.WriteLine("input address :");
Address = Console.ReadLine();
Console.WriteLine("input gender :");
Gender = Console.ReadLine();
}
public void display()
{
Console.WriteLine("----Full name : {0} , BirthDay : {1} , Email : {2} , Address : {3} , Gender : {4}", FullName, Birthday, Email, Address, Gender);
}
}
}
#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"
}
]
}
]
#ClassRoom
using System;
namespace Bai1536
{
[Serializable]
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 inputClassRoom()
{
Console.WriteLine("input nameclass");
Name = Console.ReadLine();
Console.WriteLine("input address");
Address = Console.ReadLine();
bool ischeck = true;
Console.WriteLine("input num student");
Int32 num = Convert.ToInt32(Console.ReadLine());
for(Int32 i = 1, i <= num , i++)
{
Console.WriteLine(i);
Student st = new Student();
st.input();
studentList.Add(st);
}
}
public void display()
{
Console.WriteLine("NameClass={0},Address={1}", Name, Address);
foreach (var item in studentList)
{
item.display();
}
}
}
}
![hainguyen [T2008A]](https://www.gravatar.com/avatar/32855ce6db55d60134d830aee06b41e5.jpg?s=80&d=mm&r=g)
hainguyen
2021-05-21 10:11:19
#ClassRoom.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson04
{
[Serializable]
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 name: ");
name = Console.ReadLine();
Console.WriteLine("nhap address: ");
address = Console.ReadLine();
Console.WriteLine("Nhap so sv: ");
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("name: = {0}, address: = {1}", name, address);
foreach(Student std in studentList)
{
std.display();
}
}
}
}
#Program.cs
using System;
using System.Collections.Generic;
using System.IO;
namespace Lesson04
{
class Program
{
static void Main(string[] args)
{
int choose;
List<ClassRoom> classList = new List<ClassRoom>();
do
{
showMenu();
choose = int.Parse(Console.ReadLine());
switch(choose)
{
case 1:
Import(classList);
break;
case 2:
Display(classList);
break;
case 3:
Save(classList);
break;
case 4:
Environment.Exit(0);
break;
default:
Console.WriteLine("Fail!");
break;
}
} while (choose != 4);
}
static void Import(List<ClassRoom> classList)
{
var content = System.IO.File.ReadAllText(@"data.json");
Console.WriteLine(content);
classList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(content);
Display(classList);
}
static void Display(List<ClassRoom> classList)
{
foreach(ClassRoom classroom in classList)
{
classroom.display();
}
}
static void Save(List<ClassRoom> classList)
{
foreach(ClassRoom classroom in classList)
{
using (Stream stream = File.Open(classroom.name+".obj", FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, classroom);
}
}
}
static void showMenu()
{
Console.WriteLine("1.Nhap du lieu");
Console.WriteLine("2.Hien thi thong tin");
Console.WriteLine("3.Luu");
Console.WriteLine("4.Thoat");
Console.WriteLine("Chon: ");
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace Lesson04
{
[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.WriteLine("nhap fullname: ");
fullname = Console.ReadLine();
Console.WriteLine("nhap birthday: ");
birthday = Console.ReadLine();
Console.WriteLine("nhap email: ");
email = Console.ReadLine();
Console.WriteLine("nhap address: ");
address = Console.ReadLine();
Console.WriteLine("nhap gender: ");
gender = Console.ReadLine();
}
public void display()
{
Console.WriteLine("fullname: = {0}, birthday: = {1}, email: = {2}, address: = {3}, gender: = {4}",
fullname, birthday, email, address, gender);
}
}
}