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

Soory I still don’t understand:(
How can _mediaList be the property?
I don’t even understand how the getter works i that example?
Is there some were that I can read upp about this?

I’m thinking about Catalog class like a class of objects which are a containers of arrays of objects (books,movies and CD’s in this exercise). I dont think that Media class should inherit from Catalog class.

class Catalog {

constructor() {

this._books = [];

this._movies = [];

this._cd = [];

}

addBook(object) {

Book.prototype.isPrototypeOf(object)

  ? this._books.push(object)

  : console.log('Wrong argument');

}

addMovie(object) {

Movie.prototype.isPrototypeOf(object)

  ? this._movies.push(object)

  : console.log('Wrong argument');

}

addCD(object) {

CD.prototype.isPrototypeOf(object)

  ? this._cd.push(object)

  : console.log('Wrong argument');

}

}
const fullCatalog = new Catalog();

fullCatalog.addBook(historyOfEverything);

fullCatalog.addMovie(speed);

console.log(fullCatalog);

Here’s my solution. Only presenting additional code.

 //Make a array class -Catalog
  class Catalog {
      constructor(){
          this.data = {};
          this.length = 0;        
      }

      //declare a push method to class
      push(item) {
        this.data[this.length] = item;
        this.length++;
        return this.data;
    }
  }

//declare new array of objects from Catalog
const catalog = new Catalog();

//push media items to array
catalog.push(historyOfEverything);
catalog.push(speed);

//log array
console.log(catalog.data);

Results in an class array of objects.
Here’s a helpful link:
https://www.geeksforgeeks.org/implementation-of-array-class-in-javascript/

1 Like

Assuming I have movie instance named speed , can you explain why if I were to update the isCheckedOut property of the movie instance with speed.isCheckedOut = true, when I go to log the catalog, the property does not update accordingly when the set method is like this:

set movies(movie) {
this._movies.push({
‘title’: movie.title,
‘director’: movie.director,
‘runtime’: movie.runTime,
’isCheckedOut’: movie.isCheckedOut
});

However, if the set method is just :
set movies(movie) {
this._movies.push(movie)
}
… the property is updated accordingly?

The second example reflects how the movie is a complete object instance, and does not need to be constructed a second time, just pushed to the movies collection.

I was thinking of the catalog as a container for instances of the various media classes, too. I added some methods to add existing instances or to create new ones directly into the catalog, as well as a one that will log out a directory of everything in the catalog.

class Catalog {
  constructor() {
    this._books = [];
    this._movies = [];
    this._cds = [];
  }
  get books() {
    return this._books;
  }
  get movies() {
    return this._movies;
  }
  get cds() {
    return this._cds;
  }

  addExistingBook(bookInstance) {
    this._books.push(bookInstance);
  }
  addExistingMovie(movieInstance) {
    this._movies.push(movieInstance);
  }
  addExistingCD(cdInstance) {
    this._cds.push(cdInstance);
  }

  addNewBook(title,author,pages) {
    this._books.push(new Book(title,author,pages));
  }
  addNewMovie(title,director,runTime) {
    this._movies.push(new Movie(title,director,runTime));
  }
  addNewCD(title,artist,songs) {
    this._cds.push(new CD(title,artist,songs));
  }

  listMedia() {
    console.log('Books:');
    console.log(this.books);
    console.log('Movies:');
    console.log(this.movies);
    console.log('CDs:');
    console.log(this.cds);
  }
}
2 Likes

hey guys here is my take on that my aim was to only have three methods add ,find ,remove instead of having too many methods doing the same thing and repeating code , so after redoing it a few times i am not happy with the outcome
let me know what you think i would to get your feedback on where i can improve my code even more much appreciated !!
happy coding!!
const catalog ={

_media: [ ],

getMedia(){

return this._media;

},

addMedia(input){

this._media.push(input)

},

findMedia(title){

let media = this._media;

for(const item in media){

    if(media[item].title === title){

      console.log(media[item])

    }

}

},

deleteItem(title){

let media = this._media;

let indx = media.findIndex(function(item) {

  return item.title === title

})

  if(indx !== -1){

    media.splice(indx,1)

  }

}

}

catalog.addMedia(movie1)

catalog.addMedia(movie2)

catalog.addMedia(book1)

catalog.addMedia(book2)

catalog.addMedia(cd1)

catalog.addMedia(cd2)

catalog.deleteItem(‘hard target’)

console.log(catalog._media)