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 exercise #416

Merged
merged 8 commits into from
Sep 26, 2022
Merged
Changes from 6 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
138 changes: 138 additions & 0 deletions submissions/OlexiyDobroskok/OOP_exercise/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
class Inhabitant {
constructor({
species = "No species",
name = "No name",
gender = "No gender",
saying = "Hello",
friends = [],
}) {
this.species = species;
this.name = name;
this.gender = gender;
this.saying = saying;
this.friends = friends;
}

addFriend(person) {
this.friends.push(person);
}

introduceYourSelf() {
if (this.friends.length === 0) {
this.friends = "Looking for friends!";
Copy link
Member

Choose a reason for hiding this comment

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

What will happen if we .addFriend after first call of .introduceYourSelf?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@

What will happen if we .addFriend after first call of .introduceYourSelf?

@OleksiyRudenko Instead of an empty array, I get a string...) Done)

return `<strong>${this.saying}! My name is ${this.name}. Species: ${this.species}. Gender: ${this.gender}. Friends: ${this.friends} </strong>`;
}
const friendsName = this.friends.map((friend) => friend.name).join(", ");
return `<strong>${this.saying}! My name is ${this.name}. Species: ${this.species}. Gender: ${this.gender}. Friends: ${friendsName}. </strong>`;
}
Copy link
Member

Choose a reason for hiding this comment

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

This fragment can be made DRY.
Optimize this method.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This fragment can be made DRY. Optimize this method.

done

}

class Human extends Inhabitant {
constructor({ name, gender, saying, legs = 2, hands = 2, friends }) {
super({ species: "human", name, gender, saying, friends });
this.hands = hands;
this.legs = legs;
}
introduceYourSelf() {
return (
super.introduceYourSelf() +
`<strong>I have ${this.hands} hands and ${this.legs} legs!</strong>`
);
}
}

class Man extends Human {
constructor({ name, saying, friends, legs, hands }) {
super({ name, gender: "male", saying, legs, hands, friends });
}
}

class Woman extends Human {
constructor({ name, saying, friends, legs, hands }) {
super({ name, gender: "female", saying, legs, hands, friends });
}
}

class Pets extends Inhabitant {
constructor({ species, name, gender, paws = 4, saying, friends }) {
super({ species, name, gender, saying, friends });
this.paws = paws;
}
introduceYourSelf() {
return (
super.introduceYourSelf() + `<strong>I have ${this.paws} paws!</strong>`
);
}
}

class Cat extends Pets {
static meow;
constructor({ name, gender, saying = "¡miau miau", friends, paws }) {
super({
species: "cat",
name,
gender,
saying,
paws,
friends,
});
Cat.meow = saying;
this.saying = Cat.getMeow();
}
static getMeow() {
return this.meow;
}
}

class Dog extends Pets {
static bark;
constructor({ name, gender, saying = "¡guau guau", friends, paws }) {
super({
species: "dog",
name,
gender,
saying,
paws,
friends,
});
Dog.bark = this.saying;
}
}

class CatWoman extends Woman {
constructor({ name, saying = "", friends, legs, hands }) {
super({ name, saying, friends, legs, hands });
this.saying = Cat.getMeow() + " " + saying;
}
}

const manJose = new Man({ name: "José", saying: "Hola, amigo" });
const womanMartina = new Woman({ name: "Martina", saying: "Buenos días" });
const catLalo = new Cat({ name: "Lalo", gender: "female" });
const dogPako = new Dog({ name: "Pako", gender: "male" });
const catwomanNerea = new CatWoman({ name: "Nerea", saying: "muchachos" });

manJose.addFriend(catwomanNerea);
womanMartina.addFriend(dogPako);
catwomanNerea.addFriend(catLalo);
catwomanNerea.addFriend(manJose);

const worldPopulation = [
manJose,
womanMartina,
catLalo,
dogPako,
catwomanNerea,
];

function showWorldPopulation() {
const inhabitantNames = worldPopulation.map((inhabitant) => inhabitant.name);
print(
`<strong>Population: ${worldPopulation.length} inhabitants! It is: ${inhabitantNames}</strong>`
);
worldPopulation.forEach((inhabitant) =>
print(inhabitant.introduceYourSelf())
);
}

showWorldPopulation();