public void AddMovieToDB(Movie movie) { int minYear = 1000; int maxYear = 2016; if (movie.Year > minYear && movie.Year <= maxYear) { movieTable.Add(movie.Year, movie); //Arguement Exception MovieListKeys.Add(movie.Year); } else throw new System.IndexOutOfRangeException("Movie must between " + minYear + " and " + maxYear); }
public void PopulateMovieDB() { Movie MovieOne = new Movie(2014, "Night at the Museum 3", "Shawn Levy"); Movie MovieTwo = new Movie(1993, "Jurassic Park", "Steven Spielberg"); Movie MovieThree = new Movie(1994, "The Shawshank Redemption", "Frank Darabont"); Movie MovieFour = new Movie(2004, "Man On Fire", "Tony Scott"); Movie MovieFive = new Movie(2007, "American Gangster", "Ridley Scott"); Movie MovieSix = new Movie(1997, "Titanic", "James Cameron" ); Movie MovieSeven = new Movie(2015, "Legend", "Brian Helgeland"); AddMovieToDB(MovieOne); AddMovieToDB(MovieTwo); AddMovieToDB(MovieThree); AddMovieToDB(MovieFour); AddMovieToDB(MovieFive); AddMovieToDB(MovieSix); AddMovieToDB(MovieSeven); }
private void btnAddMovie_Click(object sender, EventArgs e) { string movieTitle; string movieDirector; int movieYear; if (tbAddTitle.Text != "" && tbAddDirector.Text != "") //If these two text boxes aren't empty { movieTitle = tbAddTitle.Text; movieDirector = tbAddDirector.Text; bool result = Int32.TryParse(tbAddYear.Text, out movieYear); //use TryParse so that we don't need to catch an exception if (result) { Movie movieToAdd = new Movie(movieYear, movieTitle, movieDirector); try { movieDB.AddMovieToDB(movieToAdd); MessageBox.Show("Movie added successfully"); } catch (ArgumentException) //we already have a movie with that key in our DB { MessageBox.Show(movieYear + " is already taken by another movie"); } catch (IndexOutOfRangeException ex) //movie.Year below 1000 or above 2016 { MessageBox.Show(ex.Message); } finally { ClearAllInputControls(); } } else { MessageBox.Show("Movie year must be numeric"); ClearAllInputControls(); } } // end text box check else { MessageBox.Show("Please fill out all required fields"); } }