Exemplo n.º 1
0
        /// <summary>
        /// Description: Executed when the save changes button is pressed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void saveChangesBtn_Click(object sender, EventArgs e)
        {
            // If there are no items in the list box, function ends
            if (itemListBox.Items.Count == 0) return;

            // Creates ints pastItemCount and index, as number of items in list box and selected index
            int pastItemCount = itemListBox.Items.Count;
            int index = itemListBox.SelectedIndex;

            // Creates variables for new attributes of item
            string newType = (string)infoTypeComboBox.SelectedItem;
            string title = currTitleTxt.Text;
            double cost;
            string genre = infoGenreComboBox.Text;
            string platform = infoPlatformComboBox.Text;
            int releaseYear;

            // Attempts to convert user input
            // If TryParse fails, message box is shown with error
            if (!Double.TryParse(currCostTxt.Text, out cost))
            {
                MessageBox.Show("ERROR: Invalid cost.");
                return;
            }
            if (!Int32.TryParse(currReleasedTxt.Text, out releaseYear))
            {
                MessageBox.Show("ERROR: Invalid release year.");
                return;
            }

            // Switch statement for each type of item
            switch (newType)
            {
                case "Movie":
                    {
                        // Creates variables for type specific attributes
                        string director = currTypeSpecific1Txt.Text;
                        int duration;

                        // Attempts to convert duration input to integer
                        // If it fails, shows message box with error
                        if (!Int32.TryParse(currTypeSpecific2Txt.Text, out duration))
                        {
                            MessageBox.Show("ERROR: Invalid duration. (Please use whole minutes.)");
                            return;
                        }

                        // If filter is active, change is made to item at filtered index
                        // Otherwise, change is made to item at selected index
                        if (filterActive) inventory.items[filteredIndex[index]] = new Movie(newType, title, cost, genre, platform, releaseYear, director, duration);
                        else inventory.items[index] = new Movie(newType, title, cost, genre, platform, releaseYear, director, duration);
                        break;
                    }
                case "Game":
                    {
                        string developer = currTypeSpecific1Txt.Text;
                        double ignScore;

                        if (!Double.TryParse(currTypeSpecific2Txt.Text, out ignScore))
                        {
                            MessageBox.Show("ERROR: Invalid IGN score. (IGN scores are single digit decimals from 0 to 10.");
                            return;
                        }

                        if (filterActive) inventory.items[filteredIndex[index]] = new Game(newType, title, cost, genre, platform, releaseYear, developer, ignScore);
                        else inventory.items[index] = new Game(newType, title, cost, genre, platform, releaseYear, developer, ignScore);
                        break;
                    }
                case "Book":
                    {
                        string author = currTypeSpecific1Txt.Text;
                        string publisher = currTypeSpecific2Txt.Text;

                        if (filterActive) inventory.items[filteredIndex[index]] = new Book(newType, title, cost, genre, platform, releaseYear, author, publisher);
                        else inventory.items[index] = new Book(newType, title, cost, genre, platform, releaseYear, author, publisher);
                        break;
                    }
            }

            // Updates possible platforms and genres for item type
            inventory.UpdatePlatforms(newType, platform);
            inventory.UpdateGenres(newType, genre);

            // Changes item in list box according to changes
            itemListBox.Items[index] = inventory.items[index].Display();

            // If filter is active, filter is updated 
            if (filterActive)
            {
                UpdateFilter();

                // If number of items in list box has not changed, index stays the same
                // Otherwise, if items still exist in list box, index is decremented
                if (pastItemCount == itemListBox.Items.Count) itemListBox.SelectedIndex = index;
                else if (itemListBox.Items.Count > 0) itemListBox.SelectedIndex = index - 1;
            }

        }