Tuesday, November 25, 2014

Book Dictionary _ Lab


namespace Dictionary_Lab
{

    public class Book
    {
        private string _title;
        public string Title
        {get { return _title; }}

        private string _author;
        public string Author
        {get { return _author; }}

        private string _isbn;
        public string ISBN
        {get { return _isbn; }}

        private decimal _price;
        public decimal Price
        {get { return _price; }}

        public Book(string title, string author, string isbn, decimal price)
        {
            _title = title;
            _author = author;
            _isbn = isbn;
            _price = price;
        }
    }
}

//======================================================================
namespace Dictionary_Lab
{
    public partial class Form1 : Form
    {
        Dictionary<string, Book> bookDictionary = new Dictionary<string,Book>(20);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            PopulateDictionary();
            DisplayAllBooks();
            LoadComboBoxWithAllBooks();
        }

        private void PopulateDictionary()
        {
            Book b = new Book("The Count of Monte Cristo", "Alexander Dumas", "1983726184920", 3.99m);
            bookDictionary.Add(b.ISBN, b);
           
            b = new Book("The Art of War", "Sun Tzu", "9983726184920", 4.99m);
            bookDictionary.Add(b.ISBN, b);
           
            b = new Book("The Republic", "Plato", "1183726184920", 2.99m);
            bookDictionary.Add(b.ISBN, b);

            b = new Book("The Bible", "Various", "9981126184920", 5.99m);
            bookDictionary.Add(b.ISBN, b);

            b = new Book("The Prince", "Niccolò Machiavelli", "9983726141220", 3.99m);
            bookDictionary.Add(b.ISBN, b);

            b = new Book("The Wealth of Nations", "Adam Smith", "9983726184911", 4.99m);
            bookDictionary.Add(b.ISBN, b);

            b = new Book("The Rights of Man", "Thomas Paine", "1133726184911", 2.99m);
            bookDictionary.Add(b.ISBN, b);

            b = new Book("A Vindication of the Rights of Women", "Mary Wollstonecraft", "9122726114911", 4.99m);
            bookDictionary.Add(b.ISBN, b);

            b = new Book("The Communist Manifesto", "Karl Marx & Friedrich Engles", "1234567893584", 1.99m);
            bookDictionary.Add(b.ISBN, b);

            b = new Book("The Origin of Species", "Charles Darwin", "9584628716458", 8.99m);
            bookDictionary.Add(b.ISBN, b);
        }

        private void btnDisplayAllBooks_Click(object sender, EventArgs e)
        {
            DisplayAllBooks();
        }

        private void DisplayAllBooks()
        {
            lbxDisplay.Items.Clear();
            foreach (KeyValuePair<string, Book> book in bookDictionary)
            {
                lbxDisplay.Items.Add(String.Format("{0} {1} {2} {3}", book.Value.Title.PadRight(45), book.Value.Author.PadRight(35), book.Value.Price.ToString("C").PadRight(10), book.Value.ISBN.PadRight(15)));
            }
        }

        private void btnAddBook_Click(object sender, EventArgs e)
        {
            // read title, author, price, isbn from user
            string title = txtTitle.Text;
            string author = txtAuthor.Text;
            string ISBN = txtISBN.Text;
            decimal price;
            if (title != string.Empty && author != string.Empty && ISBN != string.Empty)
            {
                if (decimal.TryParse(txtPrice.Text, out price))
                {
                    // create a book
                    Book b = new Book(title, author, ISBN, price);
                    // add the book to the dictionary
                    bookDictionary.Add(b.ISBN, b);
                    // Display new book and update combo box
                    DisplaySingleBook(b);
                    LoadComboBoxWithAllBooks();
                }
            }
           
           
           
        }

        private void btnRemoveBook_Click(object sender, EventArgs e)
        {
            // look at the combo box, find which book is selected
            // get the ISBN from that book
            if (cboFindBook.SelectedIndex > 0)
            {
                string ISBN = cboFindBook.SelectedItem.ToString().Substring(0, 13);
                // remove the book with that ISBN from the dictionary
                bookDictionary.Remove(ISBN);
                // redisplay all books
                DisplayAllBooks();
                // Update Combo Box
                LoadComboBoxWithAllBooks();
            }          
        }

        private void LoadComboBoxWithAllBooks()
        {
            cboFindBook.Items.Clear();
            cboFindBook.Items.Add("Select a book from this list to view it alone, or delete it from the dictionary");
            Dictionary<string, Book>.KeyCollection kc = bookDictionary.Keys;
            foreach (KeyValuePair<string, Book> item in bookDictionary)
            {
                cboFindBook.Items.Add(item.Key.PadRight(15) + " " + item.Value.Title.PadRight(40) + " " + item.Value.Author);
            }
            // Workaround for double clicking remove - Would like to set the first item in teh combo box to an instruction
            // message then reselect that instruction so the user knows what to do next and doesn't get confused by selecting a new book.
            cboFindBook.SelectedIndex = 0;
        }

        private void cboFindBook_SelectedIndexChanged(object sender, EventArgs e)
        {
            // get the ISBN for the selected book
            string ISBN = cboFindBook.SelectedItem.ToString().Substring(0, 13);
            // use the ISBN to find that book in the dictionary
            Book selectedBook;
            if (bookDictionary.TryGetValue(ISBN, out selectedBook))
            {
                DisplaySingleBook(selectedBook);
            }
            // display that book - ONLY - in the listbox
        }

        private void DisplaySingleBook(Book book)
        {
            lbxDisplay.Items.Clear();
            lbxDisplay.Items.Add(String.Format("{0} {1} {2} {3}", book.Title.PadRight(45), book.Author.PadRight(35), book.Price.ToString("C").PadRight(10), book.ISBN.PadRight(15)));
        }
    }
}

// Read about Dictionaries - hash tables, collisions (how to solve)
/// LAB:
/// New Project:
/// Create a dictionary to hold books
/// Define a Book class: _title, _author, _isbn, _price
/// in Form_Load prepopulate the dictionary with 10+ books
/// Add functionality to:
///         Add a new book to the dictionary
///         Display all books in the dictionary
///         Search for a book in the dictionary
///         Remove a book from the dictionary

No comments:

Post a Comment