From 4c52a74d39407635ebdcb2d7a2ecdf56c7680849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ana=20=C5=A0inik?= Date: Sat, 30 Mar 2024 00:10:59 +0100 Subject: [PATCH] [Add] Implementation of StudentDAO class --- LangLang/Core/Model/DAO/StudentDAO.cs | 62 +++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 LangLang/Core/Model/DAO/StudentDAO.cs diff --git a/LangLang/Core/Model/DAO/StudentDAO.cs b/LangLang/Core/Model/DAO/StudentDAO.cs new file mode 100644 index 00000000..f44e4e0a --- /dev/null +++ b/LangLang/Core/Model/DAO/StudentDAO.cs @@ -0,0 +1,62 @@ +using System.Collections.Generic; +using System.Linq; +using LangLang.Core.Repository; +using LangLang.Core.Observer; + +namespace LangLang.Core.Model.DAO +{ + /** + * This class encapsulates a list of Student objects and provides methods + * for adding, updating, deleting, and retrieving Student objects. + * Additionally, this class uses Repository for loading and saving objects. + **/ + public class StudentDAO : Subject + { + private readonly List _students; + private readonly Repository _repository; + + + public StudentDAO() + { + _repository = new Repository("student.csv"); + _students = _repository.Load(); + } + + private int GenerateId() + { + if (_students.Count == 0) return 0; + return _students.Last().Profile.Id + 1; + } + + public Student AddStudent(Student student) + { + student.Profile.Id = GenerateId(); + _students.Add(student); + _repository.Save(_students); + NotifyObservers(); + return student; + } + + public Student? UpdateStudent(Student student) + { + Student oldStudent = GetStudentById(student.Profile.Id); + if (oldStudent == null) return null; + + oldStudent.Profile = student.Profile; + oldStudent.CanModifyInfo = student.CanModifyInfo; + oldStudent.ProfessionalQualification = student.ProfessionalQualification; + + _repository.Save(_students); + NotifyObservers(); + return oldStudent; + } + + public Student GetStudentById(int id) + { + return _students.Find(s => s.Profile.Id == id); + } + + // TODO: implement RemoveStudent(int id), GetAllStudent() + } + +}