// Precondition: Item, Return menu item activated // Postcondition: The Return dialog box is displayed. If data entered // are OK, an item is returned to the library private void returnToolStripMenuItem_Click(object sender, EventArgs e) { // Extra Credit - Only display items that are already checked out List <LibraryItem> checkedOutList; // List of items checked out List <int> checkedOutIndices; // List of index values of items checked out List <LibraryItem> items; // List of library items items = _lib.GetItemsList(); checkedOutList = new List <LibraryItem>(); checkedOutIndices = new List <int>(); for (int i = 0; i < items.Count(); ++i) { if (items[i].IsCheckedOut()) // Checked out { checkedOutList.Add(items[i]); checkedOutIndices.Add(i); } } if ((checkedOutList.Count() == 0)) // Must have checked out items { MessageBox.Show("Must have items to return!", "Return Error"); } else { ReturnForm returnForm = new ReturnForm(checkedOutList); // The return dialog box form DialogResult result = returnForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { try { int itemIndex; // Index of item from full list of items itemIndex = checkedOutIndices[returnForm.ItemIndex]; // Look up index from full list decimal lateFee = _lib.ReturnToShelf(_lib.GetItemsList()[itemIndex].CallNumber); //Changed to //correspond with the new ReturnToShelf parameters; also assigns the return value to a decimal so //that it can be displayed in a MessageBox MessageBox.Show($"Late Fee: {lateFee:C}"); } catch (ArgumentOutOfRangeException) // This should never happen { MessageBox.Show("Problem with Return Index!", "Return Error"); } } returnForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }
// Precondition: Item, Return menu item activated // Postcondition: The Return dialog box is displayed. If data entered // are OK, an item is returned to the library private void returnToolStripMenuItem_Click(object sender, EventArgs e) { List <LibraryItem> items; // List of library items items = _lib.GetItemsList(); if ((_lib.GetCheckedOutCount() == 0)) // Must have items to return { MessageBox.Show("Must have items to return!", "Return Error"); } else { ReturnForm returnForm = new ReturnForm(items); // The return dialog box form DialogResult result = returnForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { _lib.ReturnToShelf(returnForm.ItemIndex); } returnForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }
// Precondition: Item, Return menu item activated // Postcondition: The Return dialog box is displayed. If data entered // are OK, an item is returned to the library private void returnToolStripMenuItem_Click(object sender, EventArgs e) { // Extra Credit - Only display items that are already checked out List<LibraryItem> checkedOutList; // List of items checked out List<int> checkedOutIndices; // List of index values of items checked out List<LibraryItem> items; // List of library items List<LibraryPatron> patrons; // List of patrons items = lib.GetItemsList(); patrons = lib.GetPatronsList(); checkedOutList = new List<LibraryItem>(); checkedOutIndices = new List<int>(); for (int i = 0; i < items.Count(); ++i) if (items[i].IsCheckedOut()) // Checked out { checkedOutList.Add(items[i]); checkedOutIndices.Add(i); } if ((checkedOutList.Count() == 0)) // Must have checked out items MessageBox.Show("Must have checked out items to return!", "Return Error"); else { ReturnForm returnForm = new ReturnForm(checkedOutList); // The return dialog box form DialogResult result = returnForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { try { int itemIndex; // Index of item from full list of items itemIndex = checkedOutIndices[returnForm.ItemIndex]; // Look up index from full list lib.ReturnToShelf(itemIndex); } catch (ArgumentOutOfRangeException) // This should never happen { MessageBox.Show("Problem with Return Index!", "Return Error"); } } returnForm.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 } }
// Precondition: Item, Return menu item activated // Postcondition: The Return dialog box is displayed. If data entered // are OK, an item is returned to the library private void returnToolStripMenuItem_Click(object sender, EventArgs e) { List<LibraryItem> items; // List of library items List<LibraryPatron> patrons; // List of patrons items = lib.GetItemsList(); patrons = lib.GetPatronsList(); if ((items.Count() == 0) || (patrons.Count() == 0)) // Must have items and patrons MessageBox.Show("Must have items and patrons to return!", "Return Error"); else { ReturnForm returnForm = new ReturnForm(items); // The return dialog box form DialogResult result = returnForm.ShowDialog(); // Show form as dialog and store result if (result == DialogResult.OK) // Only add if OK { lib.ReturnToShelf(returnForm.ItemIndex); } returnForm.Dispose(); // Good .NET practice - will get garbage collected anyway } }