Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var readline = require("readline-sync");
- //3a
- class Contact{
- constructor(name, mobileNumber){
- this.name = name;
- this.mobileNumber = mobileNumber;
- }
- getContactDetails(){
- console.log("Name: " + this.name + "\nMobile Number: " + this.mobileNumber);
- }
- }
- //3b
- class AddressBook{
- //i
- constructor() {
- this.myContact = []; //class property
- this.myContact.push(new Contact("Homer", 98849959));
- this.myContact.push(new Contact("Marge", 84774744));
- this.myContact.push(new Contact("Lisa", 86994994));
- this.myContact.push(new Contact("Maggie", 94775883));
- this.myContact.push(new Contact("Bart", 88838848));
- } //end constructor
- //ii
- getNumberofContacts(){
- console.log(this.myContact.length);
- }
- //iii
- getContactAt(index){
- console.log(this.myContact[index]);
- }
- //iv
- searchContact(search){
- for(var i = 0; i < this.myContact.length; i++){
- if(search != this.myContact[i].name){
- i++
- }
- else{
- console.log(this.myContact[i].mobileNumber);
- }
- }
- }
- myAddressBook(){
- var dowhat = readline.question("Personal Addressbook\n--------------------------\n(1) Show All Contacts\n(2) Search Contact\n(3) Exit\n>> ")
- if(dowhat == 1){
- var i = 0
- while(i >= 0 || i < this.myContact.length){
- console.log("-----------------------------\nContact " + (i + 1) + " of 5\n-----------------------------\n" + "Name: " + this.myContact[i].name + "\nMobile Number: " + this.myContact[i].mobileNumber + "\n-----------------------------");
- var nextaction = readline.question("(Press N for next contact, P for previous contact, X to exit)\n>> ");
- if(nextaction == "N" || nextaction == "n"){
- i++
- }
- if(nextaction == "P" || nextaction == "p"){
- i--
- }
- if(nextaction == "X" || nextaction == "x"){
- this.myAddressBook()
- }
- }
- }
- else if(dowhat == 2){
- var s = readline.question("Enter the name of the contact: ");
- console.log(s + "'s mobile number is " + this.searchContact(s))
- }
- else if(dowhat == 3){
- console.log("Good Bye!")
- }
- }
- }
- // var contact = new Contact("Joel", 91456151);
- // contact.getContactDetails();
- // var a = new AddressBook();
- // a.getNumberofContacts();
- // a.getContactAt(2);
- // a.searchContact("Homer");
- //3c
- var addbk = new AddressBook();
- addbk.myAddressBook();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement