Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. /* Parent Class */
  2. class Media {
  3. constructor(title) {
  4. this._title = title;
  5. this._isCheckedOut = false;
  6. this._ratings = [];
  7. }
  8.  
  9. get title() {
  10. return this._title;
  11. }
  12.  
  13. get isCheckedOut() {
  14. return this._isCheckOut;
  15. }
  16.  
  17. get ratings() {
  18. return this._ratings;
  19. }
  20.  
  21. set isCheckedOut(checkedOut) {
  22. this._isCheckOut = checkedOut;
  23. }
  24.  
  25.  
  26. toggleCheckedOutStatus() {
  27. this._isCheckedOut = !this._isCheckedOut;
  28. }
  29.  
  30.  
  31. getAverageRating() {
  32.  
  33. let ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
  34. let ratingsLength = this._ratings.length;
  35.  
  36. let average = ratingsSum/ratingsLength;
  37.  
  38. this._ratings = average;
  39.  
  40. return this._ratings;
  41.  
  42. }
  43.  
  44. addRating(ratings) {
  45. if(ratings < 1 || ratings > 5 ) {
  46. console.log('Error! Please rate between 1 and 5')
  47.  
  48. } else {
  49. this._ratings.push(ratings);
  50. }
  51.  
  52. }
  53.  
  54. }
  55.  
  56. class Book extends Media{
  57. constructor(author, title, pages) {
  58. super(title);
  59. this._author = author;
  60. this._pages = pages;
  61. }
  62.  
  63. get author() {
  64. return this._author;
  65. }
  66.  
  67. get pages() {
  68. return this._pages;
  69. }
  70. }
  71.  
  72. class Movie extends Media {
  73. constructor(director, title, runTime, movieCast) {
  74. super(title);
  75. this._director = director;
  76. this._runTime = runTime;
  77. this._movieCast = movieCast;
  78. }
  79.  
  80. get director() {
  81. return this._director;
  82. }
  83.  
  84. get runTime() {
  85. return this._runTime;
  86. }
  87. }
  88.  
  89. class Cd extends Media {
  90. constructor(title, artist, songs) {
  91. super(title);
  92. this._artist = artist;
  93. this._songs = [];
  94. }
  95. get artist() {
  96. return this._artist;
  97. }
  98.  
  99. get songs() {
  100. return this._songs;
  101. }
  102.  
  103. addSong(songs) {
  104. this._songs.push(songs);
  105. }
  106.  
  107. shuffle(songs) {
  108. for (var i = songs.length - 1; i > 0; i--) {
  109. var j = Math.floor(Math.random() * (i + 1));
  110. var temp = songs[i];
  111. songs[i] = songs[j];
  112. songs[j] = temp;
  113. }
  114. return songs;
  115. }
  116. }
  117.  
  118. class Catalog {
  119. constructor(mediaList){
  120. this._MyCatalog = [mediaList];
  121. }
  122.  
  123. get mediaList() {
  124. return this._mediaList;
  125. }
  126.  
  127. set mediaList(newMedialist) {
  128. this._MyCatalog.push(newMedialist);
  129. }
  130. }
  131.  
  132.  
  133. const historyOfEverything = new Book('Bill Bryson', 'A Short History of Nearly Everything', 544);
  134.  
  135. //console.log(historyOfEverything);
  136. historyOfEverything.toggleCheckedOutStatus();
  137. //console.log(historyOfEverything._isCheckedOut);
  138. historyOfEverything.addRating(4);
  139. historyOfEverything.addRating(5);
  140. historyOfEverything.addRating(5);
  141. //console.log(historyOfEverything.getAverageRating());
  142.  
  143. const tgg = new Book('F. Scott Fitzgerald', 'The Great Gatsby', 192);
  144.  
  145. const avengers = new Movie('Joss Whedon', 'Avengers', 143);
  146.  
  147.  
  148. //Console logs for the movie
  149. //console.log(avengers);
  150. avengers.toggleCheckedOutStatus();
  151. console.log(avengers._isCheckedOut);
  152. avengers.addRating(4);
  153. avengers.addRating(4);
  154. avengers.addRating(5);
  155. console.log(avengers.getAverageRating());
  156.  
  157. // Outputs to console if the book is checked out in a fancy way
  158. if (avengers._isCheckedOut === 'true') {
  159. console.log("The book is checked out")
  160. } if (avengers._isCheckedOut === 'false') {
  161. console.log("This book is avaliable")
  162. } else
  163. console.log("ERROR");
  164.  
  165. const newCd = new Cd('Ocean', 'Kasaija Akiiki');
  166.  
  167.  
  168. newCd.addSong('Billy Jean');
  169. newCd.addSong('Mafia');
  170. newCd.addSong('Yeke Yeke');
  171.  
  172. //-----HERE IS THE CONSOLE.LOG FOR THE CD'S
  173.  
  174. //console.log(newCd);
  175.  
  176.  
  177. const Test = new Catalog();
  178. Test.mediaList = historyOfEverything;
  179. Test.mediaList = avengers;
  180. Test.mediaList = tgg;
  181. Test.mediaList = newCd;
  182. //------HERE IS THE CONSOLE.LOG FOR THE OUTPUT
  183. //console.log(Test)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement