-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddressBook.js
201 lines (183 loc) · 6.1 KB
/
AddressBook.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
console.log("------------------------------Welcome to the Address book program-----------------------------");
const regName = /^[A-Z][a-z]{2,}$/;
const regAddress = /^[A-Z]{1}[a-z]{3,}/;
const regZip = /^[0-9]{6}$/;
const regPhoneNo = /^[9][1][6-9]{1}[0-9]{9}$/;
const regEmail = /^[A-Za-z0-9]+([-\\\\.\\\\+\\\\_][0-9A-Za-z]+)*[@][A-Za-z0-9]+.[a-zA-Z]{2,4}([\\\\.\\\\,][a-z]{2,3})?$/;
let personArray = new Array();
class Person {
set firstName(firstName) {
if (regName.test(firstName))
this.perFirstName = firstName;
else throw "Invalid first name ";
}
get firstName() {
return this.perFirstName;
}
set lastName(lastName) {
if (regName.test(lastName))
this.perLastName = lastName;
else throw "Invalid last name";
}
get lastName() {
return this.perLastName;
}
set address(address) {
if (regAddress.test(address))
this.perAddress = address;
else throw "Invalid address";
}
get address() {
return this.perAddress;
}
set city(city) {
if (regAddress.test(city))
this.perCity = city;
else throw "Invalid city name";
}
get city() {
return this.perCity;
}
set state(state) {
if (regAddress.test(state))
this.perState = state;
else throw "Invalid state";
}
get state() {
return this.perState;
}
set zip(zip) {
if (regZip.test(zip))
this.perZip = zip;
else throw "Invalid zip code";
}
get zip() {
return this.perZip;
}
set phoneNumber(phoneNumber) {
if (regPhoneNo.test(phoneNumber))
this.perPhoneNumber = phoneNumber;
else throw "Invalid phone number";
}
get phoneNumber() {
return this.perPhoneNumber;
}
set email(email) {
if (regEmail.test(email))
this.perEmail = email;
else throw "Invalid email";
}
get email() {
return this.perEmail;
}
constructor(firstName, lastName, address, city, state, zip, phoneNumber, email) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
this.phoneNumber = phoneNumber;
this.email = email;
}
toString() {
return "First name: " + this.firstName + "\nLast name: " + this.lastName + "\nAddress: " + this.address + "\nCity: " + this.city + "\nState: " + this.state + "\nZip: " + this.zip + "\nPhone number: " + this.phoneNumber + "\nEmail: " + this.email;
}
}
function editContact(firstName, choice, updatedValue) {
let person = firstName;
switch (choice) {
case "address": let address = updatedValue;
person.address = address;
break;
case "city": let city = updatedValue;
person.city = city;
break;
case "state": let state = updatedValue;
person.state = state;
break;
case "zip": let zip = updatedValue;
person.zip = zip;
break;
case "phone number": let phoneNumber = updatedValue;
person.phoneNumber = phoneNumber;
break;
case "email": let email = updatedValue;
person.email = email;
break;
default:
throw "Invalid choice";
}
}
function deleteContact(firstName) {
personArray.forEach(person => {
if (person.firstName == firstName)
personArray.pop();
});
}
let personExist;
function findPerson(firstName) {
personArray.forEach(person => {
if (person.firstName == firstName)
personExist = person;
});
if (personExist != null)
console.log("Contact match");
else
throw "Contact haven't match";
}
function addPerson(person) {
if (personArray == null) {
personArray.push(person);
}
personArray.forEach(everyPerson => {
if (everyPerson.firstName == person.firstName)
personExist = everyPerson;
});
if (personExist == null)
personArray.push(person);
else
throw "Person exists";
}
function searchPerson(location, search) {
switch (location) {
case "city":
console.log(personArray.filter(person => person.city == search));
case "state":
console.log(personArray.filter(person => person.state == search));
}
}
function countPerson(location, view) {
switch (location) {
case "city":
console.log(personArray.filter(person => person.city == view).reduce(count => count + 1, 0));
case "state":
console.log(personArray.filter(person => person.state == view).reduce(count => count + 1, 0));
}
}
try {
let personObj1 = new Person("Usha", "Ahirwar", "Shivpur", "Bangalore", "Uttar", 560069, 917398033321, "[email protected]");
let personObj2 = new Person("Aditya", "Bharti", "Ecovillage", "Varanasi", "Noida", 560063, 917398031234, "[email protected]");
let personObj3 = new Person("Tusar", "Patel", "Panchsheel", "GreaterNoida", "UttarPradesh", 561234, 917312371234, "[email protected]");
addPerson(personObj1);
addPerson(personObj2);
addPerson(personObj3);
console.log(personArray);
editContact("Aditya", "state", "Delhi");
deleteContact("Aditya");
console.log("Number of Contacts : " + personArray.reduce(count => count + 1, 0));
findPerson("Usha");
searchPerson("state", "Uttar");
countPerson("city", "Bangalore")
console.log("Sort By Name" + personArray.sort((a, b) => a.firstName.localeCompare(b.firstName)));
console.log("Sort By ZipCode" + personArray.sort((a, b) => a.zip === (b.zip)));
console.log("Sort By City" + personArray.sort((a, b) => a.city.localeCompare(b.city)));
console.log("Sort By State" + personArray.sort((a, b) => a.state.localeCompare(b.state)));
} catch (Exception) {
console.log(Exception);
}
/*Ability to sort the entries
in the address book by
City, State, or Zip - Write functions to sort person by City, State or
Zip
- Use Array Functions of filter, map, reduce, etc to*/