Object-oriented programming (OOP) is a powerful way to organize code by grouping related data and behavior into classes and objects. In this activity, you will create a simple “Movie Watch List” application. This project will reinforce your understanding of classes, objects, lists, and methods, providing a practical example of how to manage and manipulate data in a real-world scenario.
In this activity, you will:
Movie.cs
public class Movie
{
public string Title;
public int Year;
public string Genre;
public Movie(string title, int year, string genre)
{
Title = title;
Year = year;
Genre = genre;
}
public void GetDetails()
{
Console.WriteLine($"{Title} - {Year} - {Genre}");
}
}
Program.cs
List<Movie> movies = [];
Console.WriteLine("--- Movie Watch List ---");
string choice = "";
while (choice != "4")
{
Console.WriteLine("\n1) Add movie to watch list");
Console.WriteLine("2) Display watch list");
Console.WriteLine("3) Remove movie from watch list");
Console.WriteLine("4) Exit");
choice = Console.ReadLine();
if (choice == "1")
{
AddMovie();
}
else if (choice == "2")
{
DisplayMovies();
}
else if (choice == "3")
{
RemoveMovie();
}
else if (choice == "4")
{
Console.WriteLine("Exiting application.");
}
else
{
Console.WriteLine("Invalid option.");
}
}
void AddMovie()
{
Console.WriteLine("Adding new movie to watch list");
Console.Write("Title: ");
string title = Console.ReadLine();
Console.Write("Release Year: ");
int year = Convert.ToInt32(Console.ReadLine());
Console.Write("Genre: ");
string genre = Console.ReadLine();
Movie movie = new Movie(title, year, genre);
movies.Add(movie);
}
void DisplayMovies()
{
Console.WriteLine("- Watch List -");
Console.WriteLine("Title - Year - Genre");
Console.WriteLine("--------------------");
// For each movie in list
foreach (Movie movie in movies)
{
movie.GetDetails();
}
}
void RemoveMovie()
{
Console.WriteLine("- Removing movie from watch list -");
Console.Write("Enter title: ");
string title = Console.ReadLine();
// Find movie by title and remove it if the title matches
foreach (Movie movie in movies)
{
if (movie.Title == title)
{
movies.Remove(movie);
Console.WriteLine($"{title} was removed from watch list.");
break; // Exit the loop immediately after removing the movie
}
}
}
--- Movie Watch List ---
1) Add movie to watch list
2) Display watch list
3) Remove movie from watch list
4) Exit
1
Adding new movie to watch list
Title: Iron Man
Release Year: 2008
Genre: Action
1) Add movie to watch list
2) Display watch list
3) Remove movie from watch list
4) Exit
1
Adding new movie to watch list
Title: Big Daddy
Release Year: 1999
Genre: Comedy
1) Add movie to watch list
2) Display watch list
3) Remove movie from watch list
4) Exit
2
- Watch List -
Title - Year - Genre
--------------------
Iron Man - 2008 - Action
Big Daddy - 1999 - Comedy
1) Add movie to watch list
2) Display watch list
3) Remove movie from watch list
4) Exit
3
- Removing movie from watch list -
Enter title: Iron Man
Iron Man was removed from watch list.
1) Add movie to watch list
2) Display watch list
3) Remove movie from watch list
4) Exit
4
Exiting application.