-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactService.java
More file actions
63 lines (53 loc) · 1.91 KB
/
ContactService.java
File metadata and controls
63 lines (53 loc) · 1.91 KB
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
package contact;
import java.util.ArrayList;
import java.util.List;
public class ContactService {
private List<ContactClass> contacts;
public ContactService() {
contacts = new ArrayList<>();
}
public void addContact(ContactClass contact) {
String contactId = contact.getContactID();
for (ContactClass existingContact : contacts) {
if (existingContact.getContactID().equals(contactId)) {
throw new IllegalArgumentException("Contact with ID " + contactId + " already exists.");
}
}
contacts.add(contact);
}
public void deleteContact(String contactId) {
ContactClass contactToRemove = null;
for (ContactClass contact : contacts) {
if (contact.getContactID().equals(contactId)) {
contactToRemove = contact;
break;
}
}
if (contactToRemove != null) {
contacts.remove(contactToRemove);
}
}
public void updateContact(String contactId, String firstName, String lastName, String phoneNumber, String address) {
ContactClass contact = getContact(contactId);
if (firstName != null) {
contact.setFirstName(firstName);
}
if (lastName != null) {
contact.setLastName(lastName);
}
if (phoneNumber != null) {
contact.setPhoneNumber(phoneNumber);
}
if (address != null) {
contact.setAddress(address);
}
}
public ContactClass getContact(String contactId) {
for (ContactClass contact : contacts) {
if (contact.getContactID().equals(contactId)) {
return contact;
}
}
throw new IllegalArgumentException("Contact with ID " + contactId + " does not exist.");
}
}