Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. var readline = require("readline-sync");
  2.  
  3. //3a
  4. class Contact{
  5. constructor(name, mobileNumber){
  6. this.name = name;
  7. this.mobileNumber = mobileNumber;
  8. }
  9.  
  10. getContactDetails(){
  11. console.log("Name: " + this.name + "\nMobile Number: " + this.mobileNumber);
  12. }
  13. }
  14.  
  15. //3b
  16. class AddressBook{
  17. //i
  18. constructor() {
  19. this.myContact = []; //class property
  20. this.myContact.push(new Contact("Homer", 98849959));
  21. this.myContact.push(new Contact("Marge", 84774744));
  22. this.myContact.push(new Contact("Lisa", 86994994));
  23. this.myContact.push(new Contact("Maggie", 94775883));
  24. this.myContact.push(new Contact("Bart", 88838848));
  25. } //end constructor
  26.  
  27. //ii
  28. getNumberofContacts(){
  29. console.log(this.myContact.length);
  30. }
  31.  
  32. //iii
  33. getContactAt(index){
  34. console.log(this.myContact[index]);
  35. }
  36.  
  37. //iv
  38. searchContact(search){
  39. for(var i = 0; i < this.myContact.length; i++){
  40. if(search != this.myContact[i].name){
  41. i++
  42. }
  43. else{
  44. console.log(this.myContact[i].mobileNumber);
  45. }
  46. }
  47. }
  48.  
  49. myAddressBook(){
  50. var dowhat = readline.question("Personal Addressbook\n--------------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n>> ")
  51. if(dowhat == 1){
  52. var i = 0
  53. while(i >= 0 || i < this.myContact.length){
  54. console.log("-----------------------------\nContact " + (i + 1) + " of 5\n-----------------------------\n" + "Name: " + this.myContact[i].name + "\nMobile Number: " + this.myContact[i].mobileNumber + "\n-----------------------------");
  55. var nextaction = readline.question("(Press N for next contact, P for previous contact, X to exit)\n>> ");
  56. if(nextaction == "N" || nextaction == "n"){
  57. i++
  58. }
  59. if(nextaction == "P" || nextaction == "p"){
  60. i--
  61. }
  62. if(nextaction == "X" || nextaction == "x"){
  63. this.myAddressBook()
  64. }
  65. }
  66. }
  67.  
  68. else if(dowhat == 2){
  69. var s = readline.question("Enter the name of the contact: ");
  70. console.log(s + "'s mobile number is " + this.searchContact(s))
  71. }
  72.  
  73. else if(dowhat == 3){
  74. console.log("Good Bye!")
  75. }
  76. }
  77. }
  78.  
  79. // var contact = new Contact("Joel", 91456151);
  80. // contact.getContactDetails();
  81.  
  82. // var a = new AddressBook();
  83. // a.getNumberofContacts();
  84. // a.getContactAt(2);
  85. // a.searchContact("Homer");
  86.  
  87. //3c
  88. var addbk = new AddressBook();
  89. addbk.myAddressBook();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement