private void AddButton_Click(object sender, RoutedEventArgs e) { string author = AuthorTextBox.Text; string title = TitleTextBox.Text; string releaseDate, nISBN; Categories category = (Categories)Enum.Parse(typeof(Categories), CategoryComboBox.Text); Types type = (Types)Enum.Parse(typeof(Types), TypeComboBox.Text); int numberOfPages, length, numberOfActors; int selectedCategory = CategoryComboBox.SelectedIndex; switch (selectedCategory) { case 0: try { nISBN = FirstAdditionalInformationInfoTextBox.Text; numberOfPages = int.Parse(SecondAdditionalInformationInfoTextBox.Text); Book book = new Book(title, author, category, type, nISBN, numberOfPages); BookList.Add(book); totalToRead = book.News(totalToRead, "add"); } catch { MessageBox.Show("Ilość stron powinna być podana jako liczba", "Zła wartość"); } break; case 1: try { length = int.Parse(SecondAdditionalInformationInfoTextBox.Text); numberOfActors = int.Parse(FirstAdditionalInformationInfoTextBox.Text); Audiobook audiobook = new Audiobook(title, author, category, type, length, numberOfActors); AudiobookList.Add(audiobook); totalToListen = audiobook.News(totalToListen, "add"); } catch { MessageBox.Show("Czas trwania oraz liczba aktorów powinny być podane jako liczby", "Zła wartość"); } break; case 2: try { releaseDate = FirstAdditionalInformationInfoTextBox.Text; length = int.Parse(SecondAdditionalInformationInfoTextBox.Text); Movie movie = new Movie(title, author, category, type, length, releaseDate); MovieList.Add(movie); totalToWatch = movie.News(totalToWatch, "add"); } catch { MessageBox.Show("Czas trwania powinien być podany jako liczba", "Zła wartość"); } break; } SetNewsTextBlocks(selectedCategory); }
private void ReadTXTFile_Click(object sender, RoutedEventArgs e) { try { string fileName = TXTfilePath.Text + TXTfileName.Text + ".txt"; FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); try { StreamReader streamReader = new StreamReader(fileStream); while (streamReader.EndOfStream == false) { string line = streamReader.ReadLine(); string[] table = line.Split('_'); if (table[0] == "Książka") { Categories category = (Categories)Enum.Parse(typeof(Categories), "Książka"); string title = table[1]; string author = table[2]; string nISBN = table[3]; int numberOfPages = int.Parse(table[4]); Types type = (Types)Enum.Parse(typeof(Types), table[5]); Book book = new Book(title, author, category, type, nISBN, numberOfPages); BookList.Add(book); totalToRead = book.News(totalToRead, "add"); } else if (table[0] == "Audiobook") { Categories category = (Categories)Enum.Parse(typeof(Categories), "Audiobook"); string title = table[1]; string author = table[2]; int length = int.Parse(table[3]); int numberOfActors = int.Parse(table[4]); Types type = (Types)Enum.Parse(typeof(Types), table[5]); Audiobook audiobook = new Audiobook(title, author, category, type, length, numberOfActors); AudiobookList.Add(audiobook); totalToListen = audiobook.News(totalToListen, "add"); } else if (table[0] == "Film") { Categories category = (Categories)Enum.Parse(typeof(Categories), "Film"); string title = table[1]; string author = table[2]; int length = int.Parse(table[3]); string releaseDate = table[4]; Types type = (Types)Enum.Parse(typeof(Types), table[5]); Movie movie = new Movie(title, author, category, type, length, releaseDate); MovieList.Add(movie); totalToWatch = movie.News(totalToWatch, "add"); } else { MessageBox.Show("Wystąpił błąd podczas odczytywana danych z pliku"); } } streamReader.Close(); MessageBox.Show("Wczytano pomyslnie"); } catch (Exception ex) { MessageBox.Show("Nie można wczytać pliku."); } int selectedCategory = CategoryComboBox.SelectedIndex; SetNewsTextBlocks(selectedCategory); } catch (Exception ex) { MessageBox.Show("Nie można otworzyć pliku."); } }