115 lines
No EOL
2.6 KiB
Text
115 lines
No EOL
2.6 KiB
Text
public class Media
|
|
{
|
|
public string Title { get; private set; }
|
|
public int Year { get; private set; }
|
|
|
|
public Media(string title, int year)
|
|
{
|
|
Title = title;
|
|
Year = year;
|
|
}
|
|
|
|
public virtual void PrintDetails()
|
|
{
|
|
Console.WriteLine($"MEDIA: {Title}, {Year}");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public class Movie : Media, IComparable<Movie>
|
|
{
|
|
public string ParentalRating { get; private set; }
|
|
public int Rating { get; private set; } // derived from imdbRating * 10
|
|
|
|
public Movie(string title, int year, string parentalRating, double imdbRating) : base(title, year)
|
|
{
|
|
ParentalRating = parentalRating;
|
|
Rating = (int)(imdbRating * 10);
|
|
}
|
|
|
|
public override bool Equals(object obj)
|
|
{
|
|
if (obj == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (obj is Movie other)
|
|
{
|
|
return this.Title == other.Title &&
|
|
this.Year == other.Year &&
|
|
this.ParentalRating == other.ParentalRating &&
|
|
this.Rating == other.Rating;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public int CompareTo(Movie other)
|
|
{
|
|
if (other == null)
|
|
{
|
|
return 1;
|
|
}
|
|
|
|
if(this.Rating > other.Rating)
|
|
{
|
|
return 1;
|
|
}
|
|
else if(this.Rating < other.Rating)
|
|
{
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
public override int GetHashCode()
|
|
{
|
|
int hash = 17;
|
|
foreach (char c in Title)
|
|
{
|
|
hash = hash * 31 + c;
|
|
}
|
|
hash = hash * 31 + Year;
|
|
foreach (char c in ParentalRating)
|
|
{
|
|
hash = hash * 31 + c;
|
|
}
|
|
hash = hash * 31 + Rating;
|
|
return (Math.Abs(hash) % 1000) / 100;
|
|
}
|
|
|
|
public override void PrintDetails()
|
|
{
|
|
Console.WriteLine($"Movie: {base.Title}, {base.Year}, Parental Rating = {ParentalRating}, IMDB Rating = {(double)Rating / 10}");
|
|
}
|
|
|
|
public override string ToString()
|
|
{
|
|
return $"Movie: {base.Title}, {base.Year}, Parental Rating = {ParentalRating}, IMDB Rating = {(double)Rating / 10}";
|
|
}
|
|
}
|
|
|
|
public class Program
|
|
{
|
|
public static void Main(string[] args)
|
|
{
|
|
Media media1 = new Media("Star Wars", 1977);
|
|
|
|
Media media2 = new Movie("Drive", 2011, "R", 7.9);
|
|
|
|
Movie movie1 = new Movie("Dune", 2021, "R", 8.1);
|
|
|
|
media1.PrintDetails();
|
|
|
|
media2.PrintDetails();
|
|
|
|
movie1.PrintDetails();
|
|
}
|
|
}
|
|
|
|
OUTPUT:
|
|
MEDIA: Star Wars, 1977
|
|
Movie: Drive, 2011, Parental Rating = R, IMDB Rating = 7.9
|
|
Movie: Dune, 2021, Parental Rating = R, IMDB Rating = 8.1 |