Example #1
0
        private void bookToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            List <LibraryItem> items;         // List of Items



            items = _lib.GetItemsList(); // gets list of items from library



            EditBookForm editBookForm     = new EditBookForm(items); // pass items to the form
            BookForm     editSelectedBook = new BookForm();          // create new book form

            DialogResult result = editBookForm.ShowDialog();         // Show form as dialog and store result

            // ensure user clicked OK
            if (result == DialogResult.OK)
            {
                try
                {
                    int bookCopyrightYear; // holds copyright year
                    int bookLoanPeriod;    // holds load period


                    LibraryBook selectedItem = (LibraryBook)items[editBookForm.ItemIndex];       // stores selected book

                    editSelectedBook.ItemTitle         = selectedItem.Title;
                    editSelectedBook.ItemPublisher     = selectedItem.Publisher;
                    editSelectedBook.ItemCopyrightYear = selectedItem.CopyrightYear.ToString();
                    editSelectedBook.ItemLoanPeriod    = selectedItem.LoanPeriod.ToString();
                    editSelectedBook.ItemCallNumber    = selectedItem.CallNumber;
                    editSelectedBook.BookAuthor        = selectedItem.Author;

                    int.TryParse(editSelectedBook.ItemCopyrightYear, out bookCopyrightYear);
                    int.TryParse(editSelectedBook.ItemLoanPeriod, out bookLoanPeriod);

                    result = editSelectedBook.ShowDialog();       // show form as dialog for selected book

                    _lib._items.RemoveAt(editBookForm.ItemIndex); // the book being edited is removed


                    LibraryBook editedBook = new LibraryBook(editSelectedBook.ItemTitle, editSelectedBook.ItemPublisher, bookCopyrightYear, bookLoanPeriod, editSelectedBook.ItemCallNumber, editSelectedBook.BookAuthor);

                    _lib._items.Insert(editBookForm.ItemIndex, editedBook); // the edited version of the book is added
                }
                catch (InvalidCastException)
                {
                    MessageBox.Show("Select a Book Item!"); // error if selected item is not a book
                }
                catch (ArgumentOutOfRangeException)
                {
                    MessageBox.Show("Select a Book!");  // error if no book selected
                }
            }


            editBookForm.Dispose(); // Good .NET practice - will get garbage collected anyway
        }
Example #2
0
        //Preconditions: user clicks edit then clicks book. user must select an item from the combo box
        //Postconditions: dialog box shows up with the books current information loaded into it. user can then change the info
        private void bookToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            List <LibraryItem> items        = lib.GetItemsList();       //reference library items
            List <LibraryItem> bookList     = new List <LibraryItem>(); //create new list of only library books
            List <int>         bookIndecies = new List <int>();         //creates index of which book is stored in the index in the items list

            for (int i = 0; i < items.Count; ++i)
            {
                if (items[i] is LibraryBook)
                {
                    bookList.Add(items[i]);
                    bookIndecies.Add(i);
                }
            }

            if ((bookList.Count() == 0))
            {
                MessageBox.Show("Must have books in the book list");
            }
            else
            {
                EditBookForm editBookForm = new EditBookForm(bookList);
                DialogResult result1      = editBookForm.ShowDialog();

                if (result1 == DialogResult.OK)
                {
                    int itemIndex;                                //to hold index

                    itemIndex = bookIndecies[editBookForm.ItemIndex];
                    BookForm bookForm = new BookForm();

                    LibraryBook books = (LibraryBook)items[itemIndex];   //downcast

                    bookForm.ItemTitle         = items[itemIndex].Title;
                    bookForm.ItemPublisher     = items[itemIndex].Publisher;
                    bookForm.ItemCopyrightYear = items[itemIndex].CopyrightYear.ToString();
                    bookForm.ItemLoanPeriod    = items[itemIndex].LoanPeriod.ToString();
                    bookForm.ItemCallNumber    = items[itemIndex].CallNumber;
                    bookForm.BookAuthor        = books.Author; //from downcast

                    DialogResult result2 = bookForm.ShowDialog();

                    if (result2 == DialogResult.OK)
                    {
                        items[itemIndex].Title         = bookForm.ItemTitle;
                        items[itemIndex].Publisher     = bookForm.ItemPublisher;
                        items[itemIndex].CopyrightYear = int.Parse(bookForm.ItemCopyrightYear);
                        items[itemIndex].LoanPeriod    = int.Parse(bookForm.ItemLoanPeriod);
                        items[itemIndex].CallNumber    = bookForm.ItemCallNumber;
                        books.Author = bookForm.BookAuthor;  //from downcast
                    }
                }
            }
        }
Example #3
0
        // Precondition:  User clicks on the edit book control
        // Postcondition: Edited book is saved to the library.
        private void bookToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            List <LibraryBook> books = new List <LibraryBook>(); // List of books


            // Walk through items to find books
            for (int i = 0; i < _lib.GetItemsList().Count(); i++)
            {
                //Is the item a book?
                if (_lib.GetItemsList().ElementAt(i) is LibraryBook)
                {
                    // Cast and add to list
                    books.Add((LibraryBook)_lib.GetItemsList().ElementAt(i));
                }
            }

            EditBookForm editBook = new EditBookForm(books); // Create edit book form

            DialogResult result = editBook.ShowDialog();     // Show form as dialog and store result

            if (result == DialogResult.OK)
            {
                LibraryItem selectedBook = _lib.GetItemsList().ElementAt(editBook.BookIndex); // the book to be updated

                // walk thru library items list
                foreach (LibraryItem i in _lib.GetItemsList())
                {
                    // condition to find the selected book
                    if (i == selectedBook)
                    {
                        LibraryItem oldbook = _lib.GetItemsList().ElementAt(_lib._items.IndexOf(selectedBook)); // Old book
                        LibraryBook newi    = (LibraryBook)i;                                                   // New book

                        newi.Title         = editBook.ItemTitle;                                                // set new book title
                        newi.Publisher     = editBook.ItemPublisher;                                            // set new book publisher
                        newi.CopyrightYear = int.Parse(editBook.ItemCopyrightYear);                             // set new book copyright year
                        newi.LoanPeriod    = int.Parse(editBook.ItemLoanPeriod);                                // set new book loan period
                        newi.CallNumber    = editBook.ItemCallNumber;                                           // set new book call number
                        newi.Author        = editBook.Author.ToString();                                        // set new book author

                        // Puts the book into item format
                        LibraryItem castednewi = (LibraryItem)newi;

                        // Updates the book
                        oldbook = newi;
                    }
                }
            }
        }