diff --git a/MeetTheTeacher/Model/Comparison/ByHourComparer.cs b/MeetTheTeacher/Model/Comparison/ByHourComparer.cs index 41b2319..e147f39 100644 --- a/MeetTheTeacher/Model/Comparison/ByHourComparer.cs +++ b/MeetTheTeacher/Model/Comparison/ByHourComparer.cs @@ -16,7 +16,20 @@ public sealed class ByHourComparer : ComparerBase, IComparer public int Compare(Teacher? x, Teacher? y) { - // TODO - return -1; + int nullCheck = CheckNull(x, y); + if (nullCheck != 2) + { + return nullCheck; + } + + // Ignore possibility of null, bc we already checked it above + int dayComparison = CompareEnum(x!.ConsultingHourWeekDay, y!.ConsultingHourWeekDay); + if (dayComparison != 0) + { + return ApplyCorrectDirection(dayComparison); + } + + int unitComparison = CompareEnum(x.ConsultingHourUnit, y.ConsultingHourUnit); + return ApplyCorrectDirection(unitComparison); } } diff --git a/MeetTheTeacher/Model/Comparison/ByNameComparer.cs b/MeetTheTeacher/Model/Comparison/ByNameComparer.cs index fa7fe79..4b09f7c 100644 --- a/MeetTheTeacher/Model/Comparison/ByNameComparer.cs +++ b/MeetTheTeacher/Model/Comparison/ByNameComparer.cs @@ -16,7 +16,12 @@ public sealed class ByNameComparer : ComparerBase, IComparer public int Compare(Teacher? x, Teacher? y) { - // TODO - return -1; + int nullCheck = CheckNull(x, y); + if (nullCheck != 2) + { + return nullCheck; + } + + return ApplyCorrectDirection(string.Compare(x!.Name, y!.Name, StringComparison.Ordinal)); } } diff --git a/MeetTheTeacher/Model/Comparison/ByRoomComparer.cs b/MeetTheTeacher/Model/Comparison/ByRoomComparer.cs index 4877614..b053b9c 100644 --- a/MeetTheTeacher/Model/Comparison/ByRoomComparer.cs +++ b/MeetTheTeacher/Model/Comparison/ByRoomComparer.cs @@ -8,7 +8,19 @@ public sealed class ByRoomComparer : IComparer { public int Compare(Teacher? x, Teacher? y) { - // TODO - return -1; + if (x == null && y == null) + { + return 0; + } + if (x == null) + { + return 1; + } + if (y == null) + { + return -1; + } + + return string.Compare(x!.Room, y!.Room, StringComparison.Ordinal); } } diff --git a/MeetTheTeacher/Model/Comparison/ComparerBase.cs b/MeetTheTeacher/Model/Comparison/ComparerBase.cs index 6ad1d4d..8c75632 100644 --- a/MeetTheTeacher/Model/Comparison/ComparerBase.cs +++ b/MeetTheTeacher/Model/Comparison/ComparerBase.cs @@ -18,7 +18,59 @@ public abstract class ComparerBase /// Gets if the comparer is configured to sort in ascending order; descending if false /// protected bool Ascending { get; } - - // TODO: consider moving the basic RefEquals code of CompareTo from the subclasses here - // maybe also rename this to SortOrderComparerBase or something and introduce another, common base class for all three? + + /// + /// Checks if a is null + /// + /// Teacher x + /// Teacher y + /// 0 if both are null, the comparison if only one is null and 2 if both are NOT null + protected int CheckNull(Teacher? x, Teacher? y) + { + if (x == null && y == null) + { + return 0; + } + if (x == null) + { + return Ascending ? 1 : -1; + } + if (y == null) + { + return Ascending ? -1 : 1; + } + + return 2; + } + + /// + /// Compares the two enums + /// + /// First enum + /// Second enum + /// Enum Type + /// The comparison + protected int CompareEnum(TEnum? x, TEnum? y) where TEnum : struct, Enum + { + if (!x.HasValue && !y.HasValue) + { + return 0; + } + if (!x.HasValue) + { + return -1; + } + if (!y.HasValue) + { + return 1; + } + return x.Value.CompareTo(y.Value); + } + + /// + /// Applies the correct direction + /// + /// The comparison + /// if is true, else returns negated value + protected int ApplyCorrectDirection(int comparison) => Ascending ? comparison : -comparison; } diff --git a/MeetTheTeacher/Model/CsvData.cs b/MeetTheTeacher/Model/CsvData.cs index d6cbc59..ccaf9fb 100644 --- a/MeetTheTeacher/Model/CsvData.cs +++ b/MeetTheTeacher/Model/CsvData.cs @@ -35,13 +35,13 @@ public sealed class CsvData /// Gets a CSV header based on /// /// A CSV header line - public string GetHeader() => null!; // TODO + public string GetHeader() => string.Join(Separator.ToString(), HeaderNames); /// /// Gets a CSV data line based on /// /// A CSV data line - public string GetData() => null!; // TODO + public string GetData() => string.Join(Separator.ToString(), Data); /// /// Allows to deconstruct the object into its header and data parts @@ -50,8 +50,7 @@ public sealed class CsvData /// Set to public void Deconstruct(out IReadOnlyList headerNames, out IReadOnlyList data) { - // TODO - headerNames = null!; - data = null!; + headerNames = HeaderNames; + data = Data; } } diff --git a/MeetTheTeacher/Model/Teacher.cs b/MeetTheTeacher/Model/Teacher.cs index d191b98..dfa8bce 100644 --- a/MeetTheTeacher/Model/Teacher.cs +++ b/MeetTheTeacher/Model/Teacher.cs @@ -45,10 +45,25 @@ public class Teacher : ICsvRepresentable { get { - // TODO - return null!; + if (ConsultingHour == null) + { + return null; + } + + return ConsultingHourUnit == null ? ConsultingHour.ToString() : $"{ConsultingHour} ({ConsultingHourUnit})"; } } - public virtual CsvData ToCsvData() => null!; // TODO + public virtual CsvData ToCsvData() + { + List headerNames = ["Name", "Day", "ConsultingHour", "Room"]; + List dataLine = [ + Name, + ConsultingHourWeekDay?.ToString() ?? string.Empty, + ConsultingHourTime ?? string.Empty, + Room ?? string.Empty + ]; + var data = new CsvData(headerNames, dataLine); + return data; + } } diff --git a/MeetTheTeacher/Model/TeacherWithBusinessCard.cs b/MeetTheTeacher/Model/TeacherWithBusinessCard.cs index a3c4671..d0de110 100644 --- a/MeetTheTeacher/Model/TeacherWithBusinessCard.cs +++ b/MeetTheTeacher/Model/TeacherWithBusinessCard.cs @@ -26,7 +26,16 @@ public sealed class TeacherWithBusinessCard : Teacher public override CsvData ToCsvData() { - // TODO (remember to maybe use the base implementation...) - return null!; + var data = base.ToCsvData(); + (IReadOnlyList headerNamesBase, IReadOnlyList dataLineBase) = data; + + var headerNames = (List)headerNamesBase; + var dataLine = (List)dataLineBase; + + headerNames.Add("Image"); + dataLine.Add($"{BaseUrl}/{Id}"); + + var csvData = new CsvData(headerNames, dataLine); + return csvData; } } diff --git a/MeetTheTeacher/Model/TimeFrame.cs b/MeetTheTeacher/Model/TimeFrame.cs index e509e6c..26e5eed 100644 --- a/MeetTheTeacher/Model/TimeFrame.cs +++ b/MeetTheTeacher/Model/TimeFrame.cs @@ -21,18 +21,30 @@ public readonly record struct TimeFrame(TimeOnly Start, TimeOnly End) /// True if time frame could be parsed successfully; false otherwise public static bool TryParse(string timeFrame, out TimeFrame? result) { - // TODO result = null; - return false; + string[] timeFrames = timeFrame.Split("-"); + if (timeFrames.Length != 2) + { + return false; + } + + if (!TimeOnly.TryParse(timeFrames[0], out TimeOnly start) || !TimeOnly.TryParse(timeFrames[1], out TimeOnly end)) + { + return false; + } + + if (start > end) + { + (start, end) = (end, start); + } + + result = new TimeFrame(start, end); + return true; } /// /// Returns a string representation of the time frame in format HH:MM-HH:MM /// /// - public override string ToString() - { - // TODO - return null!; - } + public override string ToString() => $"{Start}-{End}"; }