School Catalogue Project - inheritance and object instantiation


#1

I was wondering if someone could review my code. I got to the end of the Schools Catalogue project in classes. My code is below. I was able to complete it but I cannot create a school and pass subclass arguments in. Instead of creating an object in one step like so (in the case of primary schools:

const primarySchool = new School( 'A Primary School', 'primary' , 34, 'Pickup up your kids at 3pm sharp. Must be a parent');

I have to write the following because my class is not seeing the pickupPolicy argument

primarySchool.pickupPolicy = 'Pickup up your kids at 3pm sharp. Must be a parent';

If I don’t do that then when I call console.log(primarySchool) the pickup policy will not show. The same is true for my high school sports teams.

Can anyone tell me what I’m doing wrong?

//Parent Class
class School {
  constructor(name, level, studentCount){
    this._name = name;
    this._level = level;
    this._numberOfStudents = studentCount;
  } 
  
  get name(){
    return this._name;
  }
  
 set level(index){
    this._level = this._level[index];
  }
 
  get level(){
    return this._level;
  }
   
  get numberOfStudents(){
    return this._numberOfStudents; 
    }
 
  set numberOfStudents(newNumberOfStudents){
    if (typeof newNumberOfStudents === 'number'){
    this._numberOfStudents = newNumberOfStudents;
 		} else {
    this._numberOfStudents = newNumberOfStudents + ' is not a number'; 
  	}
	}
  
  quickFacts(){
    console.log(`${this._name} educates ${this._numberOfStudents} students at the ${this._level} school level`); 
  }
  
  static pickSubstituteTeacher (substituteTeachers){
    console.log(substituteTeachers)
    let randomTeacher = Math.floor(substituteTeachers.length * Math.random());
    console.log(randomTeacher)
    return substituteTeachers[randomTeacher];
  }
  
}

//School test
const valleySchool = new School('Valley High');

valleySchool.numberOfStudents = 345;
//valleySchool.level = 2;
valleySchool.quickFacts();

console.log(School.pickSubstituteTeacher(['Mr. Garrison','Mr Mackey','Mrs Cribapple','Principle Vagina']));

///////// Subclasses

////Primary
class Primary extends School{
  constructor(name, numberOfStudents, pickupPolicy){
    super(name, level, numberOfStudents);
    this._pickupPolicy = pickupPolicy;
  }
  
  get pickupPolicy(){
    return this._pickupPolicy;
  }
  
  
}

const primarySchool = new School('1st', 'primary' , 34, 'before school');

primarySchool.pickupPolicy = '4pm pickup'

console.log(primarySchool);


////Middle
class Middle extends School{
  constructor(name){
    super(name, level, numberOfStudents);
  }
}

const middleSchool = new School('Middle','middle', 4556);

//middleSchool.level = 1;
console.log(middleSchool);



////High
class High extends School{
  constructor( name, level, numberOfStudents, sportsTeams){
    super(name, level, numberOfStudents);
    this._sportsTeams = _sportsTeams;
  }
  
  get sportsTeams(){
    return this._sportsTeams;
  }
  
  set sportsTeams(sportsTeams){
    this._sportsTeams = sportsTeams;
  }
  
}

const highSchool = new School('High', ['foxes','hounds','rabbits']);

highSchool.numberOfStudents = 445;
console.log(highSchool);


//FINAL TESTS

const lorraineHansbury = new School('Lorraine Hansbury','primary',514);

lorraineHansbury.pickupPolicy = 'Students must be picked up by a parent, guardian, or a family member over the age of 13.'
console.log(lorraineHansbury);

lorraineHansbury.quickFacts()

console.log(School.pickSubstituteTeacher(['Jamal Crawford', 'Lou Williams', 'J. R. Smith', 'James Harden', 'Jason Terry', 'Manu Ginobli']));

const alsmith = new School('Al E. Smith','high',415)

alsmith.sportsTeams = ['Baseball', 'Basketball', 'Volleyball', 'Track and Field'] 
console.log(alsmith);

// QUESTION WHY DOES 
const williamPit = new School('William Pit','high', 815, ['Football', 'Cricket', 'Tennis', 'Rugby'])

console.log(williamPit);


#2

Your class constructor only takes three parameters but your instance call has four arguments.


#3

@mtf

I tried this:

////Primary
class Primary extends School{
  constructor(name, level, numberOfStudents, pickupPolicy){
    super(name, level, numberOfStudents);
    this._pickupPolicy = pickupPolicy;
  }
  
  get pickupPolicy(){
    return this._pickupPolicy;
  }
}

const primarySchool = new School('PrimaryTest', 'primary' , 34, 'Pick up your kids at 4pm sharp round the back gate'); 

and the console still shows this after modifying the constructor to add an extra argument to the super:

School {
  _name: 'PrimaryTest',
  _level: 'primary',
  _numberOfStudents: 34 }

#4

Might that be because you are calling

new School()

and not

new Primary()

Just a thought.


#5

School Catalogue

  1. … Since this is the PrimarySchool class, pass ‘primary’ as the argument for the level parameter in the parent constructor.

I interpret this to mean…

super(name, 'primary', numberOfStudents);

which would mean,

constructor(name, numberOfStudents, pickupPolicy)

As @thinkverse pointed out, be sure to invoke the correct class instance, PrimarySchool.


#6

Oops! When I make such obvious errors in future just tell me too look again bug hint INHERITANCE !
Thanks so much for answering my stupid question !

@mtf and @thinkverse you are very kind and very patient!

:+1:


#7

Don’t sweat it dude, it happens all the time, you write code you know it’s going to work, then something doesn’t work, you scan through it like a madman trying to figure out what went wrong, and it’s most likely a misspelled variable or a wrong method call somewhere, happens to me all the time when you go in to the trance of coding.