private void initLibrary() { string path = @"Database\MaterialItems.txt"; string[] materialData; materialData = File.ReadAllLines(path); foreach (string data in materialData) { string[] datas = data.Split('|'); string name = datas[1]; string author = datas[2]; string publisher = datas[3]; DateTime publishDate = Convert.ToDateTime(datas[4]); int stock = 0; bool isFile = false; if (datas[5] == "F") { isFile = true; stock = 0; } else { stock = Convert.ToInt16(datas[5]); } string type = datas[6]; switch (datas[0]) { case "Book": BookObject newBook = new BookObject(name, author, publisher, publishDate, stock); BookObserver newBookObserver = new BookObserver(newBook); this.materialList.Add(newBookObserver); break; case "EBook": EBookObject newEBook = new EBookObject(name, author, publisher, publishDate); EBookObserver newEBookObserver = new EBookObserver(newEBook); this.materialList.Add(newEBookObserver); break; case "Video": VideoObject newVideo = new VideoObject(name, type, author, publishDate, publisher, isFile, stock); VideoObserver newVideoObserver = new VideoObserver(newVideo); this.materialList.Add(newVideoObserver); break; case "Audio": AudioObject newAudio = new AudioObject(name, type, author, publishDate, publisher, isFile, stock); AudioObserver newAudioObserver = new AudioObserver(newAudio); this.materialList.Add(newAudioObserver); break; default: break; } } return; }
static void Main(string[] args) { var subject = new CardCatalogue(); var observer = new BookObserver("default name", subject); Console.WriteLine(observer.Name); subject.NotifyObservers(new PropertyChangeEventInformation("new name")); Console.WriteLine(observer.Name); }