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 (spaceRequired > this.spaceAvailable) {
- throw new Error('Not enough space in the garden.');
- }
- this.plants.push({
- plantName,
- spaceRequired,
- ripe: false,
- quantity: 0,
- });
- this.spaceAvailable -= spaceRequired;
- return `The ${plantName} has been successfully planted in the garden.`;
- }
- ripenPlant(plantName, quantity) {
- let searchedPlant = this.plants.find((o) => o.plantName == plantName);
- if (!searchedPlant) {
- throw new Error(`There is no ${plantName} in the garden.`);
- }
- if (searchedPlant.ripe) {
- throw new Error(`The ${plantName} is already ripe.`);
- }
- if (quantity <= 0) {
- throw new Error(`The quantity cannot be zero or negative.`);
- }
- searchedPlant.ripe = true;
- searchedPlant.quantity += quantity;
- if (quantity === 1) {
- return `${quantity} ${plantName} has successfully ripened.`;
- }
- return `${quantity} ${plantName}s have successfully ripened.`;
- }
- harvestPlant(plantName) {
- let sPlant = this.plants.find((o) => o.plantName == plantName);
- if (!sPlant) {
- throw new Error(`There is no ${plantName} in the garden.`);
- }
- if (!sPlant.ripe) {
- throw new Error(
- `The ${plantName} cannot be harvested before it is ripe.`
- );
- }
- let plIndx = this.plants.findIndex((o) => o.plantName === plantName);
- this.plants.splice(plIndx, 1);
- this.storage.push({
- plantName: sPlant.plantName,
- quantity: sPlant.quantity,
- });
- this.spaceAvailable += sPlant.spaceRequired;
- return `The ${plantName} has been successfully harvested.`;
- }
- generateReport() {
- let result = [];
- result.push(`The garden has ${this.spaceAvailable} free space left.`);
- let sortedPlants = this.plants.sort((a, b) =>
- a.plantName.localeCompare(b.plantName)
- );
- let plInGarden = [];
- sortedPlants.forEach((pl) => plInGarden.push(pl.plantName));
- result.push(`Plants in the garden: ${plInGarden.join(', ')}`);
- if (this.storage.length == 0) {
- result.push('Plants in storage: The storage is empty.');
- } else {
- let plInSt = [];
- this.storage.forEach((pl) =>
- plInSt.push(`${pl.plantName} (${pl.quantity})`)
- );
- result.push(`Plants in storage: ${plInSt.join(', ')}`);
- }
- return result.join('\n');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement