Skip to content
Advertisement

How to create a class that holds collection of other class in JavaScript

I want to create a class called SchoolCatalog that will hold a collection of schools, e.g. Create an instance of SchoolCatalog for primary, middle, and high schools. (This is one of the problems from Code Academy Introduction To JavaScript). But, they have no proper guide on how to make a Catalog to hold all classes in the forum. So, if anybody can help me out I will be grateful.

class School {
  constructor(name, level, numberOfStudents) {
    this._name = name
    this._level = level
    this._numberOfStudents = numberOfStudents 
  }
  
  get name() {
    return this._name
  }
  
  get level() {
    return this._level
  }
  
  get numberOfStudents() {
    return this._numberOfStudents 
  }
  
  
  set numberOfStudents(studentsNumber) {
    if (typeof studentNumber === 'number')
      this._numberOfStudents = studentsNumber
    else
      console.log(`Invalid input: numberOfStudents must be set to a Number.`)
  }
  
  quickFacts() {
    console.log(`${this._name} educates ${this._numberOfStudents} students at the ${this._level} school level.`)
  }
  
  static pickSubstituteTeacher(substituteTeachers) {
    const randomTeacher = Math.floor(Math.random() * substituteTeachers.length)
    console.log(substituteTeachers[randomTeacher])
  }
  
}

// Primary School

class PrimarySchool extends School {
  constructor(name, numberOfStudents, pickupPolicy) {
    super(name, 'primary', numberOfStudents)
    this._pickupPolicy = pickupPolicy
  }
  
  get pickupPolicy() {
    return this._pickupPolicy
  }
}


class HighSchool extends School {
  constructor(name, numberOfStudents, sportsTeams) {
    super(name,'high', numberOfStudents)
    this._sportsTeams = sportsTeams
  }
  
  get sportsTeams() {
    this._sportsTeams.forEach(team => {
      console.log(team)
    })
  }
}

const lorraineHansbury = new PrimarySchool('Lorraine Hansbury', 514, 'Students must be picked up by a parent, guardian, or a family member over the age of 13.')

lorraineHansbury.quickFacts()
PrimarySchool.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli'])


const alSmith = new HighSchool('Al E. Smith', 415, ['Baseball', 'Basketball', 'Volleyball', 'Track and Field'])
alSmith.sportsTeams


// My Attempt To Make Collection of Classes


/*class SchoolCatalog {
  constructor(schools) {
    this._schools = schools
  }
  
  get schools() {
    this._schools.forEach(school => {
      console.log(school)
    })
  }
}


const catalog = new SchoolCatalog([''])*/

Advertisement

Answer

Is there an issue with your implementation ?

class Catalog{
   constructor(){
     this.schools = []
   }
   addSchool(school){
     if(!this[school._name]){
       // reference for easy access
       this[school._name]=school;
     }
     this.schools.push(school);
   }
}

If I needed a list I could iterate over and still easily get single schools from this is how i would do it ,

That said depending on your requirements this could be totally wrong.

Your implementation at first blush doesn’t look to be incorrect , is it giving you a error?

Advertisement