Done.
This commit is contained in:
parent
368e2a271c
commit
5a78786532
2 changed files with 120 additions and 1 deletions
6
main.py
6
main.py
|
|
@ -7,4 +7,8 @@ media3 = Movie("Dune", 2011, "R", 81)
|
|||
|
||||
media1.print_details()
|
||||
media2.print_details()
|
||||
media3.print_details()
|
||||
media3.print_details()
|
||||
|
||||
comparison = media2.compare_to(media3)
|
||||
|
||||
print(comparison)
|
||||
115
txt.txt
Normal file
115
txt.txt
Normal file
|
|
@ -0,0 +1,115 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue