public MovieDictionary() { //creating dictionary and adding some initial data to it movieTable = new SortedDictionary<int, Movie>(); Movie movie1 = new Movie(2016, "Spotlight", "Tom McCarthy"); Movie movie2 = new Movie(2015, "Birdman", " Alejandro González Iñárritu"); Movie movie3 = new Movie(2014, "12 Years A Slave", "Steve McQuenn"); Movie movie4 = new Movie(2013, "Argo", "Ben Affleck"); Movie movie5 = new Movie(2012, "The Artist", "Michel Hazanavicius"); movieTable.Add(movie1.Year, movie1); movieTable.Add(movie2.Year, movie2); movieTable.Add(movie3.Year, movie3); movieTable.Add(movie4.Year, movie4); movieTable.Add(movie5.Year, movie5); }
/// <summary> /// Add a new movie to the database /// </summary> /// <param name="year">Movie year</param> /// <param name="title">Movie title</param> /// <param name="Director">Movie director</param> /// <param name="feedback">Feedback about success of adding movie</param> /// <returns>True if addition was successful</returns> public Boolean AddMovie(String year, String title, String director, out String feedback) { int key; //TODO Place return false in each check so that user only gets one error message? //Check year is valid checkYear(year, out key, out feedback); //Check that there isn't already a movie for this year! if (movieTable.ContainsKey(key)) { feedback += " A movie for " + key + " already exists."; } //Check a title has been entered if (title.Length == 0) { feedback += " Please enter a title."; } //Check a director has been entered if (director.Length == 0) { feedback += " Please enter a director."; } //Check all data is valid if (feedback != "") { return false; } //Create new movie and add to database now that we know that data is all okay Movie movie = new Movie(key, title, director); movieTable.Add(key, movie); feedback = "Movie was added successfully"; return true; }
//adds a movie to the dictionary public void addMovie(int year, string title, string director) { try { //if the year is either to high or too small it will show an error message if ((year < 2020) && (year > 1900)) { //if there is no title it will show an error message if (title != "") { //if there is no director it will show an error message if (director != "") { Movie movie = new Movie(year, title, director); movieTable.Add(movie.Year, movie); } else { MessageBox.Show("Please enter a Director"); } } else { MessageBox.Show("Please enter a Title"); } } else { MessageBox.Show("That is not a valid Year"); } } catch (ArgumentException ex) { MessageBox.Show("There is already a movie from that Year"); } }