Files
spplus/import.cs

50 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace spplus;
public static class import
{
public static List<Student> ImportStudentsFromFile(string path)
{
var dict = new Dictionary<string, (string Name, List<string> 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<string>());
dict[id].Courses.Add(course);
}
var result = new List<Student>();
foreach (var (id, data) in dict)
{
var student = new Student(id, data.Name, data.Courses);
result.Add(student);
}
return result;
}
}