-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactService.java
More file actions
30 lines (24 loc) · 1.15 KB
/
ContactService.java
File metadata and controls
30 lines (24 loc) · 1.15 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
import java.util.HashMap;
import java.util.Map;
public class ContactService {
private final Map<String, Contact> contacts = new HashMap<>();
public void addContact(Contact contact) {
if (contacts.containsKey(contact.getContactId())) throw new IllegalArgumentException("Contact ID already exists");
contacts.put(contact.getContactId(), contact);
}
public void deleteContact(String contactId) {
if (!contacts.containsKey(contactId)) throw new IllegalArgumentException("Contact ID not found");
contacts.remove(contactId);
}
public void updateContact(String contactId, String firstName, String lastName, String phone, String address) {
Contact contact = contacts.get(contactId);
if (contact == null) throw new IllegalArgumentException("Contact ID not found");
if (firstName != null) contact.setFirstName(firstName);
if (lastName != null) contact.setLastName(lastName);
if (phone != null) contact.setPhone(phone);
if (address != null) contact.setAddress(address);
}
public Contact getContact(String contactId) {
return contacts.get(contactId);
}
}