Skip to content

Instantly share code, notes, and snippets.

@cocomatt
Created February 27, 2018 21:58
Show Gist options
  • Select an option

  • Save cocomatt/6837092133747cde3a1375a9ccb85dd3 to your computer and use it in GitHub Desktop.

Select an option

Save cocomatt/6837092133747cde3a1375a9ccb85dd3 to your computer and use it in GitHub Desktop.
Music Library as OOP exercise
'use strict'
/*
A Library has a name and a creator (both strings)
These are the only things required in order to create a music library
A Library has many playlists (starts as an empty array)
Playlist objects can be added to an instance of a library
Each Playlist has a name which is required upon creation
A Playlist also has many tracks
A Track has a title, a rating (an integer from 1 to 5) and a length (integer in seconds) all of which are required when creating a new track
Each Playlist also has an overallRating function which will calculate the rating by averaging the rating of its tracks
Each Playlist also has a totalDuration function which will sum the duration of all of its tracks
*/
//A Library has a name and a creator (both strings)
class Library {
constructor(name, creator) {
this.name = name;
this.creator = creator;
//A Library has many playlists (starts as an empty array)
this.playlists = [];
}
//Playlist objects can be added to an instance of a library
addPlaylist(playlist) {
if (playlist instanceof Playlist) {
this.playlists.push(playlist)
}
}
}
//Each Playlist has a name which is required upon creation
class Playlist {
constructor (name) {
this.name = name;
//A Playlist also has many tracks
this.tracks = [];
}
addTrack(track) {
if (track instanceof Track) {
this.tracks.push(track);
}
}
//Each Playlist also has an overallRating function which will calculate the rating by averaging the rating of its tracks
overallRating() {
let sum = 0;
this.tracks.forEach((element) => {
sum += element.rating;
})
return sum / this.tracks.length
}
//Each Playlist also has a totalDuration function which will sum the duration of all of its tracks
totalDuration() {
let total = 0;
this.tracks.forEach((element) => {
total += element.length;
})
return total;
}
}
//A Track has a title, a rating (an integer from 1 to 5)
//and a length (integer in seconds) all of which are required
//when creating a new track
class Track {
constructor(title, rating, length){
this.title = title;
this.rating = rating;
this.length = length;
}
}
const library1 = new Library("My Classical Music", "Matt");
const playlist1 = new Playlist("The Best of Liszt");
const track1 = new Track("Liebestraum", 4, 350);
library1.addPlaylist(playlist1);
playlist1.addTrack(track1);
console.log("library1:", library1);
//console.log("playlist1:", playlist1);
//console.log("track1:", track1);
console.log("playlist1.overallRating():", playlist1.overallRating());
console.log("playlist1.totalDuration():", playlist1.totalDuration());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment