Skip to content

Commit

Permalink
Merge pull request #27 from kzi-nastava/feat/Course
Browse files Browse the repository at this point in the history
[Add] CoursesDAO class to Model; functions and attributes to Course class
  • Loading branch information
DusicaPesic authored Mar 29, 2024
2 parents 999d5bd + 2342e36 commit 79b8a4a
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 24 deletions.
132 changes: 108 additions & 24 deletions LangLang/Core/Model/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,161 @@
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 string language;
private LanguageLevel level;
private int numberOfWeeks;
private List<WeekDays> days;
private bool online;
private int numberOfStudents;
private int maxStudents;
private int _id;
private string _language;
private LanguageLevel _level;
private int _numberOfWeeks;
private List<WeekDays> _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<WeekDays> 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

public Course(string language, LanguageLevel level, int numberOfWeeks, List<WeekDays> days, bool online, int numberOfStudents, int maxStudents)
public Course(int id, string language, LanguageLevel level, int numberOfWeeks, List<WeekDays> 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;
}

public Course()
{
}

public override string ToString()
{
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);
}

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<WeekDays>();
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]);
}
}
}
74 changes: 74 additions & 0 deletions LangLang/Core/Model/DAO/CoursesDAO.cs
Original file line number Diff line number Diff line change
@@ -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<Course> _courses;
private readonly Repository<Course> _repository;

public CoursesDAO()
{
_repository = new Repository<Course>("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<Course> GetAllCourses()
{
return _courses;
}

}

0 comments on commit 79b8a4a

Please sign in to comment.