diff --git a/import.cs b/import.cs index d5e7ee7..148d62c 100644 --- a/import.cs +++ b/import.cs @@ -47,4 +47,45 @@ public static class import return result; } + + public static List ImportResultFromFile(string path) + { + var dict = new Dictionary Courses)>(); + + foreach (var line in File.ReadLines(path).Skip(1)) // Header überspringen + { + if (string.IsNullOrWhiteSpace(line)) + continue; + + var parts = line.Split(','); + if (parts.Length < 3) + continue; + + string nameWithId = parts[0].Trim(); + string course = parts[2].Replace("(2)", "").Replace("(3)", "").Replace("(4)", "").Trim(); + + int open = nameWithId.LastIndexOf('('); + int close = nameWithId.LastIndexOf(')'); + if (open < 0 || close < 0 || close <= open) + continue; + + string name = nameWithId[..open].Trim(); + string id = nameWithId[(open + 1)..close].Trim(); + + if (!dict.ContainsKey(id)) + dict[id] = (name, new List()); + + dict[id].Courses.Add(course); + } + + var result = new List(); + + foreach (var (id, data) in dict) + { + var student = new Student(id, data.Name, data.Courses); + result.Add(student); + } + + return result; + } } \ No newline at end of file