impactbyte-drogon-scholarship / discussion

General Discussion
https://glints.id
The Unlicense
2 stars 0 forks source link

Address Book with OOP approach #9

Open mhaidarhanif opened 6 years ago

mhaidarhanif commented 6 years ago
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: "haidar@impactbyte.com",
    name: "M Haidar Hanif",
    phone: "+62-8-1993-101010",
    website: "https://mhaidarhanif.com"
});
const arie = new Contact({
    name: "Arie Brainware",
    email: "arie@brainware.com"
});
const bapak = new ContactFamily({
    name: "Bapak",
    email: "bapak@gmail.com",
    phone: "+62-12345678",
    relation: "Father"
});

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

addressBook.display();