Advertisement
viligen

garden

Jun 11th, 2022
1,023
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Garden {
  2.     constructor(spaceAvailable) {
  3.         this.spaceAvailable = spaceAvailable;
  4.         this.plants = [];
  5.         this.storage = [];
  6.     }
  7.  
  8.     addPlant(plantName, spaceRequired) {
  9.         if (spaceRequired > this.spaceAvailable) {
  10.             throw new Error('Not enough space in the garden.');
  11.         }
  12.         this.plants.push({
  13.             plantName,
  14.             spaceRequired,
  15.             ripe: false,
  16.             quantity: 0,
  17.         });
  18.         this.spaceAvailable -= spaceRequired;
  19.         return `The ${plantName} has been successfully planted in the garden.`;
  20.     }
  21.     ripenPlant(plantName, quantity) {
  22.         let searchedPlant = this.plants.find((o) => o.plantName == plantName);
  23.         if (!searchedPlant) {
  24.             throw new Error(`There is no ${plantName} in the garden.`);
  25.         }
  26.         if (searchedPlant.ripe) {
  27.             throw new Error(`The ${plantName} is already ripe.`);
  28.         }
  29.         if (quantity <= 0) {
  30.             throw new Error(`The quantity cannot be zero or negative.`);
  31.         }
  32.         searchedPlant.ripe = true;
  33.         searchedPlant.quantity += quantity;
  34.         if (quantity === 1) {
  35.             return `${quantity} ${plantName} has successfully ripened.`;
  36.         }
  37.         return `${quantity} ${plantName}s have successfully ripened.`;
  38.     }
  39.     harvestPlant(plantName) {
  40.         let sPlant = this.plants.find((o) => o.plantName == plantName);
  41.  
  42.         if (!sPlant) {
  43.             throw new Error(`There is no ${plantName} in the garden.`);
  44.         }
  45.         if (!sPlant.ripe) {
  46.             throw new Error(
  47.                 `The ${plantName} cannot be harvested before it is ripe.`
  48.             );
  49.         }
  50.         let plIndx = this.plants.findIndex((o) => o.plantName === plantName);
  51.         this.plants.splice(plIndx, 1);
  52.         this.storage.push({
  53.             plantName: sPlant.plantName,
  54.             quantity: sPlant.quantity,
  55.         });
  56.         this.spaceAvailable += sPlant.spaceRequired;
  57.         return `The ${plantName} has been successfully harvested.`;
  58.     }
  59.     generateReport() {
  60.         let result = [];
  61.         result.push(`The garden has ${this.spaceAvailable} free space left.`);
  62.         let sortedPlants = this.plants.sort((a, b) =>
  63.             a.plantName.localeCompare(b.plantName)
  64.         );
  65.         let plInGarden = [];
  66.         sortedPlants.forEach((pl) => plInGarden.push(pl.plantName));
  67.         result.push(`Plants in the garden: ${plInGarden.join(', ')}`);
  68.         if (this.storage.length == 0) {
  69.             result.push('Plants in storage: The storage is empty.');
  70.         } else {
  71.             let plInSt = [];
  72.             this.storage.forEach((pl) =>
  73.                 plInSt.push(`${pl.plantName} (${pl.quantity})`)
  74.             );
  75.             result.push(`Plants in storage: ${plInSt.join(', ')}`);
  76.         }
  77.         return result.join('\n');
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement