private void add_book_btn_click(object sender, EventArgs e) { try { string name = author_select_combo_box.SelectedItem.ToString(); Author current = authorService.GetAuthorOnName(name); Book b = new Book { Title = book_title_txt_box.Text, Description = book_description_txt_box.Text, AuthorOfBook = authorService.Find(current.AuthorID) }; bookService.Add(b); } catch (Exception ex) { UserError(ex); } }
/// <summary> /// Button to create a new book /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCreateNewBook_Click(object sender, EventArgs e) { Author addNewAuthor; if (chckAddNewAuthor.Checked == false) { addNewAuthor = lbAuthor.SelectedItem as Author; Debug.WriteLine(addNewAuthor); } else { addNewAuthor = new Author(txtAuthorName.Text); authorService.Add(addNewAuthor); } try { Regex rx = new Regex(@"^[0-9]{10,13}$"); if (rx.Match(txtBookISBN.Text).Success) { Book newBook = new Book() { ISBN = txtBookISBN.Text, Title = txtBookTitle.Text, Description = txtBookDesc.Text, BookAuthor = addNewAuthor }; bookService.Add(newBook); MessageBox.Show("Book added"); } else { MessageBox.Show("Invalid ISBN, should be a combination of 10 or 13 digits"); } } catch (Exception exp) { MessageBox.Show("Unable to add, make sure you selected an author."); Debug.WriteLine(exp); } }
/// <summary> /// "Add book"-button /// </summary> private void btnAddNewBook_Click(object sender, EventArgs e) { if (textBoxISBN.Text == "" || textBoxTitle.Text == "" || textBoxDescription.Text == "" || (Author)comboBoxAuthor.SelectedItem == null) { MessageBox.Show("You need to fill in all book details.", "Error!"); } else { var author = (Author)comboBoxAuthor.SelectedItem; Book book = new Book(textBoxISBN.Text, textBoxTitle.Text, textBoxDescription.Text, author); author.Books.Add(book); bookService.Add(book); MessageBox.Show("You have now added the book: " + textBoxTitle.Text); textBoxISBN.Clear(); textBoxTitle.Clear(); textBoxDescription.Clear(); ShowAllBooks(bookService.All()); ShowAllBooksInComboBox(bookService.All()); } }
private void AddBookButton_Click(object sender, EventArgs e) { Author selectedAuthor = Author_listBox.SelectedItem as Author; int copies; string selectedCopies = numberOfCopies_textBox.Text; bool isSelectedCopies = int.TryParse(selectedCopies, out copies); string isbn = ISBN_textBox.Text; string title = Title_textBox.Text; string description = Description_richTextBox.Text; if (isSelectedCopies == true && !string.IsNullOrEmpty(isbn) && !string.IsNullOrEmpty(title) && !string.IsNullOrEmpty(description) && selectedAuthor != null) { Book book = new Book(isbn, title, description, selectedAuthor, copies); bookService.Add(book); MessageBox.Show(title + " was succesfully added too library"); } else { MessageBox.Show("Invalid input"); } }
static void Main(string[] args) { JsonFileHandler fileHandler = new JsonFileHandler(); BookService library = new BookService(fileHandler); Console.WriteLine("-=====Menu=====-"); Console.WriteLine("1 - Show catalog"); Console.WriteLine("2 - Add new book"); Console.WriteLine("3 - Remove book"); Console.WriteLine("4 - Change book"); Console.WriteLine("0 - Exit"); Console.WriteLine("-==============-"); int choice = Convert.ToInt32(Console.ReadLine()); switch (choice) { case 1: library.ShowCatalog(); break; case 2: Book new_book = new Book(); Console.WriteLine("Input information about book"); try { Console.WriteLine("Input new ID"); new_book.Id = Convert.ToInt32(Console.ReadLine()); } catch { throw new Exception("Incorrect input!"); } Console.WriteLine("Input new Title"); new_book.Title = Console.ReadLine(); Console.WriteLine("Input new amount of Pages"); new_book.Pages = Convert.ToInt32(Console.ReadLine()); library.Add(new_book); break; case 3: try { library.ShowCatalog(); Console.WriteLine("Input necessary id"); library.Remove(Convert.ToInt32(Console.ReadLine())); } catch { throw new Exception("Incorrect input!"); } break; case 4: Book change_book = new Book(); library.ShowCatalog(); try { Console.WriteLine("Input ID"); change_book.Id = Convert.ToInt32(Console.ReadLine()); } catch { throw new Exception("Incorrect input!"); } Console.WriteLine("Input new information about book"); Console.WriteLine("Input new Title"); change_book.Title = Console.ReadLine(); Console.WriteLine("Input new amount of Pages"); change_book.Pages = Convert.ToInt32(Console.ReadLine()); library.Change(change_book); break; case 0: break; default: throw new Exception("Incorrect input!"); } }