public static ChildList ReadChildList(string path) { var myReader = new StreamReader(path); var child = myReader.ReadLine(); var lines = 0; while (child != null) // count lines { lines++; child = myReader.ReadLine(); } var childList = new ChildList(lines); myReader = new StreamReader(path); // reopen file child = myReader.ReadLine(); while (child != null) { childList.AddChild(Child.Deserialize(child)); child = myReader.ReadLine(); } myReader.Close(); return(childList); }
public void SaveChildList(ChildList childList, string path) { var myWriter = new StreamWriter(path, true); for (int child = 0; child < children.Length; child++) { myWriter.WriteLine(Child.Serialize(childList.children[child])); // writes one line for every child in childList } myWriter.Close(); }
static void Test() { Child k = new Child("Aris", Behavior.MostlyKind); k.AddWish("Sandspielzeug"); k.AddWish("Bausteine"); k.AddWish("Puppen"); Console.WriteLine(k.AsString()); ChildList liste = new ChildList(10); liste.AddChild(k); liste.AddChild(new Child("Anton", Behavior.AlwaysKind, new string[] { "Spielzeugauto", "Bausteine" })); liste.AddChild(new Child("Emma", Behavior.Naughty, new string[] { "Spielekonsole" })); liste.AddChild(new Child("Mehmet", Behavior.OftenKind, new string[] { "Bausteine", "Spielesammlung", "Sandspielzeug", "Computer" })); liste.AddChild(new Child("Esra", Behavior.MostlyKind, new string[] { "Handy", "Kinogutschein" })); liste.AddChild(new Child("Anna", Behavior.OftenKind, new string[] { "Musikstream", "Chemiebaukasten" })); liste.AddChild(new Child("Azra", Behavior.MostlyKind, new string[] { "Elektronik-Baukasten", "Computer", "Handy" })); liste.AddChild(new Child("Hans", Behavior.Naughty, new string[] { "Spielzeugautos", "Kinogutscheine" })); liste.AddChild(new Child("Egon", Behavior.AlwaysKind, new string[] { "Puppen", "Spielekonsole" })); liste.AddChild(new Child("Marie", Behavior.MostlyKind, new string[] { "Handy", "Spielekonsole" })); Console.WriteLine($"Anzahl lieber Kinder: {liste.NumberOfKindChildren}"); Console.WriteLine("So oft wünschen sich Kinder ..."); foreach (string wish in new string[] { "Handy", "Computer", "Spielekonsole", "Bausteine", "Puppen" }) { Console.WriteLine($"... {wish}: {liste.CountChildrenWithWish(wish)}"); } var r = Child.Deserialize(Child.Serialize(k)); liste.SaveChildList(liste, @"C:\Users\Chris\source\Prog12020\Praktikum08\children.txt"); var newList = ChildList.ReadChildList(@"C:\Users\Chris\source\Prog12020\Praktikum08\children.txt"); Console.WriteLine($"Anzahl lieber Kinder: {newList.NumberOfKindChildren}"); Console.WriteLine("So oft wünschen sich Kinder ..."); foreach (string wish in new string[] { "Handy", "Computer", "Spielekonsole", "Bausteine", "Puppen" }) { Console.WriteLine($"... {wish}: {newList.CountChildrenWithWish(wish)}"); } }