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)
![Trần Văn Lâm [T2008A]](https://www.gravatar.com/avatar/cfc15c8cb7781ad669b013e01f9f1a6b.jpg?s=80&d=mm&r=g)
Trần Văn Lâm
2021-05-21 10:10:55
#Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace les4t1
{
class Program
{
static void Main(string[] args)
{
List<ClassRoom> classRooms = new List<ClassRoom>();
int choose;
do
{
showMenu();
choose = Convert.ToInt32(Console.ReadLine());
switch (choose)
{
case 1:
ImportJSON(classRooms);
break;
case 2:
Display(classRooms);
break;
case 3:
SaveFiles(classRooms);
break;
case 4:
Console.WriteLine("Exit!!!");
break;
default:
Console.WriteLine("Input Fail!!!");
break;
}
} while (choose != 4);
}
static void ImportJSON(List<ClassRoom> classRooms)
{
var content = System.IO.File.ReadAllText(@"data.json");
Console.WriteLine(content);
classRooms = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(content);
Console.WriteLine("Length: " + classRooms.Count);
Display(classRooms);
}
static void Display(List<ClassRoom> classRooms)
{
foreach (ClassRoom classroom in classRooms)
{
classroom.Display();
}
}
static void SaveFiles(List<ClassRoom> classRooms)
{
foreach(ClassRoom classroom in classRooms)
{
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.Input data from file data.json");
Console.WriteLine("2.Display Class Room");
Console.WriteLine("3.Save");
Console.WriteLine("4.Exit");
Console.WriteLine("Choose");
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace les4t1
{
[Serializable]
class Student
{
public string Fullname { set; get; }
public string Birthday { get; set; }
public string Email { set; get; }
public string Address { set; get; }
public string Gender { set; get; }
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("Input fullname:");
Fullname = Console.ReadLine();
Console.WriteLine("Input birthday:");
Birthday = Console.ReadLine();
Console.WriteLine("Input email:");
Email = Console.ReadLine();
Console.WriteLine("Input address:");
Address = Console.ReadLine();
Console.WriteLine("Input gender:");
Gender = Console.ReadLine();
}
public void Display()
{
Console.WriteLine("fullname : {0}, birthday : {1}, email : {2}, address : {3}, gender : {4}", Fullname, Birthday, Email, Address, Gender);
}
}
}
#ClassRoom.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace les4t1
{
[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("Input Name:");
Name = Console.ReadLine();
Console.WriteLine("Input Address:");
Address = Console.ReadLine();
Console.WriteLine("Input N Student:");
int N = Convert.ToInt32(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();
}
}
}
}
#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"
}
]
}
]
}
![Đỗ Minh Quân [T2008A]](https://www.gravatar.com/avatar/fa40264d7c4b4209c87a9e9451d2b9f0.jpg?s=80&d=mm&r=g)
Đỗ Minh Quân
2021-05-21 09:43:45
[
{
"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"
}
]
}
]
![Đỗ Minh Quân [T2008A]](https://www.gravatar.com/avatar/fa40264d7c4b4209c87a9e9451d2b9f0.jpg?s=80&d=mm&r=g)
Đỗ Minh Quân
2021-05-21 09:43:21
using System;
using System.Collections.Generic;
using System.Text;
namespace Ql.sinhvien
{
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("Enter fullname : ");
FullName = Console.ReadLine();
Console.WriteLine("Enter birthday(MM/dd/yyyy) : ");
BirthDay = 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("----Full name : {0} , BirthDay : {1} , Email : {2} , Address : {3} , Gender : {4}", FullName, BirthDay, Email, Address, Gender);
}
}
}
![Nguyên Phấn Đông [T2008A]](https://www.gravatar.com/avatar/c9c4f8f79ce35b9224637b6cc5fbe5c4.jpg?s=80&d=mm&r=g)
Nguyên Phấn Đông
2021-05-21 09:21:20
#ClassRoom.cs
using System;
namespace buoi4
{
[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();
}
}
}
}
#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"
}
]
}
]
#Main .cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Newtonsoft.Json;
namespace buoi4
public class Main
{
static void Main(string[] args)
{
List<ClassRoom> classList = new ArrayList<>()
do
{
showmenu();
int choose = Convert.ToInt32(Console.ReadLine());
switch (choose)
{
case 1:
class = importJSON(classList);
listclass(class);
break;
case 2:
listclass(classList);
break;
case 3:
exportJSON(classList);
break;
case 4:
Console.WriteLine("Thoat !!");
break;
}
} while (choose != 4);
public static void showmenu()
{
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. Thoát");
Console.WriteLine("Choose");
}
public static void listclass(List<ClassRoom> classList)
{
Console.WriteLine("Danh sach sinh vien theo classroom");
foreach (var item in classList)
{
item.display();
}
}
static void exportJSON(List<ClassRoom> classRooms)
{
foreach (ClassRoom classroom in classRooms)
{
using (StreamWriter file = File.CreateText(@"C:/Users/dong/source/repos/CMakeProject1/buoi4/" + classroom.name + ".obj"))
{
JsonSerializer serializer = new JsonSerializer();
serializer.Serialize(file, classroom);
}
}
static List<ClassRoom> importJSON(List<ClassRoom> classRooms)
{
var content = File.ReadAllText(@"C:/Users/dong/source/repos/CMakeProject1/buoi4/data.json");
classRooms = JsonConvert.DeserializeObject<List<ClassRoom>>(content);
return classRooms;
}
}
}
#stuednt.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace buoi4
{
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 display()
{
Console.WriteLine("FullName ={0},Birthday={1},Email={2},Address={3},Gender={4}", Fullname, Birthday, Email, Address, Gender);
}
public void input()
{
Console.WriteLine("input fullname: ");
Fullname = Console.ReadLine();
Console.WriteLine("input birthday:");
Birthday = Console.ReadLine();
Console.WriteLine("input email");
Email = Console.ReadLine();
Console.WriteLine("input address:");
Address = Console.ReadLine();
Console.WriteLine("input gender:");
Gender = Console.ReadLine();
}
}
}
![Nguyễn Tiến Đạt [T2008A]](https://www.gravatar.com/avatar/b5819cd0adc95c727c7ad0c2bcf6098b.jpg?s=80&d=mm&r=g)
Nguyễn Tiến Đạt
2021-05-21 09:00:30
#Program.cs
using System;
using System.Collections.Generic;
using QuanLySinhVien.Modals;
namespace QuanLySinhVien
{
class Program
{
static void Main(string[] args)
{
List<ClassRoom> classList = new List<ClassRoom>();
int choose=0;
do
{
showMenu();
choose = int.Parse(Console.ReadLine());
switch (choose)
{
case 1:
string content = System.IO.File.ReadAllText(@"data.json");
classList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(content);
Console.WriteLine("Lay du lieu thanh cong!!");
break;
case 2:
foreach (ClassRoom classroom in classList)
{
Console.WriteLine("Ten lop: {0}", classroom.name);
Console.WriteLine("Dia chi: {0}", classroom.address);
classroom.display();
}
break;
case 3:
foreach (ClassRoom classroom in classList)
{
string text = "Dia chi lop: " + classroom.address + "\n";
foreach(Student std in classroom.studentList)
{
text += std.chuoi();
}
System.IO.File.WriteAllText(classroom.name + ".json", text);
}
Console.WriteLine("Luu thanh cong!!");
break;
case 4:
Console.WriteLine("Tam biet!!");
break;
default:
Console.WriteLine("Loi nhap!!");
break;
}
} while (choose != 4);
}
static void showMenu()
{
Console.WriteLine("Lua chon chuong trinh:");
Console.WriteLine("1.Nhap thong tin sinh vien tu file json");
Console.WriteLine("2.Hien thi thong tin sinh vien");
Console.WriteLine("3.Luu tung lop hoc");
Console.WriteLine("4.Thoat");
}
}
}
#ClassRoom.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLySinhVien.Modals
{
class ClassRoom
{
public string name { get; set; }
public string address { get; set; }
public List<Student> studentList = new List<Student>();
public ClassRoom()
{
}
public void input()
{
Console.WriteLine("Nhap ten lop:");
name = Console.ReadLine();
Console.WriteLine("Nhap dia chi lop:");
address = Console.ReadLine();
}
public void display()
{
foreach(Student std in studentList)
{
std.display();
}
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace QuanLySinhVien.Modals
{
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 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("Ho va ten: {0}, Ngay sinh: {1}, Email: {2}, Dia chi: {3}, Gioi tinh: {4}", fullname, birthday, email, address, gender);
}
public string chuoi()
{
return "Ho va ten: " + fullname + ", Ngay sinh: " + birthday + ", Email: " + email + ", Dia chi: " + address + ", Gioi tinh: " + gender + "\n";
}
}
}
#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"
}
]
}
]
#T1801A.json
Dia chi lop: Detech Building
Ho va ten: Tran Van A, Ngay sinh: 01-01-19991, Email: tranvana@gmail.com, Dia chi: Nam Dinh, Gioi tinh: Nam
Ho va ten: Tran Van A, Ngay sinh: 01-01-19991, Email: tranvana@gmail.com, Dia chi: Nam Dinh, Gioi tinh: Nam
Ho va ten: Tran Van A, Ngay sinh: 01-01-19991, Email: tranvana@gmail.com, Dia chi: Nam Dinh, Gioi tinh: Nam
#T1804M.json
Dia chi lop: Detech Building
Ho va ten: Tran Van A, Ngay sinh: 01-01-19991, Email: tranvana@gmail.com, Dia chi: Nam Dinh, Gioi tinh: Nam
Ho va ten: Tran Van A, Ngay sinh: 01-01-19991, Email: tranvana@gmail.com, Dia chi: Nam Dinh, Gioi tinh: Nam
Ho va ten: Tran Van A, Ngay sinh: 01-01-19991, Email: tranvana@gmail.com, Dia chi: Nam Dinh, Gioi tinh: Nam
![Nguyễn Hoàng Anh [C1907L]](https://www.gravatar.com/avatar/5b7bb435cae0d0a0fd414f6fdd0adc87.jpg?s=80&d=mm&r=g)
Nguyễn Hoàng Anh
2020-07-06 15:33:15
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace StudentManager
{
class Program
{
static void menu() {
Console.WriteLine("0.Create student json file.");
Console.WriteLine("1.Import student from json file.");
Console.WriteLine("2.Show student.");
Console.WriteLine("3.Save student file to ten_lop.obj.");
Console.WriteLine("4.Exit.");
}
static void Main(string[] args)
{
string dir = "my_json.text";
List<ClassRoom> classRooms = new List<ClassRoom>();
List<ClassRoom> objs = new List<ClassRoom>();
while (true) {
menu();
int choice = int.Parse(Console.ReadLine());
switch (choice) {
case 0:
ClassRoom classRoom = new ClassRoom();
classRoom.input();
classRooms.Add(classRoom);
try
{
string json = JsonConvert.SerializeObject(classRooms);
Console.WriteLine(json);
FileStream file = File.Create(dir);
file.Close();
System.IO.File.WriteAllText(dir, json);
}
catch (SerializationException e)
{
Console.WriteLine(e);
}
break;
case 1:
string strings = System.IO.File.ReadAllText(dir);
objs = JsonConvert.DeserializeObject<List<ClassRoom>>(strings);
break;
case 2:
for (int i = 0; i < objs.Count; i++)
{
objs[i].display();
}
break;
case 3:
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("ten_lop.obj", FileMode.Create, FileAccess.Write);
formatter.Serialize(stream, objs);
stream.Close();
break;
case 5:
IFormatter formatter_ = new BinaryFormatter();
Stream stream_ = new FileStream("ten_lop.obj", FileMode.Open, FileAccess.Read);
List<ClassRoom> classRoomms = (List<ClassRoom>)formatter_.Deserialize(stream_);
for (int i = 0; i < classRoomms.Count; i++) {
classRoomms[i].display();
}
stream_.Close();
break;
case 4:
return;
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace StudentManager
{
[Serializable]
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("Enter fullname : ");
FullName = Console.ReadLine();
Console.WriteLine("Enter birthday(MM/dd/yyyy) : ");
BirthDay = 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("----Full name : {0} , BirthDay : {1} , Email : {2} , Address : {3} , Gender : {4}",FullName,BirthDay,Email,Address,Gender);
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace StudentManager
{
[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("Enter name : ");
Name = Console.ReadLine();
Console.WriteLine("Enter address : ");
Address = Console.ReadLine();
Console.WriteLine("Enter number student : ");
int n = int.Parse(Console.ReadLine());
for(int i = 0; i < n; i++)
{
Console.WriteLine("Enter student {0} infomation.",(i + 1));
Student std = new Student();
std.input();
StudentList.Add(std);
}
}
public void display() {
Console.WriteLine("Class Name : {0} , Address : {1} ", Name, Address);
for (int i = 0; i < StudentList.Count; i++) {
StudentList[i].display();
}
}
}
}
![trung [C1907L]](https://www.gravatar.com/avatar/67c23432e4710f33dd14e580d41b0379.jpg?s=80&d=mm&r=g)
trung
2020-07-06 14:30:46
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace qlsv
{
[Serializable]
public class ClassRoom
{
public string Name { get; set; }
public string Address { get; set; }
public List<Student> ListStudent { get; set; }
public ClassRoom()
{
}
public void Input()
{
ListStudent = new List<Student>();
Console.WriteLine("Input classroom name:");
Name = Console.ReadLine();
Console.WriteLine("Input classroom address:");
Address = Console.ReadLine();
int i = 1;
do
{
Console.WriteLine("Input information for student number " + (i));
Student std = new Student();
std.Input();
ListStudent.Add(std);
Console.WriteLine("Add more student ? (y/n)");
i++;
} while (Console.ReadLine().Equals("y"));
}
public void Output()
{
Console.WriteLine("Classroom name:" + Name);
Console.WriteLine("Address: " + Address);
Console.WriteLine("List student is : ");
foreach(Student std in ListStudent)
{
std.Output();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace qlsv
{
[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("Input fullname");
FullName = Console.ReadLine();
Console.WriteLine("Input birth day");
BirthDay = Console.ReadLine();
Console.WriteLine("Input email");
Email = Console.ReadLine();
Console.WriteLine("Input address");
Address = Console.ReadLine();
Console.WriteLine("Input gender");
Gender = Console.ReadLine();
}
public void Output()
{
Console.WriteLine("Fullname: {0}",FullName);
Console.WriteLine("Birthday: {0}", BirthDay);
Console.WriteLine("Email: {0}", Email);
Console.WriteLine("Address: {0}", Address);
Console.WriteLine("Gender: {0}", Gender);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace qlsv
{
public class Program
{
public static void Main(string[] args)
{
//tao json file
/*List<ClassRoom> classList = new List<ClassRoom>();
do
{
ClassRoom newClr = new ClassRoom();
newClr.Input();
classList.Add(newClr);
Console.WriteLine("Continue adding classroom (y/n) ?");
} while (Console.ReadLine().Equals("y"));
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(classList);
Console.WriteLine(jsonString);
System.IO.File.WriteAllText(@"test.json", jsonString);
*/
int choice = 0;
List<ClassRoom> realClassList = new List<ClassRoom>();
do
{
ShowMenu();
choice = int.Parse(Console.ReadLine());
switch(choice)
{
case 1:
string resultStr = System.IO.File.ReadAllText(@"test.json");
dynamic test = Newtonsoft.Json.JsonConvert.DeserializeObject(resultStr);
realClassList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(resultStr);
foreach(ClassRoom clr in realClassList)
{
clr.Output();
}
break;
case 2:
foreach(ClassRoom clr in realClassList)
{
foreach(Student std in clr.ListStudent)
{
std.Output();
}
}
break;
case 3:
foreach (ClassRoom clr in realClassList)
{
using (Stream stream = File.Open((clr.Name+".obj"), FileMode.Create))
{
var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
bformatter.Serialize(stream, clr);
}
}
break;
case 4:
Console.WriteLine("bye bye ...");
break;
default:
Console.WriteLine("Invalid input...");
break;
}
} while (choice != 4);
}
public static void ShowMenu()
{
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. Exit");
}
}
}
![Ngô Quang Huy [C1907L]](https://www.gravatar.com/avatar/8e54dbf5994077ce599f49278164ae79.jpg?s=80&d=mm&r=g)
Ngô Quang Huy
2020-07-06 14:09:36
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Menu
{
class Program
{
static List<ClassRoom> classList = new List<ClassRoom>();
static void Main(string[] args)
{
while (true)
{
showMenu();
int choose = int.Parse(Console.ReadLine());
switch (choose)
{
case 1:
Import();
break;
case 2:
Display();
break;
case 3:
Save();
break;
default:
break;
}
}
}
static void showMenu()
{
Console.WriteLine("\n1. Import from file JSON");
Console.WriteLine("2. Display student from class");
Console.WriteLine("3. Save to class_name.obj");
Console.Write("Choose: ");
}
static void Import()
{
string text = System.IO.File.ReadAllText(@"C:\\Users\\student.APROTRAIN-APTEC\\Desktop\\data.json");
classList = JsonConvert.DeserializeObject<List<ClassRoom>>(text);
}
static void Display()
{
foreach (ClassRoom cls in classList)
{
cls.output();
}
}
static void Save()
{
foreach (ClassRoom cls in classList)
{
using (Stream stream = File.Open(cls.name+".obj", FileMode.Create))
{
var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
binaryFormatter.Serialize(stream, cls);
}
}
Console.WriteLine("Save!!");
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Menu
{
[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("Insert fullname: ");
fullname = Console.ReadLine();
Console.WriteLine("Insert birthday: ");
birthday = Console.ReadLine();
Console.WriteLine("Insert email: ");
email = Console.ReadLine();
Console.WriteLine("Insert address: ");
address = Console.ReadLine();
Console.WriteLine("Insert gender: ");
gender = Console.ReadLine();
}
public void output()
{
Console.WriteLine("fullname: {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 Menu
{
[Serializable]
class ClassRoom
{
public string name { get; set; }
public string address { get; set; }
public List<Student> studentList { get; set; }
//List<Student> studentList = new List<Student>();
public ClassRoom()
{
studentList = new List<Student>();
}
public void input()
{
Console.WriteLine("Insert name: ");
name = Console.ReadLine();
Console.WriteLine("Insert address: ");
address = Console.ReadLine();
while (true)
{
Student std1 = new Student();
std1.input();
studentList.Add(std1);
Console.WriteLine("Continue? y/n");
string confirm = Console.ReadLine();
if (confirm.Equals("n"))
{
break;
}
}
}
public void output()
{
Console.WriteLine("name: {0}; address: {1}", name, address);
foreach (Student tmp in studentList)
{
tmp.output();
}
}
}
}
![Luong Dinh Dai [T1907A]](https://www.gravatar.com/avatar/ca08fa4090e1038e541197564747f93c.jpg?s=80&d=mm&r=g)
Luong Dinh Dai
2020-06-04 08:50:46
#classRomm.json
{
"information": {
"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"
}
]
}
#ClassRoom.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ReadWriteFile
{
[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;
while (ischeck)
{
Student st = new Student();
st.input();
studentList.Add(st);
Console.WriteLine("Ban co muon tiep tuc them sv ko");
Console.WriteLine("1. Yes");
Console.WriteLine("2. No");
int choose = Convert.ToInt32(Console.ReadLine());
if (choose == 1)
ischeck = false;
}
}
public void display()
{
Console.WriteLine("NameClass={0},Address={1}",Name,Address);
foreach (var item in studentList)
{
Console.WriteLine("chay voa day");
item.display();
}
}
}
}
#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"
}
]
}
]
#Manage.cs
using Nancy.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Xml.Serialization;
namespace ReadWriteFile
{
class Manage
{
public static void showInfoClassList(List<ClassRoom> classList)
{
Console.WriteLine("Danh sach sinh vien theo classroom");
foreach (var item in classList)
{
item.display();
}
}
public static void ImportJson()
{
List<ClassRoom> classList = new List<ClassRoom>();
string _url = "D:\\C#nangcao\\C#FPT\\T1907A\\ReadWriteFile\\data.json";
String st = File.ReadAllText(_url);
classList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(st);
Console.WriteLine(classList.Count);
foreach (var item in classList)
{
item.display();
}
}
}
}
#Program.cs
using System;
namespace ReadWriteFile
{
class Program
{
static void Main(string[] args)
{
int choose;
do
{
showmenu();
choose = Convert.ToInt32(Console.ReadLine());
switch (choose)
{
case 1:
Manage.ImportJson();
break;
}
} while (true);
}
public static void showmenu()
{
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("Choose");
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ReadWriteFile
{
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 void display()
{
Console.WriteLine("FullName ={0},Birthday={1},Email={2},Address={3},Gender={4}",Fullname,Birthday,Email,Address,Gender);
}
public void input()
{
Console.WriteLine("input fullname ");
Fullname = Console.ReadLine();
Console.WriteLine("input birthday");
Birthday = Console.ReadLine();
Console.WriteLine("input email");
Email = Console.ReadLine();
Console.WriteLine("input address");
Address = Console.ReadLine();
Console.WriteLine("input gender");
Gender = Console.ReadLine();
}
}
}
![thienphu [T1907A]](https://www.gravatar.com/avatar/c4573ea65e411176c1852fd8584f1ab1.jpg?s=80&d=mm&r=g)
thienphu
2020-06-02 13:07:56
#classRomm.json
{
"information": {
"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"
}
]
}
#ClassRoom.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ReadWriteFile
{
[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;
while (ischeck)
{
Student st = new Student();
st.input();
studentList.Add(st);
Console.WriteLine("Ban co muon tiep tuc them sv ko");
Console.WriteLine("1. Yes");
Console.WriteLine("2. No");
int choose = Convert.ToInt32(Console.ReadLine());
if (choose == 1)
ischeck = false;
}
}
public void display()
{
Console.WriteLine("NameClass={0},Address={1}",Name,Address);
foreach (var item in studentList)
{
Console.WriteLine("chay voa day");
item.display();
}
}
}
}
#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"
}
]
}
]
#Manage.cs
using Nancy.Json;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Xml.Serialization;
namespace ReadWriteFile
{
class Manage
{
public static void showInfoClassList(List<ClassRoom> classList)
{
Console.WriteLine("Danh sach sinh vien theo classroom");
foreach (var item in classList)
{
item.display();
}
}
public static void ImportJson()
{
List<ClassRoom> classList = new List<ClassRoom>();
string _url = "D:\\C#nangcao\\C#FPT\\T1907A\\ReadWriteFile\\data.json";
String st = File.ReadAllText(_url);
classList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<ClassRoom>>(st);
Console.WriteLine(classList.Count);
foreach (var item in classList)
{
item.display();
}
}
}
}
#Program.cs
using System;
namespace ReadWriteFile
{
class Program
{
static void Main(string[] args)
{
int choose;
do
{
showmenu();
choose = Convert.ToInt32(Console.ReadLine());
switch (choose)
{
case 1:
Manage.ImportJson();
break;
}
} while (true);
}
public static void showmenu()
{
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("Choose");
}
}
}
#Student.cs
using System;
using System.Collections.Generic;
using System.Text;
namespace ReadWriteFile
{
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 void display()
{
Console.WriteLine("FullName ={0},Birthday={1},Email={2},Address={3},Gender={4}",Fullname,Birthday,Email,Address,Gender);
}
public void input()
{
Console.WriteLine("input fullname ");
Fullname = Console.ReadLine();
Console.WriteLine("input birthday");
Birthday = Console.ReadLine();
Console.WriteLine("input email");
Email = Console.ReadLine();
Console.WriteLine("input address");
Address = Console.ReadLine();
Console.WriteLine("input gender");
Gender = Console.ReadLine();
}
}
}