@@ -79,7 +79,8 @@ const rl = readline.createInterface({ input, output });
79
79
80
80
const contacts = [
81
81
{ id : 1 , name : "Juan" , phone : 1234560987 } ,
82
- { id : 1 , name : "Pedro" , phone : 1234560987 } ,
82
+ { id : 1 , name : "Juan" , phone : 1222543388 } ,
83
+ { id : 2 , name : "Pedro" , phone : 1255566427 } ,
83
84
] ;
84
85
85
86
const ops = [
@@ -89,34 +90,126 @@ const ops = [
89
90
{ id : 4 , name : "Delete" } ,
90
91
] ;
91
92
93
+ const showAgenda = ( arr ) => {
94
+ console . log ( `
95
+ *****************************************************
96
+ * CONTACT LIST *
97
+ *****************************************************` ) ;
98
+ arr . map ( ( contact ) =>
99
+ console . log ( ` * Name: ${ contact . name }
100
+ * Phone: +57 ${ contact . phone }
101
+ -----------------------------------------------------` )
102
+ ) ;
103
+ } ;
104
+
105
+ const isExit = async ( op ) => {
106
+ const answer = await rl . question ( `
107
+ *****************************************************
108
+ * *
109
+ * Please select one of the following options: *
110
+ * *
111
+ * 1: Go back and try again *
112
+ * 2: Go to the Main Menu *
113
+ * 3: Show contact list and exit *
114
+ * 0: Exit *
115
+ * *
116
+ *****************************************************
117
+ * ` ) ;
118
+
119
+ if ( + answer === 0 ) {
120
+ rl . close ( ) ;
121
+ } else if ( + answer === 1 ) {
122
+ op ( ) ;
123
+ } else if ( + answer === 2 ) {
124
+ agenda ( ) ;
125
+ } else {
126
+ showAgenda ( contacts ) ;
127
+ rl . close ( ) ;
128
+ }
129
+ } ;
130
+
92
131
const searchContact = async ( ) => {
93
132
console . log ( `
94
133
*****************************************************
95
134
* SEARCH CONTACT *
96
- *****************************************************
97
- ` ) ;
98
- const contactInput = await rl . question ( "Contact name? " ) ;
135
+ *****************************************************` ) ;
136
+ const contactInput =
137
+ await rl . question ( ` * Contact name? *
138
+ * ` ) ;
99
139
100
- const contact = contacts . find ( ( contact ) => {
101
- return contact . name === contactInput ;
140
+ const contactsFound = contacts . filter ( ( contact ) => {
141
+ return contact . name . toLowerCase ( ) === contactInput . toLowerCase ( ) ;
102
142
} ) ;
103
143
104
- if ( ! contact ) {
105
- console . log ( "Contact not found, please try again" ) ;
106
- searchContact ( ) ;
144
+ if ( contactsFound . length === 0 ) {
145
+ console . log ( ` * *
146
+ * Contact not found. *` ) ;
147
+ isExit ( searchContact ) ;
107
148
} else {
108
- console . log ( contact ) ;
149
+ showAgenda ( contactsFound ) ;
150
+ isExit ( searchContact ) ;
109
151
}
110
-
111
- agenda ( ) ;
112
152
} ;
113
153
114
- const addContact = ( ) => {
115
- console . log ( "Add" ) ;
154
+ const addContact = async ( ) => {
155
+ console . log ( `
156
+ *****************************************************
157
+ * ADD CONTACT *
158
+ *****************************************************` ) ;
159
+ const contactInputName =
160
+ await rl . question ( ` * Contact name? *
161
+ * ` ) ;
162
+ const contactInputPhone =
163
+ await rl . question ( ` * *
164
+ * Phone number? *
165
+ * ` ) ;
166
+
167
+ const nameHasNumbers = / \d / . test ( contactInputName ) ;
168
+ const phoneHasLetters = / [ a - z A - Z ] / g. test ( contactInputPhone ) ;
169
+
170
+ if ( contactInputName === "" || nameHasNumbers ) {
171
+ console . log ( `
172
+ * Contact name is not valid. *
173
+ * Only letters allowed. *` ) ;
174
+ isExit ( addContact ) ;
175
+ } else if (
176
+ contactInputPhone === "" ||
177
+ phoneHasLetters ||
178
+ contactInputPhone . length > 10
179
+ ) {
180
+ console . log ( `
181
+ * Contact phone number is not valid. *
182
+ * Only numbers allowed, has to be 10 digits *` ) ;
183
+ isExit ( addContact ) ;
184
+ } else {
185
+ const newContact = {
186
+ id : contacts . length + 1 ,
187
+ name : contactInputName ,
188
+ phone : + contactInputPhone ,
189
+ } ;
190
+
191
+ contacts . push ( newContact ) ;
192
+ console . log ( ` *
193
+ * The contact ${ newContact . name } has been added sucessfully *` ) ;
194
+ isExit ( addContact ) ;
195
+ }
116
196
} ;
117
- const updateContact = ( ) => {
118
- console . log ( "Update" ) ;
197
+
198
+ const updateContact = async ( ) => {
199
+ console . log ( `
200
+ *****************************************************
201
+ * UPDATE CONTACT *
202
+ *****************************************************` ) ;
203
+ const contactInputData =
204
+ await rl . question ( ` * Contact to update? Type name or phone number
205
+ * ` ) ;
206
+
207
+ const filteredContact = contacts . find ( ( contact ) => {
208
+ contact . name === contactInputData || contact . phone === contactInputData ;
209
+ } ) ;
210
+ console . log ( ` * Updating contact ${ contactInputData } ` ) ;
119
211
} ;
212
+
120
213
const deleteContact = ( ) => {
121
214
console . log ( "Delete" ) ;
122
215
} ;
@@ -134,8 +227,10 @@ async function agenda() {
134
227
* 2: Add Contact *
135
228
* 3: Update Contact *
136
229
* 4: Delete Contact *
230
+ * 5: Show contact list *
137
231
* *
138
232
* 0: Exit program *
233
+ * *
139
234
*****************************************************
140
235
141
236
* Insert the operation number or 0 to exit: `
@@ -153,6 +248,10 @@ async function agenda() {
153
248
case 4 :
154
249
deleteContact ( ) ;
155
250
break ;
251
+ case 5 :
252
+ showAgenda ( contacts ) ;
253
+ isExit ( ) ;
254
+ break ;
156
255
case 0 :
157
256
rl . close ( ) ;
158
257
break ;
0 commit comments