Implemented the importing
This commit is contained in:
parent
8aa236233a
commit
4dfeb3d683
1 changed files with 95 additions and 10 deletions
|
|
@ -26,29 +26,114 @@ public sealed class TeacherDataCsvImporter : ITeacherDataImporter
|
|||
|
||||
public IEnumerable<Teacher> LoadTeacherData()
|
||||
{
|
||||
// TODO
|
||||
return null!;
|
||||
var basicData = ReadTeacherData() ?? new Dictionary<string, Teacher>();
|
||||
var advancedData = ReadTeacherBusinessCardData() ?? new List<(string, int)>();
|
||||
var completeData = CombineData(basicData, advancedData);
|
||||
|
||||
var teacherList = new List<Teacher>();
|
||||
foreach (var teacher in completeData)
|
||||
{
|
||||
teacherList.Add(teacher.Value);
|
||||
}
|
||||
return teacherList;
|
||||
}
|
||||
|
||||
private static Dictionary<string, Teacher> CombineData(Dictionary<string, Teacher> generalData,
|
||||
IEnumerable<(string name, int id)> businessCardData)
|
||||
{
|
||||
// Note: this method is designed specifically to force you to deal with the dictionary and
|
||||
// different teacher classes as value. It could be done in different ways as well, of course.
|
||||
|
||||
// TODO
|
||||
return null!;
|
||||
Dictionary<string, Teacher> result = new Dictionary<string, Teacher>();
|
||||
|
||||
Dictionary<string, int> businessCardDict = new Dictionary<string, int>();
|
||||
foreach (var item in businessCardData)
|
||||
{
|
||||
businessCardDict[item.name] = item.id;
|
||||
}
|
||||
|
||||
foreach (var (name, teacher) in generalData)
|
||||
{
|
||||
if (businessCardDict.TryGetValue(name, out int id))
|
||||
{
|
||||
var teacherWithCard = new TeacherWithBusinessCard(name, id)
|
||||
{
|
||||
ConsultingHourWeekDay = teacher.ConsultingHourWeekDay,
|
||||
ConsultingHourUnit = teacher.ConsultingHourUnit,
|
||||
ConsultingHour = teacher.ConsultingHour,
|
||||
Room = teacher.Room
|
||||
};
|
||||
|
||||
result.Add(name, teacherWithCard);
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Add(name, teacher);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private IEnumerable<(string name, int id)>? ReadTeacherBusinessCardData()
|
||||
{
|
||||
// TODO - CSV import (mind the return type: ValueTuple!)
|
||||
return null!;
|
||||
if (!File.Exists(_businessCardDataFilePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string[] lines = File.ReadAllLines(_businessCardDataFilePath);
|
||||
|
||||
List<(string name, int id)> businessData = new List<(string name, int id)>();
|
||||
foreach (string line in lines)
|
||||
{
|
||||
IReadOnlyList<string> dataLine = line.Split(CsvData.Separator);
|
||||
if (dataLine.Count != 2)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
bool success = int.TryParse(dataLine[0], out int id);
|
||||
if (!success)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
string name = dataLine[1];
|
||||
|
||||
businessData.Add((name, id));
|
||||
}
|
||||
|
||||
return businessData;
|
||||
}
|
||||
|
||||
private Dictionary<string, Teacher>? ReadTeacherData()
|
||||
{
|
||||
// TODO - CSV import
|
||||
return null!;
|
||||
if (!File.Exists(_generalDataFilePath))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
string[] lines = File.ReadAllLines(_generalDataFilePath);
|
||||
|
||||
Dictionary<string, Teacher> generalData = new Dictionary<string, Teacher>();
|
||||
foreach (string line in lines)
|
||||
{
|
||||
IReadOnlyList<string> dataLine = line.Split(CsvData.Separator);
|
||||
string name = dataLine[0];
|
||||
DayOfWeek? consultingDay = Enum.TryParse<DayOfWeek>(dataLine[1], true, out var day) ? day : null;
|
||||
SchoolUnit? consultingHourUnit = int.TryParse(dataLine[2], out var consultingHourUnitOut) ? (SchoolUnit?)consultingHourUnitOut : null;
|
||||
TimeFrame? consultingHour = TimeFrame.TryParse(dataLine[3], out TimeFrame? frame) ? frame : null;
|
||||
string? room = string.IsNullOrWhiteSpace(dataLine[4]) ? null : dataLine[4];
|
||||
|
||||
var teacher = new Teacher(name)
|
||||
{
|
||||
ConsultingHourWeekDay = consultingDay,
|
||||
ConsultingHourUnit = consultingHourUnit,
|
||||
ConsultingHour = consultingHour,
|
||||
Room = room,
|
||||
};
|
||||
|
||||
generalData.Add(name, teacher);
|
||||
}
|
||||
|
||||
return generalData;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue