Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OOP exersice #576

Merged
merged 3 commits into from
Oct 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions submissions/iva-stasia/oop-exercise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { print } from './js/lib.js';

class Inhabitant {
constructor(species, name, gender, legs, saying) {
this.species = species,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why commas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot that commas are not used in classes and was confused with the syntax of objects. Fixed in my repo.

this.name = name,
this.gender = gender,
this.legs = legs,
this.saying = saying
}

toPrint() {
return `${this.species}. ${this.saying.words}! My name's ${this.name} and I'm ${this.gender}. I have ${this.legs} legs.`
}
};

class Human extends Inhabitant {
constructor({species = 'Human', name, gender, legs = '2', saying, hands = '2'}) {
super(species, name, gender, legs, saying)
this.hands = hands
}

toPrint() {
return `${super.toPrint()} As a human, I also have ${this.hands} hands.`
}
};

class Animal extends Inhabitant {
constructor({species, name, gender, legs = '4', saying}) {
super(species, name, gender, legs, saying)
}
};

class Man extends Human {
constructor({species, name, gender = 'male', legs, saying, hands}) {
super({species, name, gender, legs, saying, hands})
}
};

class Woman extends Human {
constructor({species, name, gender = 'female', legs, saying, hands}) {
super({species, name, gender, legs, saying, hands})
}
};

class Dog extends Animal {
constructor({species = 'Dog', name, gender, legs, saying}) {
super({species, name, gender, legs, saying})
}
};

class Cat extends Animal {
constructor({species = 'Cat', name, gender, legs, saying}) {
super({species, name, gender, legs, saying})
}
};

const say = {
cat: {words: 'Meeeow'},
dog: {words: 'Woof-woof'},
woman: {words: 'Hello'},
man: {words: 'Glad to see you'},
};

const man = new Man({name: 'Leon', saying: say.man});
const woman = new Woman({name: 'Matilda', saying: say.woman});
const dog = new Dog({name: 'Pretty', gender: 'male', saying: say.dog});
const cat = new Cat({name: 'Kitty', gender: 'female', saying: say.cat});
const catwoman = Object.assign(new Woman({name: 'Selina'}), {saying: cat.saying});

const inhabitants = [dog, cat, woman, man, catwoman];

cat.saying.words = 'Mrr... Meeeow';

inhabitants.forEach(inhabitant => {print(inhabitant.toPrint())});