From 745a310bceb0067ffd5f6f8fcdd1a6069c023678 Mon Sep 17 00:00:00 2001 From: Maya Buser De Date: Tue, 11 May 2021 15:05:05 -0400 Subject: [PATCH] creating class plant with subclasses --- app.js | 109 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 57 insertions(+), 52 deletions(-) diff --git a/app.js b/app.js index 29c0766..ff7cc08 100644 --- a/app.js +++ b/app.js @@ -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(); \ No newline at end of file