Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PhoneBook {
- constructor(owner) {
- this.allContacts = [];
- this.owner = owner;
- }
- getContact(firstName, lastName, phoneNumber) {
- let contacts = {};
- for (const contact of this.allContacts) {
- if (
- constact.firstName === firstName ||
- constact.lastName === lastName ||
- constact.phoneBook === phoneBook
- ) {
- contacts.push(constact);
- }
- }
- return contacts;
- }
- addContact(newContact) {
- this.allContacts.push(newContact);
- }
- }
- //creating a base class
- class Contact {
- constructor(firstName, lastName, phoneNumber, address) {
- this.firstName = firstName;
- this.lastName = lastName;
- this.phoneNumber = phoneNumber;
- this.address = address;
- }
- get contactType() {
- if (this.relationship === "self") {
- console.log("Owner");
- } else if (this.relationship) {
- console.log("Family");
- } else {
- console.log("Friend");
- }
- }
- edit(parameter, newValue) {
- switch (parameter) {
- case 'firstName':
- this.firstName = newValue;
- break
- case 'lastName':
- this.lastName = newValue;
- break
- case 'phoneNumber':
- this.phoneNumber = newValue;
- break
- default:
- console.log("Please select a parameter to edit");
- }
- }
- details() {
- console.log(`
- First name: ${this.firstName}
- Last Name: ${this.lastName}
- Phone number: ${this.phoneNumber}
- Address: ${this.address}
- `);
- }
- static newContact(
- firstName,
- lastName,
- phoneNumber,
- address = "",
- relationship = ""
- ) {
- let newContact;
- if (relationship) {
- newContact = new Family(
- firstName,
- lastName,
- phoneNumber,
- address,
- relationship
- );
- } else {
- newContact = new Friend(firstName, lastName, phoneNumber, address);
- }
- return newContact;
- }
- }
- // friend inherits contact
- class Friend extends Contact {
- constructor(firstName, lastName, phoneNumber, address) {
- super(firstName, lastName, phoneNumber, address);
- }
- }
- //Family inherits contact
- class Family extends Contact {
- constructor(firstName, lastName, phoneNumber, address, relationship) {
- super(firstName, lastName, phoneNumber, address);
- this.relationship = relationship;
- }
- //override a function
- details() {
- console.log(`
- First name: ${this.firstName}
- Last Name: ${this.lastName}
- Phone number: ${this.phoneNumber}
- Address: ${this.address}
- Relationship: ${this.relationship}
- `);
- }
- }
- //instance
- const owner = Family.newContact("me", "I", "98877665444", "my house", "self");
- const brother = Family.newContact("my", "Brother", "98877665444", "my house", "brother");
- const friend = new Friend("Good", "Friend", "0012345678", "4, somewhere");
- //polymorphism
- owner.contactType;
- brother.contactType;
- friend.contactType;
- owner.details()
- brother.details()
- friend.details()
- friend.edit('firstName', 'Very Good')
- friend.details()
- //encapsulation
- const phoneBook = new PhoneBook(owner)
- phoneBook.owner.details()
- phoneBook.addContact(brother)
- phoneBook.addContact(friend)
- console.log(phoneBook.allContacts)
Advertisement
Add Comment
Please, Sign In to add comment