private void BookToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryItem> items; // List of library items items = _lib.GetItemsList(); if ((items.Count() == 0)) { MessageBox.Show("Must have a book to edit!"); } else { BookSelectionForm bookSelectForm = new BookSelectionForm(items); // The return dialog box form DialogResult result = bookSelectForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) { LibraryBook b = (LibraryBook)_lib._items[bookSelectForm.ItemIndex]; BookForm bf = new BookForm(); bf.ItemTitle = b.Title; bf.ItemPublisher = b.Publisher; bf.ItemCopyrightYear = b.CopyrightYear.ToString(); bf.ItemLoanPeriod = b.LoanPeriod.ToString(); bf.ItemCallNumber = b.CallNumber; bf.BookAuthor = b.Author; DialogResult result1 = bf.ShowDialog(); if (result1 == DialogResult.OK) { b.Title = bf.ItemTitle; b.Publisher = bf.ItemPublisher; b.CopyrightYear = int.Parse(bf.ItemCopyrightYear); b.LoanPeriod = int.Parse(bf.ItemLoanPeriod); b.CallNumber = bf.ItemCallNumber; b.Author = bf.BookAuthor; } } bookSelectForm.Dispose(); } }
// Precondition: theCopyrightYear >= 0 and theLoanPeriod >= 0 // Postcondition: A library book has been created with the specified // values for title, publisher, copyright year, loan period, // call number, and author. The item is not checked out. // The book has been added to the Library. public bool AddLibraryBook(String theTitle, String thePublisher, int theCopyrightYear, int theLoanPeriod, String theCallNumber, String theAuthor) { // create the new boook LibraryBook newBook = new LibraryBook(theTitle, thePublisher, theCopyrightYear, theLoanPeriod, theCallNumber, theAuthor); // item callnum doesn't exist if (!libraryItems.ContainsKey(theCallNumber)) { // add the book to dict libraryItems.Add(theCallNumber, newBook); _items.Add(new LibraryBook(theTitle, thePublisher, theCopyrightYear, theLoanPeriod, theCallNumber, theAuthor)); return(SUCCESSFUL_ADD); } else { return(FAILED_ADD); } }
//Precondition: none //Postcondition: Outputs the values of the selected book into the edit book form private void bookButton_Click(object sender, EventArgs e) { LibraryBook b = _lib._patrons[BookIndex]; BookForm pf = new BookForm(); pf.BookAuthor = b.Author; pf.ItemCopyrightYear = b.CopyrightYear; pf.ItemCallNumber = b.CallNumber; pf.ItemLoanPeriod = b.LoanPeriod; pf.ItemPublisher = b.Publisher; pf.ItemTitle = b.Title; DialogResult result = pf.ShowDialog(); //Precondition: The result is valid //Postcondition: Outputs the values of the selected book into the edit book form if (result == DialogResult.OK) { b.Author = pf.BookAuthor; b.CopyrightYear = pf.ItemCopyrightYear; b.CallNumber = pf.ItemCallNumber; b.LoanPeriod = pf.ItemLoanPeriod; b.Publisher = pf.ItemPublisher; b.Title = pf.ItemTitle; } }
private void BookToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryItem> items; // List of items items = _lib.GetItemsList(); if (items.Count() == 0) // Must have items { MessageBox.Show("Must have books to edit!", "Edit Patron Error"); } else { SelectBookForm selectBookForm = new SelectBookForm(items); // The Select Book dialog box form load with items DialogResult result = selectBookForm.ShowDialog(); // Show form as dialog and stores result if (result == DialogResult.OK) // Only add if OK { try { BookForm bookForm = new BookForm(); // The patron dialog box form LibraryBook selected = (LibraryBook)items[selectBookForm.BookIndex]; //carryovers the selected book's properties into the new form bookForm.ItemTitle = selected.Title; // sets itemTitle property to one the user selected bookForm.ItemPublisher = selected.Publisher; // sets ItemPublisherTxt property to one the user selected bookForm.ItemCopyrightYear = selected.CopyrightYear.ToString(); // sets ItemCopyrightYearsTxt property to one the user selected bookForm.ItemLoanPeriod = selected.LoanPeriod.ToString(); // sets LoanPeriodTxt property to one the user selected bookForm.ItemCallNumber = selected.CallNumber; // sets ItemCallNumberTxt property to one user selected bookForm.BookAuthor = selected.Author; // sets bookAuthorTxt property to one user selected bookForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { selected.Title = bookForm.ItemTitle; // sets Title to input selected.Publisher = bookForm.ItemPublisher; // sets Publisher to input selected.CopyrightYear = int.Parse(bookForm.ItemCopyrightYear); // sets CopyrightYears to input selected.LoanPeriod = int.Parse(bookForm.ItemLoanPeriod); // sets Loan Period to input selected.CallNumber = bookForm.ItemCallNumber; // sets Call Number to input selected.Author = bookForm.BookAuthor; // sets Call Number to input } bookForm.Dispose(); // Good .NET practice - will get garbage collected anyway } catch (ArgumentOutOfRangeException) { MessageBox.Show("Enter Input into the Proper Field", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (InvalidOperationException) { MessageBox.Show("Invalid Operation", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (FormatException) { MessageBox.Show("Please Enter Correct Format", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } selectBookForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }
// Precondition: Edit, Book menu item activated // Postcondition: The book selected from the library has been edited // with the new information replacing the existing object's // properties private void bookToolStripMenuItem1_Click(object sender, EventArgs e) { List <LibraryItem> bookItems; // List of items that are books List <LibraryItem> items; // List of library items items = _lib.GetItemsList(); bookItems = new List <LibraryItem>(); for (int i = 0; i < items.Count(); ++i) { if (items[i] is LibraryBook) // Book? { bookItems.Add(items[i]); } } if ((bookItems.Count() == 0)) // Must have books to edit { MessageBox.Show("Must have books to edit!", "Edit Error"); } else { ReturnForm siForm = new ReturnForm(bookItems); // The select item dialog box form // Just reused ReturnForm, they are identical siForm.Text = "Edit Book"; // Change return form's title to match new purpose DialogResult result = siForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only edit if OK { int selectedIndex; // Index of selected item selectedIndex = siForm.ItemIndex; if (selectedIndex >= 0) // -1 if didn't select item from combo box (should never happen!) { LibraryBook editBook = bookItems[selectedIndex] as LibraryBook; // Book being edited //LibraryBook editBook = (LibraryBook)bookItems[selectedIndex]; // Book being edited // Need downcast to get to Author BookForm bookForm = new BookForm(); // The book dialog box form // Populate form fields from selected book bookForm.ItemTitle = editBook.Title; bookForm.ItemPublisher = editBook.Publisher; bookForm.ItemCallNumber = editBook.CallNumber; bookForm.ItemCopyrightYear = editBook.CopyrightYear.ToString("D4"); // ToString since property is String on form bookForm.ItemLoanPeriod = editBook.LoanPeriod.ToString(); // ToString since property is String on form bookForm.BookAuthor = editBook.Author; DialogResult editResult = bookForm.ShowDialog(); // Show form as dialog and store result if (editResult == DialogResult.OK) // Only update book if OK { // Edit book properties using form fields try { //Edits the item dictionary so that it isn't corrupted due to the keys changing //_lib.ChangeItemKey(bookItems[selectedIndex].CallNumber, editBook.CallNumber, editBook); editBook.Title = bookForm.ItemTitle; editBook.Publisher = bookForm.ItemPublisher; //editBook.CallNumber = bookForm.ItemCallNumber; //(Edited out to prevent corruption of //dictionary editBook.Author = bookForm.BookAuthor; editBook.CopyrightYear = int.Parse(bookForm.ItemCopyrightYear); editBook.LoanPeriod = int.Parse(bookForm.ItemLoanPeriod); } catch (FormatException) // This should never happen if form validation works! { MessageBox.Show("Problem with Book Validation!", "Validation Error"); } } bookForm.Dispose(); // Good .NET practice - will get garbage collected anyway } } siForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }