School catalog Project

Hello,
I am currently working on the School Catalog Project.
I am at step 19 : " Create a class called SchoolCatalog that holds a collection of schools. Create an instance of SchoolCatalog for primary, middle, and high schools."
Here is my code.
It works, but I’m wondering if there is a way to make the task automatic.
Instead of doing for example : "HighCatalog.addSchool(alSmith,‘high’), I would like that : when we create a new school, it is added to the catalog automatically and not manually.

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(numberOfStudents){
    if (typeof numberOfStudents === 'number'){
      this._numberOfStudents = numberOfStudents;
    } 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){
    let randomIndex=Math.floor(Math.random()*substituteTeachers.length);
    return substituteTeachers[randomIndex];
  }
}

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

class MiddleSchool extends School {
    constructor(name,numberofStudents){
      super(name,'middle',numberofStudents)
    }
}

class HighSchool extends School {
  constructor(name,numberOfStudents,sportsTeams){
    super(name,'high',numberOfStudents);
    this._sportsTeams = sportsTeams;
  }

  get sportsTeams (){
    return this._sportsTeams;
  }
}

class SchoolCatalog {
  constructor(level){
    this._level = level;
    this._school = [];
  }
  get level(){
    return this._level;
  }
  get school(){
    return this._school;
  }
  set school(school){
    return this._school.push(school);
  }
  addSchool(school,level){
    if (level === this.level){
    this.school = school;
    } else {
      console.log(`You got the wrong catalog! Uou are trying to register a ${level} school in the ${this.level} catalog!`  )
    }

  }
}

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();




School.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']);

console.log(alSmith.sportsTeams);

const Primarycatalog = new SchoolCatalog('primary');
const Middlecatalog = new SchoolCatalog('middle');
const HighCatalog = new SchoolCatalog('high');

HighCatalog.addSchool(alSmith,'high');
console.log(HighCatalog);
HighCatalog.addSchool(alSmith,'middle');

Thanks for your help

This is absolutely possible but it requires a little modification of the code here and there. Anything you want to happen to a class instance upon instantiation can be placed within the constructor() for that class. Therefore, if we want a new primary school to be added to PrimaryCatalog upon instantiation, we can make this a part of the constructor, like below:

class PrimarySchool extends School {
    constructor(name, numberOfStudents, pickupPolicy) {
        super(name, 'primary', numberOfStudents);
        this._pickupPolicy = pickupPolicy;
        // the below line is the new one, where this refers to the current class instance
        PrimaryCatalog.addSchool(this, 'primary')
    }
    get pickupPolicy() {
        return this._pickupPolicy;
    }
}

This can then be replicated for Middle and High by changing the line to suit. However, it’s important to note that naturally, this means the Catalog instances need to be created before any schools are created, so you would need to move your declarations for the catalogs to above lorraineHansbury else you will get an error, and make sure that PrimaryCatalog matches the catalog declaration (this is currently Primarycatalog in your code).

Hello,

Thank you very much for the explanations. It’s very clear :+1:
I tried following your recommendations and it works perfectly.

Again, thank you very much. :wink: