-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContactBook.java
71 lines (59 loc) · 1.59 KB
/
ContactBook.java
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
/**
* contacts book
* autor Diego Alfaro
* github diegoalfarog
* @version 0.1
* date: 4/15/2021
*/
import java.util.ArrayList;
import java.util.List;
public class ContactBook {
private List<Contact> contacts;
public ContactBook() {
contacts = new ArrayList<Contact>();
}
public void addContact(Contact contact) {
this.contacts.add(contact);
}
public List<Object[]> showAllContacts() {
List<Object[]> contactsToInsertInTable = new ArrayList<Object[]>();
for (Contact contact : contacts) {
contactsToInsertInTable
.add(new Object[] { contacts.indexOf(contact)+1, contact.getName(), contact.getPhone(), contact.getEmail() });
}
return contactsToInsertInTable;
}
public Contact searchContact(String name) {
for (Contact contact : contacts) {
if (contact.getName().equalsIgnoreCase(name)) {
return contact;
}
}
return null;
}
public Contact searchContact(int index) {
return index > 0 && index <= contacts.size() ? contacts.get(index - 1) : null;
}
public boolean removeContact(String name) {
Contact toRemoveContact = searchContact(name);
if (toRemoveContact != null) {
contacts.remove(toRemoveContact);
return true;
}
return false;
}
public boolean removeContact(int index) {
Contact toRemoveContact = searchContact(index);
if (toRemoveContact != null) {
contacts.remove(toRemoveContact);
return true;
}
return false;
}
public void removeAllContact() {
contacts.clear();
}
public boolean isEmpty() {
return contacts.size() == 0;
}
}