From 285d82222b0524b772ed6049678a93aa8e3271f0 Mon Sep 17 00:00:00 2001 From: Darinka Loncar <149894872+darinkaloncar@users.noreply.github.com> Date: Fri, 29 Mar 2024 03:13:32 -0700 Subject: [PATCH 1/4] [Update] private attributes, getters and setters in Course class --- LangLang/Core/Model/Course.cs | 57 ++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/LangLang/Core/Model/Course.cs b/LangLang/Core/Model/Course.cs index df643621..fbd406ae 100644 --- a/LangLang/Core/Model/Course.cs +++ b/LangLang/Core/Model/Course.cs @@ -10,54 +10,68 @@ namespace LangLang.Core.Model class Course { // Attributes - private string language; - private LanguageLevel level; - private int numberOfWeeks; - private List days; - private bool online; - private int numberOfStudents; - private int maxStudents; + private int _id; + private string _language; + private LanguageLevel _level; + private int _numberOfWeeks; + private List _days; + private bool _online; + private int _numberOfStudents; + private int _maxStudents; + private DateTime _creationDate; // Properties + + public int Id + { + get { return _id; } + set { _id = value; } + } public string Language { - get { return language; } - set { language = value; } + get { return _language; } + set { _language = value; } } public LanguageLevel Level { - get { return level; } - set { level = value; } + get { return _level; } + set { _level = value; } } public int NumberOfWeeks { - get { return numberOfWeeks; } - set { numberOfWeeks = value; } + get { return _numberOfWeeks; } + set { _numberOfWeeks = value; } } public List Days { - get { return days; } - set { days = value; } + get { return _days; } + set { _days = value; } } public bool Online { - get { return online; } - set { online = value; } + get { return _online; } + set { _online = value; } } public int NumberOfStudents { - get { return numberOfStudents; } - set { numberOfStudents = value; } + get { return _numberOfStudents; } + set { _numberOfStudents = value; } } public int MaxStudents { - get { return maxStudents; } - set { maxStudents = value; } + get { return _maxStudents; } + set { _maxStudents = value; } + } + + public DateTime CreationDate + { + get { return _creationDate; } + set { _creationDate = value; } } // Constructors @@ -71,6 +85,7 @@ public Course(string language, LanguageLevel level, int numberOfWeeks, List Date: Fri, 29 Mar 2024 04:06:40 -0700 Subject: [PATCH 2/4] [Add] ToCSV, FromCSV, ToString to Course class --- LangLang/Core/Model/Course.cs | 72 ++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/LangLang/Core/Model/Course.cs b/LangLang/Core/Model/Course.cs index fbd406ae..8814f2f2 100644 --- a/LangLang/Core/Model/Course.cs +++ b/LangLang/Core/Model/Course.cs @@ -76,14 +76,15 @@ public DateTime CreationDate // Constructors - public Course(string language, LanguageLevel level, int numberOfWeeks, List days, bool online, int numberOfStudents, int maxStudents) + public Course(int id, string language, LanguageLevel level, int numberOfWeeks, List days, bool online, int maxStudents) { + Id = id; Language = language; Level = level; NumberOfWeeks = numberOfWeeks; Days = days; Online = online; - NumberOfStudents = numberOfStudents; + NumberOfStudents = 0; MaxStudents = maxStudents; CreationDate = DateTime.Now; } @@ -91,5 +92,72 @@ public Course(string language, LanguageLevel level, int numberOfWeeks, List 0) + { + sbDays.Remove(sbDays.Length - 1, 1); + } + + return $"ID: {Id,5} | Language: {Language,20} | Level: {Level,5} | NumberOfWeeks: {NumberOfWeeks,5} | Days: {sbDays, 10} | Online: {Online,5} | NumberOfStudents : {NumberOfStudents,5} | MaxStudents : {MaxStudents,5} | CreationDate : {CreationDate,10} |"; + } + + public string[] ToCSV() + { + StringBuilder sbDays = new StringBuilder(); + foreach (WeekDays day in Days) + { + sbDays.Append(day.ToString() + " "); + } + + // Deletes the last white space from stringbuilder + if (sbDays.Length > 0) + { + sbDays.Remove(sbDays.Length - 1, 1); + } + + string[] csvValues = + { + Id.ToString(), + Language, + Level.ToString(), + NumberOfWeeks.ToString(), + sbDays.ToString(), + Online.ToString(), + NumberOfStudents.ToString(), + MaxStudents.ToString(), + CreationDate.ToString() + }; + return csvValues; + } + + public void FromCSV(string[] values) + { + Id = int.Parse(values[0]); + Language = values[1]; + Level = (LanguageLevel)Enum.Parse(typeof(LanguageLevel), values[2]); + NumberOfWeeks = int.Parse(values[3]); + + // Converting from string to list of WeekDays + string[] days = values[4].Split(' '); + Days = new List(); + foreach (string day in days) + { + Days.Add((WeekDays)Enum.Parse(typeof(WeekDays), day)); + } + + Online = bool.Parse(values[5]); + NumberOfStudents = int.Parse(values[6]); + MaxStudents = int.Parse(values[7]); + CreationDate = DateTime.Parse(values[8]); + } } } From 6787ab7b86ab19e822cd5b17053b6580863b46b8 Mon Sep 17 00:00:00 2001 From: Darinka Loncar <149894872+darinkaloncar@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:49:17 +0100 Subject: [PATCH 3/4] [Add] CoursesDAO class Created class CoursesDAO with a list of Course class, repository for Course class and CRUD functions. --- LangLang/Core/Model/DAO/CoursesDAO.cs | 74 +++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 LangLang/Core/Model/DAO/CoursesDAO.cs diff --git a/LangLang/Core/Model/DAO/CoursesDAO.cs b/LangLang/Core/Model/DAO/CoursesDAO.cs new file mode 100644 index 00000000..13a1b872 --- /dev/null +++ b/LangLang/Core/Model/DAO/CoursesDAO.cs @@ -0,0 +1,74 @@ +using System.Collections.Generic; +using System.Linq; +using LangLang.Core.Repository; +using LangLang.Core.Model; +using LangLang.Core.Observer; + +namespace LangLang.Core.Model.DAO; + +public class CoursesDAO : Subject +{ + private readonly List _courses; + private readonly Repository _repository; + + public CoursesDAO() + { + _repository = new Repository("courses.csv"); + _courses = _repository.Load(); + } + + private int GenerateId() + { + if (_courses.Count == 0) return 0; + return _courses.Last().Id + 1; + } + + public Course AddCourse(Course Course) + { + Course.Id = GenerateId(); + _courses.Add(Course); + _repository.Save(_courses); + NotifyObservers(); + return Course; + } + + public Course UpdateCourse(Course Course) + { + Course oldCourse = GetCourseById(Course.Id); + if (oldCourse == null) return null; + + oldCourse.Language = Course.Language; + oldCourse.Level = Course.Level; + oldCourse.NumberOfWeeks = Course.NumberOfWeeks; + oldCourse.Days = Course.Days; + oldCourse.Online = Course.Online; + oldCourse.NumberOfStudents = Course.NumberOfStudents; + oldCourse.MaxStudents = Course.MaxStudents; + + _repository.Save(_courses); + NotifyObservers(); + return oldCourse; + } + + public Course RemoveCourse(int id) + { + Course course = GetCourseById(id); + if (course == null) return null; + + _courses.Remove(course); + _repository.Save(_courses); + NotifyObservers(); + return course; + } + + private Course GetCourseById(int id) + { + return _courses.Find(v => v.Id == id); + } + + public List GetAllCourses() + { + return _courses; + } + +} \ No newline at end of file From 2342e36d7bcfedbba9682aa4dfe682c693c94f20 Mon Sep 17 00:00:00 2001 From: Darinka Loncar <149894872+darinkaloncar@users.noreply.github.com> Date: Fri, 29 Mar 2024 12:51:47 +0100 Subject: [PATCH 4/4] [Update] Course class to be public --- LangLang/Core/Model/Course.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/LangLang/Core/Model/Course.cs b/LangLang/Core/Model/Course.cs index 8814f2f2..5024b437 100644 --- a/LangLang/Core/Model/Course.cs +++ b/LangLang/Core/Model/Course.cs @@ -4,10 +4,11 @@ using System.Text; using System.Threading.Tasks; using System.Xml.Linq; +using LangLang.Core.Repository.Serialization; namespace LangLang.Core.Model { - class Course + public class Course : ISerializable { // Attributes private int _id;