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

Address Book with OOP approach #9

Open
mhaidarhanif opened this issue Jul 27, 2018 · 0 comments
Open

Address Book with OOP approach #9

mhaidarhanif opened this issue Jul 27, 2018 · 0 comments
Labels
enhancement New feature or request

Comments

@mhaidarhanif
Copy link
Contributor

class Contact {
	constructor({ name = "", email = "", phone = "", website = "" }) {
		this.name = name;
		this.email = email;
		this.phone = phone;
		this.website = website;
	}

	display() {
		console.log(`${this.name} can be emailed at ${this.email}, or call via ${this.phone}`);
	}
}

class ContactFamily extends Contact {
	constructor({ name = "", email = "", phone = "", relation = "" }) {
		super({
			name,
			email,
			phone
		});
		this.relation = relation;
	}
}

class AddressBook {
	constructor(contacts = []) {
		this.contacts = contacts;
	}

	display() {
		this.contacts.forEach(contact => {
			const name = contact.name;
			const email = contact.email;
			const phone = contact.phone;

			if (name && email && phone) {
				console.log(`${name} can be emailed at ${email}, or called via ${phone}`);
			} else if (name && email) {
				console.log(`${name} can be emailed at ${email}}`);
			} else if (name && phone) {
				console.log(`${name} can be called via ${phone}`);
			} else if (name) {
				console.log(`${name} has no contact info`);
			} else {
				console.log(`Unknown person!`);
			}
		});
	}
}

const haidar = new Contact({
	email: "[email protected]",
	name: "M Haidar Hanif",
	phone: "+62-8-1993-101010",
	website: "https://mhaidarhanif.com"
});
const arie = new Contact({
	name: "Arie Brainware",
	email: "[email protected]"
});
const bapak = new ContactFamily({
	name: "Bapak",
	email: "[email protected]",
	phone: "+62-12345678",
	relation: "Father"
});

const addressBook = new AddressBook([haidar, arie, bapak]);

addressBook.display();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

10 participants