eltneg

jsOOP

Oct 3rd, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class PhoneBook {
  2.   constructor(owner) {
  3.     this.allContacts = [];
  4.     this.owner = owner;
  5.   }
  6.  
  7.   getContact(firstName, lastName, phoneNumber) {
  8.     let contacts = {};
  9.     for (const contact of this.allContacts) {
  10.       if (
  11.         constact.firstName === firstName ||
  12.         constact.lastName === lastName ||
  13.         constact.phoneBook === phoneBook
  14.       ) {
  15.         contacts.push(constact);
  16.       }
  17.     }
  18.     return contacts;
  19.   }
  20.  
  21.   addContact(newContact) {
  22.     this.allContacts.push(newContact);
  23.   }
  24. }
  25.  
  26.  
  27. //creating a base class
  28. class Contact {
  29.   constructor(firstName, lastName, phoneNumber, address) {
  30.     this.firstName = firstName;
  31.     this.lastName = lastName;
  32.     this.phoneNumber = phoneNumber;
  33.     this.address = address;
  34.   }
  35.  
  36.   get contactType() {
  37.     if (this.relationship === "self") {
  38.       console.log("Owner");
  39.     } else if (this.relationship) {
  40.       console.log("Family");
  41.     } else {
  42.       console.log("Friend");
  43.     }
  44.   }
  45.  
  46.   edit(parameter, newValue) {
  47.     switch (parameter) {
  48.       case 'firstName':
  49.         this.firstName = newValue;
  50.         break
  51.       case 'lastName':
  52.         this.lastName = newValue;
  53.         break
  54.       case 'phoneNumber':
  55.         this.phoneNumber = newValue;
  56.         break
  57.       default:
  58.         console.log("Please select a parameter to edit");
  59.     }
  60.   }
  61.  
  62.   details() {
  63.     console.log(`
  64.         First name: ${this.firstName}
  65.         Last Name: ${this.lastName}
  66.         Phone number: ${this.phoneNumber}
  67.         Address: ${this.address}
  68.         `);
  69.   }
  70.  
  71.   static newContact(
  72.     firstName,
  73.     lastName,
  74.     phoneNumber,
  75.     address = "",
  76.     relationship = ""
  77.   ) {
  78.     let newContact;
  79.     if (relationship) {
  80.       newContact = new Family(
  81.         firstName,
  82.         lastName,
  83.         phoneNumber,
  84.         address,
  85.         relationship
  86.       );
  87.     } else {
  88.       newContact = new Friend(firstName, lastName, phoneNumber, address);
  89.     }
  90.     return newContact;
  91.   }
  92. }
  93.  
  94.  
  95. // friend inherits contact
  96. class Friend extends Contact {
  97.   constructor(firstName, lastName, phoneNumber, address) {
  98.     super(firstName, lastName, phoneNumber, address);
  99.   }
  100. }
  101.  
  102.  
  103. //Family inherits contact
  104. class Family extends Contact {
  105.   constructor(firstName, lastName, phoneNumber, address, relationship) {
  106.     super(firstName, lastName, phoneNumber, address);
  107.     this.relationship = relationship;
  108.   }
  109.  
  110.   //override a function
  111.   details() {
  112.     console.log(`
  113.  
  114.         First name: ${this.firstName}
  115.         Last Name: ${this.lastName}
  116.         Phone number: ${this.phoneNumber}
  117.         Address: ${this.address}
  118.         Relationship: ${this.relationship}
  119.         `);
  120.   }
  121. }
  122.  
  123.  
  124. //instance
  125. const owner = Family.newContact("me", "I", "98877665444", "my house", "self");
  126. const brother = Family.newContact("my", "Brother", "98877665444", "my house", "brother");
  127. const friend = new Friend("Good", "Friend", "0012345678", "4, somewhere");
  128.  
  129.  
  130. //polymorphism
  131. owner.contactType;
  132. brother.contactType;
  133. friend.contactType;
  134.  
  135. owner.details()
  136. brother.details()
  137. friend.details()
  138. friend.edit('firstName', 'Very Good')
  139. friend.details()
  140.  
  141. //encapsulation
  142. const phoneBook = new PhoneBook(owner)
  143. phoneBook.owner.details()
  144.  
  145. phoneBook.addContact(brother)
  146. phoneBook.addContact(friend)
  147.  
  148. console.log(phoneBook.allContacts)
Advertisement
Add Comment
Please, Sign In to add comment