Advertisement
divanov94

Untitled

Oct 16th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function petHouse(){
  2.     class Pet{
  3.         constructor(owner,name) {
  4.             this.owner=owner;
  5.             this.name=name;
  6.             this.comments=[];
  7.         }
  8.         addComment(comment){
  9.             if(this.comments.includes(comment)){
  10.                 throw new Error(`This comment is already added!`);
  11.             }
  12.             this, this.comments.push(comment);
  13.             return `Comment is added.`;
  14.         }
  15.         feed(){
  16.             return `${this.name} is fed`
  17.         }
  18.  
  19.         toString(){
  20.             let result=[];
  21.             result.push(`Here is ${this.owner}'s pet ${this.name}.`)
  22.            if(this.comments.length>0){
  23.               result.push(`Special requirements: ${this.comments.join(", ")}`)
  24.            }
  25.            return result.join("\n");
  26.        }
  27.  
  28.    }
  29.  
  30.  
  31.    class Cat extends Pet{
  32.        constructor(owner, name, insideHabits, scratching) {
  33.            super(owner,name);
  34.            this.insideHabits=insideHabits;
  35.            this.scrathing=scratching;
  36.        }
  37.        feed(){
  38.            return `${super.feed()}, happy and puring.`
  39.        }
  40.        toString(){
  41.            let result=[];
  42.            result.push(`${super.toString()}`);
  43.            result.push(`Main information:`);
  44.            let temp=`${this.name} is a cat with ${this.insideHabits}`;
  45.            if(this.scrathing){
  46.                temp+=`, but beware of scratches.`
  47.            }
  48.            result.push(temp);
  49.            return result.join("\n");
  50.        }
  51.    }
  52.  
  53.    class Dog extends Pet{
  54.        constructor(owner, name, runningNeeds, trainability) {
  55.            super(owner,name);
  56.            this.runningNeeds=runningNeeds;
  57.            this.trainability=trainability;
  58.        }
  59.        feed(){
  60.            return `${super.feed()}, happy and wagging tail.`;
  61.        }
  62.  
  63.        toString() {
  64.            let result=[];
  65.            result.push(`${super.toString()}`);
  66.            result.push('Main information:');
  67.            result.push(`${this.name} is a dog with need of ${this.runningNeeds}km running every day and ${this.trainability} trainability.`);
  68.            return result.join('\n');
  69.        }
  70.    }
  71.  
  72.    return {
  73.        Pet,
  74.        Cat,
  75.        Dog
  76.    }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement