Guest User

PetHouse

a guest
Oct 19th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveClasses() {
  2.     class Pet {
  3.         comments = [];
  4.  
  5.         constructor(owner, name) {
  6.             this.owner = owner;
  7.             this.name = name;
  8.         }
  9.  
  10.         addComment(comment) {
  11.             if (this.comments.includes(comment)) {
  12.                 throw new Error('This comment is already added!');
  13.             }
  14.  
  15.             this.comments.push(comment);
  16.  
  17.             return 'Comment is added.';
  18.         }
  19.  
  20.         feed() {
  21.             return `${this.name} is fed`
  22.         }
  23.  
  24.         toString() {
  25.             let result = `Here is ${this.owner}'s pet ${this.name}.`;
  26.  
  27.            if (this.comments.length > 0) {
  28.                result += '\n';
  29.                result += `Special requirements: ${this.comments.join(', ')}`;
  30.            }
  31.  
  32.            return result;
  33.        }
  34.    }
  35.  
  36.    class Cat extends Pet {
  37.        constructor(owner, name, insideHabits, scratching) {
  38.            super(owner, name);
  39.  
  40.            this.insideHabits = insideHabits;
  41.            this.scratching = scratching;
  42.        }
  43.  
  44.        feed() {
  45.            return super.feed() + ', happy and purring.';
  46.        }
  47.  
  48.        toString() {
  49.            let result = super.toString();
  50.  
  51.            result += '\n';
  52.            result += 'Main information:';
  53.            result += '\n';
  54.            result += `${this.name} is a cat with ${this.insideHabits}`;
  55.  
  56.            if (this.scratching) {
  57.                result += ', but beware of scratches.';
  58.            }
  59.  
  60.            return result;
  61.        }
  62.    }
  63.  
  64.    class Dog extends Pet {
  65.        constructor(owner, name, runningNeeds, trainability) {
  66.            super(owner, name);
  67.  
  68.            this.runningNeeds = runningNeeds;
  69.            this.trainability = trainability;
  70.        }
  71.  
  72.        feed() {
  73.            return super.feed() + ', happy and wagging tail.';
  74.        }
  75.  
  76.        toString() {
  77.            let result = super.toString();
  78.  
  79.            result += '\n';
  80.            result += 'Main information:';
  81.            result += '\n';
  82.            result += `${this.name} is a dog with need of ${this.runningNeeds}km running every day and ${this.trainability} trainability.`;
  83.  
  84.            return result;
  85.        }
  86.    }
  87.  
  88.    return {
  89.        Pet,
  90.        Cat,
  91.        Dog
  92.    }
  93. }
  94.  
  95. let classes = solveClasses();
  96. // let pet = new classes.Pet('Ann', 'Merry');
  97. // console.log(pet.addComment('likes bananas'));
  98. // console.log(pet.addComment('likes sweets'));
  99. // console.log(pet.feed());
  100. // console.log(pet.toString());
  101.  
  102. // let cat = new classes.Cat('Jim', 'Sherry', 'very good habits', true);
  103. // console.log(cat.addComment('likes to be brushed'));
  104. // console.log(cat.addComment('sleeps a lot'));
  105. // console.log(cat.feed());
  106. // console.log(cat.toString());
  107.  
  108. let dog = new classes.Dog('Susan', 'Max', 5, 'good');        
  109. console.log(dog.addComment('likes to be brushed'));
  110. console.log(dog.addComment('sleeps a lot'));
  111. console.log(dog.feed());
  112. console.log(dog.toString());
Advertisement
Add Comment
Please, Sign In to add comment