Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Garden {
- constructor(spaceAvailable) {
- this.spaceAvailable = spaceAvailable;
- this.plants = [];
- this.storage = [];
- }
- addPlant(plantName, spaceRequired) {
- if (this.spaceAvailable - spaceRequired < 0) {
- throw new Error('Not enough space in the garden.');
- } else {
- this.spaceAvailable -= spaceRequired;
- let plant = {
- plantName: plantName,
- spaceRequired: spaceRequired,
- ripe: false,
- quantity: 0
- }
- this.plants.push(plant);
- }
- return this.addPlant;
- }
- ripenPlant(plantName, quantity) {
- if (plant.plantName !== plantName) {
- throw new Error(`There is no ${plantName} in the garden.`);
- }
- if (plant.ripe === true) {
- throw new Error(`The ${plantName} is already ripe.`);
- }
- if (plant.quantity <= 0) {
- throw new Error(`The quantity cannot be zero or negative.`);
- }
- return this.ripenPlant;
- }
- }
- const myGarden = new Garden(250)
- console.log(myGarden.addPlant('apple', 20));
- console.log(myGarden.addPlant('orange', 100));
- console.log(myGarden.addPlant('cucumber', 30));
- console.log(myGarden.ripenPlant('apple', 10));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement