using System.Collections.Generic;
public static void Main()
var dataSource = new InMemoryStudentDataSource();
var manager = new StudentManager(dataSource);
var students = manager.GetStudents();
DumpStudentList("All students:", students);
var newStudent = new Student()
FullName = "Phan Thanh Thao",
Dob = new DateTime(2011, 4, 21),
Subjects = new List<string>()
DumpStudentList("After inserting new student:", students);
if (manager.DeleteById("2012517"))
Console.WriteLine("Student 2012517 has been deleted");
Console.WriteLine("Could not find student 2012517");
var filterCondition = new StudentNameFilter("van");
var vanStudents = manager.Find(filterCondition);
DumpStudentList("Filter by name containing 'van':", vanStudents);
var filterParam = new StudentQuery()
Subject = "HE DIEU HANH",
var complexCondition = new StudentComplexFilter(filterParam);
var studentSet = manager.Find(complexCondition);
DumpStudentList("Filter by complicated conditions:", studentSet);
Predicate<Student> matchFunc = delegate (Student sv)
return sv.Gender == "Female";
var femaleStudents = manager.Find(matchFunc);
DumpStudentList("Filter by gender = Female:", femaleStudents);
static void DumpStudentList(string title, List<Student> students)
Console.WriteLine(title);
Console.WriteLine("{0,-10}{1,-30}{2,-12:dd/MM/yyyy}{3,-10}{4,-10}{5,10}",
"MSSV", "Ho va Ten", "Ngay sinh", "Gioi tinh", "Lop", "So mon hoc");
Console.WriteLine("{0,-10}{1,-30}{2,-12:dd/MM/yyyy}{3,-10}{4,-10}{5,10}",
"---------", "-----------------------------", "-----------", "---------", "---------", "----------");
foreach (var item in students)
Console.WriteLine("{0,-10}{1,-30}{2,-12:dd/MM/yyyy}{3,-10}{4,-10}{5,10}",
item.Id, item.FullName, item.Dob, item.Gender, item.ClassName, item.Subjects.Count);
Console.WriteLine("{0,-10}{1,-30}{2,-12:dd/MM/yyyy}{3,-10}{4,-10}{5,10}",
"---------", "-----------------------------", "-----------", "---------", "---------", "----------");
public string Id { get; set; }
public string FullName { get; set; }
public DateTime Dob { get; set; }
public string Gender { get; set; }
public string ClassName { get; set; }
public List<string> Subjects { get; set; }
public class StudentManager
private IStudentDataSource _dataSource;
private List<Student> _studentList;
public StudentManager(IStudentDataSource dataSource)
throw new ArgumentNullException("dataSource");
_dataSource = dataSource;
_studentList = _dataSource.GetStudents();
public List<Student> GetStudents(bool forceReload = false)
_studentList = _dataSource.GetStudents();
public Student FindById(string id)
foreach (var item in _studentList)
if (item.Id == id) return item;
public int Find(Student student)
for (var idx = 0; idx < _studentList.Count; idx++)
if (_studentList[idx].Id == student.Id)
public List<Student> Find(IStudentFilter condition)
var results = new List<Student>();
foreach (var item in _studentList)
if (condition.IsMatch(item))
public List<Student> Find(Predicate<Student> matchFunc)
var results = new List<Student>();
foreach (var item in _studentList)
public bool Add(Student student)
if (FindById(student.Id) != null)
_studentList.Add(student);
_dataSource.Save(_studentList);
public void AddOrUpdate(Student student)
_studentList[idx] = student;
_dataSource.Save(_studentList);
public bool DeleteById(string id)
var student = FindById(id);
if (student == null) return false;
public void Delete(Student student)
_studentList.Remove(student);
_dataSource.Save(_studentList);
public void CreateFakeData()
var students = new List<Student>()
new Student() { Id = "2012345", FullName = "Nguyen Van Thanh Phong", Dob = new DateTime(2012, 6, 16), Gender = "Male", ClassName = "CTK44", Subjects = new List<string>() { "He dieu hanh", "Tin hoc co so" }},
new Student() { Id = "2012346", FullName = "Nguyen Van Thanh Son", Dob = new DateTime(2015, 8, 29), Gender = "Male", ClassName = "CTK44", Subjects = new List<string>() { "Lap trinh cau truc", "Tin hoc co so", "Toan cao cap" }},
new Student() { Id = "2012517", FullName = "Nguyen Thi Thao Nguyen", Dob = new DateTime(2014, 9, 11), Gender = "Female", ClassName = "CTK44", Subjects = new List<string>() { "Lap trinh Web", "Nhap mon nganh CNTT", "Giao duc the chat" }},
_dataSource.Save(students);
public interface IStudentDataSource
List<Student> GetStudents();
void Save(List<Student> students);
public class JsonFileStudentDataSource : IStudentDataSource
private string _filePath;
public JsonFileStudentDataSource(string filePath)
public List<Student> GetStudents()
if (!File.Exists(_filePath))
return new List<Student>();
var jsonData = File.ReadAllText(_filePath);
return JsonConvert.DeserializeObject<List<Student>>(jsonData);
public void Save(List<Student> students)
var jsonData = JsonConvert.SerializeObject(students);
File.WriteAllText(_filePath, jsonData);
public class InMemoryStudentDataSource : IStudentDataSource
private List<Student> _students;
public InMemoryStudentDataSource()
_students = new List<Student>()
new Student() { Id = "2012345", FullName = "Nguyen Van Thanh Phong", Dob = new DateTime(2012, 6, 16), Gender = "Male", ClassName = "CTK40", Subjects = new List<string>() { "He dieu hanh", "Tin hoc co so" }},
new Student() { Id = "2012346", FullName = "Nguyen Van Thanh Son", Dob = new DateTime(2015, 8, 29), Gender = "Male", ClassName = "CTK44", Subjects = new List<string>() { "Lap trinh cau truc", "Tin hoc co so", "Toan cao cap" }},
new Student() { Id = "2012517", FullName = "Nguyen Thi Thao Nguyen", Dob = new DateTime(2014, 9, 11), Gender = "Female", ClassName = "CTK43", Subjects = new List<string>() { "Lap trinh Web", "Nhap mon nganh CNTT", "Giao duc the chat" }},
new Student() { Id = "2012890", FullName = "Tran Thi Bich Thuy", Dob = new DateTime(2012, 9, 11), Gender = "Female", ClassName = "CTK40", Subjects = new List<string>() { "He dieu hanh", "Nhap mon nganh CNTT", "Giao duc the chat" }}
public List<Student> GetStudents()
public void Save(List<Student> students)
public interface IStudentFilter
bool IsMatch(Student student);
public class StudentNameFilter : IStudentFilter
public StudentNameFilter(string name)
_keyword = name.ToLower();
public bool IsMatch(Student student)
if (string.IsNullOrWhiteSpace(_keyword))
return student.FullName.ToLower().Contains(_keyword);
public class StudentComplexFilter : IStudentFilter
private StudentQuery _condition;
public StudentComplexFilter(StudentQuery condition)
public bool IsMatch(Student student)
if (!string.IsNullOrWhiteSpace(_condition.Name))
var name = _condition.Name.ToLower();
matched &= student.FullName.ToLower().Contains(name);
if (!string.IsNullOrWhiteSpace(_condition.Gender))
matched &= student.Gender.Equals(_condition.Gender, StringComparison.InvariantCultureIgnoreCase);
if (_condition.Age != null)
var age = DateTime.Today.Year - student.Dob.Year;
matched &= age == _condition.Age.Value;
if (!string.IsNullOrWhiteSpace(_condition.ClassName))
matched &= student.ClassName.Equals(_condition.ClassName, StringComparison.InvariantCultureIgnoreCase);
if (!string.IsNullOrWhiteSpace(_condition.Subject))
foreach (var subjectName in student.Subjects)
if (subjectName.Equals(_condition.Subject, StringComparison.InvariantCultureIgnoreCase))
public class StudentQuery
public string Name { get; set; }
public string Gender { get; set; }
public int? Age { get; set; }
public string ClassName { get; set; }
public string Subject { get; set; }