Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 57 additions & 52 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,74 @@
class Person {
//creates instance of Person with properties name, age, and numBooksRead
constructor(name, age, numBooksRead) {
this.name = name;
this.age = age;
this.numBooksRead = numBooksRead;
class Plant {
constructor(commonName, latinName) {
this._commonName = commonName;
this._latinName = latinName;
this._isWatered = false;
}

//increments numBooksRead by 1
readNewBook() {
this.numBooksRead++;
get commonName() {
return this._commonName;
}
}

class Electrician extends Person {
//creates instance of Electrician with Person properites plus certifications, an array of strings
constructor(name, age, numBooksRead, certifications) {
super(name, age, numBooksRead);
this.certifications = certifications;
get latinName() {
return this._latinName;
}
}

class Teenager extends Person {
//creates instance of Teenager with Person properties plus isHungry, a boolean value
constructor(name, age, numBooksRead, isHungry = true) {
super(name, age, numBooksRead);
this.isHungry = isHungry;
get isWatered() {
return this._isWatered;
}

//feeds the teenager. If they are hungry, changes isHungry to false, if they are not hungry, prints message to console.
eat() {
if(this.isHungry) {
this.isHungry = false;
} else {
console.log('Oh no I think I ate too much')
}
water() {
this._isWatered = !this._isWatered;
}
}

//below we will test our classes by instantiating them, calling each method, and verifying output

//create instances of each class
let person = new Person('Euthyphro', 35, 0);
let electrician = new Electrician('Zeus', 28, 3, ['Lightning', 'Polymorphism']);
let teen = new Teenager('Persius', 16, 5, true);
class Flower extends Plant {
constructor(commonName, latinName, flowerColor) {
super(commonName, latinName);
this._flowerColor = flowerColor;
}
}

//log each instance
console.log(person);
console.log(electrician);
console.log(teen);
class Vegetable extends Plant {
constructor(commonName, latinName, daysToMaturity) {
super(commonName, latinName);
this._daysToMaturity = daysToMaturity;
}

//call the method from the parent class to ensure it works as expected
person.readNewBook();
electrician.readNewBook();
teen.readNewBook();
get daysToMaturity() {
return this._daysToMaturity;
}

//log the objects after calling .readNewBook() to verify it worked as intended
console.log(person);
console.log(electrician);
console.log(teen);
set daysToMaturity(days) {
if (typeof days !== 'number') {
console.log('Please enter a number!')
} else {
this._daysToMaturity = days;
}
}

//call .eat()
teen.eat();
readyToHarvest(daysFromPlanting) {
if (this.daysToMaturity - daysFromPlanting > 10) {
return 'Your veggies are growing, patience!'
} else if (this.daysToMaturity - daysFromPlanting > -5) {
return 'Go check your veggies, harvest should be about ready!'
} else {
return 'the harvest is overdue...go see if there is anything left.'
}
}
}

//check that .eat() when isHungry == true works as expected
console.log(teen);
const rose = new Flower('Rose', 'Rosa', 'Red');
const tomato = new Vegetable('Tomato', 'Solanum lycopersicum', 75);
tomato.water();
rose.water();
console.log(tomato.isWatered);
console.log(rose.isWatered);
console.log(tomato.readyToHarvest(34));
console.log(tomato.readyToHarvest(77));
console.log(tomato.readyToHarvest(100));
tomato.water();
rose.water();
console.log(tomato.isWatered);
console.log(rose.isWatered);

//check that .eat() when isHungry == false works as expected
teen.eat();