Project: Build a Library, step 25, how to "create a class called Catalog"?


#1

Hello everyone,
Can someone help me with implementing this feature from the “Build a Library” project, step #25:

“Create a class called Catalog that holds all of the Media items in our library.”

I am completely at loss here, not even the slightly idea :scream_cat:
A corresponding article @MDN or a hint would be very helpful,
Thank you :+1:

This is my current implementation:

class Media {
  constructor(title) {
    this._title = title;
    this._isCheckedOut = false;
    this._ratings = [];
	}
 	get title() {
    return this._title;
  }
  get isCheckedOut() {
    return this._isCheckOut;
  }
  get ratings() {
    return this._ratings;
  }
  set isCheckOut(checked) {
		this._isCheckedOut = checked;
  }
  toggleCheckOutStatus() {
    this._isCheckedOut = !this._isCheckedOut;
  }
  addRating(rating) {
    if(rating > 5) {
      console.log('Rating between 1 and 5');
      return `Rating between 1 and 5`;
    }
    this._ratings.push(rating);
  }
  getAverageRating() {
    const ratings = this._ratings;
    const lengthOfArray = ratings.length;
        if (ratings.length === 0) {
          console.log('no ratings here!');
          return `no ratings here!`
        };
		const reducer = (accumulator, currentValue) => accumulator + currentValue;
    const average = ratings.reduce(reducer) / lengthOfArray;
    return `${this._title}'s average rating: ${Math.floor(average)}/5 - ${lengthOfArray} ratings`;
  }
}

class Book extends Media {
  constructor(title, author, pages) {
    super(title);
 		this._author = author;
    this._pages = pages;   
  }
  get author() {
    return this._author;
  }
  get pages() {
    return this._pages;
  }
}

class Movie extends Media {
  constructor(title, director, runTime) {
    super(title);
    this._director = director;
    this._runTime = runTime;  
  }
  get director() {
    return this._director;
  }
  get runTime() {
    return this._runTime;
  }
}

class Cd extends Media {
  constructor(title, artist, songs) {
    super(title);
    this._artist = artist;
  	this._songs = songs;
  }
  get artist() {
    return this._artist;
  }
  get songs() {
    return this._songs;
  }
  shuffle() {
    const a = this._songs;   
    for (let i = a.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [a[i], a[j]] = [a[j], a[i]];
    }
    return a;
  }
}

#2

Build a Library


#3

Thank @mtf, I’ve edited my post by adding the link you submitted.
Any idea on how to produce a class containing objects created by other classes ?


#4

you have done this before, using extends, how come you are struggling now?


#5

Oh, so is this simply about creating on top of Media a new root/parent class called Catalog, then extending Media out of Catalog ?

From the way I read the instructions, I am expecting Catalog to be some sort of object filled with all the instances (books, cds, movies) created in the previous steps, an object that I could console.log to display an actual catalog to the console.


#6

First time they use Media items in the instructions, how nice. So the question is, what is items referencing to?

The catalog seems to be designed to hold all the instances indeed, @mtf what do you think?


#7

Haven’t really given this a lot of thought, but one would expect the Catalog class to be a standalone companion to the media library. With each new media item instance, a parallel instance is created in the catalog referencing title, media, and other pertinent cataloging data such as subject, catalog number, when ascessioned, when discarded, and so on.


#8

/* Parent Class */
class Media {
constructor(title) {
this._title = title;
this._isCheckedOut = false;
this._ratings = [];
}

get title() {
return this._title;
}

get isCheckedOut() {
return this._isCheckOut;
}

get ratings() {
return this._ratings;
}

set isCheckedOut(checkedOut) {
this._isCheckOut = checkedOut;
}

toggleCheckedOutStatus() {
this._isCheckedOut = !this._isCheckedOut;
}

getAverageRating() {

let  ratingsSum = this._ratings.reduce((currentSum, rating) => currentSum + rating, 0);
let  ratingsLength = this._ratings.length;

let  average = ratingsSum/ratingsLength;

this._ratings = average;

return this._ratings;

}

addRating(ratings) {
if(ratings < 1 || ratings > 5 ) {
console.log(‘Error! Please rate between 1 and 5’)

} else { 
   this._ratings.push(ratings); 

}

}

}

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, movieCast) {
super(title);
this._director = director;
this._runTime = runTime;
this._movieCast = movieCast;
}

get director() {
return this._director;
}

get runTime() {
return this._runTime;
}
}

class Cd extends Media {
constructor(title, artist, songs) {
super(title);
this._artist = artist;
this._songs = [];
}
get artist() {
return this._artist;
}

get songs() {
return this._songs;
}

addSong(songs) {
this._songs.push(songs);
}

shuffle(songs) {
for (var i = songs.length - 1; i > 0; i–) {
var j = Math.floor(Math.random() * (i + 1));
var temp = songs[i];
songs[i] = songs[j];
songs[j] = temp;
}
return songs;
}
}

class Catalog {
constructor(mediaList){
this._MyCatalog = [mediaList];
}

get mediaList() {
return this._mediaList;
}

set mediaList(newMedialist) {
this._MyCatalog.push(newMedialist);
}
}

const historyOfEverything = new Book(‘Bill Bryson’, ‘A Short History of Nearly Everything’, 544);

console.log(historyOfEverything);
historyOfEverything.toggleCheckedOutStatus();
console.log(historyOfEverything._isCheckedOut);
historyOfEverything.addRating(4);
historyOfEverything.addRating(5);
historyOfEverything.addRating(5);
console.log(historyOfEverything.getAverageRating());

const hobbit = new Book(‘J.R. Tolkien’, ‘Hobbit’, 744);

const speed = new Movie(‘Jan de Bont’, ‘Speed’, 116);

console.log(speed);
speed.toggleCheckedOutStatus();
console.log(speed._isCheckedOut);
speed.addRating(1);
speed.addRating(1);
speed.addRating(5);
console.log(speed.getAverageRating());

const newCd = new Cd(‘Ocean’, ‘Kasaija Akiiki’);

newCd.addSong(‘Billy Jean’);
newCd.addSong(‘Mafia’);
newCd.addSong(‘Yeke Yeke’);

console.log(newCd);

const Test = new Catalog();
Test.mediaList = historyOfEverything;
Test.mediaList = speed;
Test.mediaList = hobbit;
Test.mediaList = newCd;
console.log(Test)