diff --git a/import.cs b/import.cs new file mode 100644 index 0000000..d5e7ee7 --- /dev/null +++ b/import.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; + +namespace spplus; + +public static class import +{ + public static List ImportStudentsFromFile(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