Created
September 2, 2019 13:42
-
-
Save debonx/4bfa8cd22b385e417adaf295caece499 to your computer and use it in GitHub Desktop.
A javascritpt based class to catalog Media objects.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Media { | |
constructor(title) { | |
this._title = title; | |
this._isCheckedOut = false; | |
this._ratings = []; | |
} | |
get title(){ | |
return this._title; | |
} | |
get isCheckedOut(){ | |
return this._isCheckedOut; | |
} | |
get ratings(){ | |
return this._ratings; | |
} | |
set isCheckedOut(checked){ | |
this._isCheckedOut = checked; | |
} | |
toggleCheckOutStatus(){ | |
this.isCheckedOut = this.isCheckedOut !== false ? false : true; | |
} | |
getAverageRating(){ | |
let rating = this.ratings.reduce((currentSum, rating) => currentSum + rating, 0); | |
return rating / this.ratings.length; | |
} | |
addRating(rating){ | |
if(typeof rating == 'number' && rating >= 1 && rating <= 5){ | |
this._ratings.push(rating); | |
} else { | |
console.log('It\'s not a valid rating'); | |
} | |
} | |
} | |
class Book extends Media { | |
constructor(author, title, pages){ | |
super(title); | |
this._author = author; | |
this._pages = pages; | |
} | |
get author(){ | |
return this._author; | |
} | |
get pages(){ | |
return this._pages; | |
} | |
} | |
class Movie extends Media { | |
constructor(director, title, runTime){ | |
super(title); | |
this._director = director; | |
this._runTime = runTime; | |
} | |
get director(){ | |
return this._director; | |
} | |
get runTime(){ | |
return this._runTime; | |
} | |
} | |
const historyOffEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544); | |
const speed = new Movie('Jan de Bont', 'Speed', 116); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment