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