mirror of
https://github.com/didyouexpectthat/cs-320.git
synced 2025-08-20 05:53:16 -07:00
Add files via upload
This commit is contained in:
parent
4f20a69a1a
commit
c2cd95eaa1
4 changed files with 505 additions and 0 deletions
74
ContactService.java
Normal file
74
ContactService.java
Normal file
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
Cody Cook
|
||||
CS-320-H7022 Software Test Automation & QA 23EW2
|
||||
2023/11/11
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ContactService {
|
||||
|
||||
private List<Contact> contacts;
|
||||
|
||||
public ContactService() {
|
||||
// Initialize the list of contacts
|
||||
this.contacts = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addContact(Contact contact) {
|
||||
// Check if a contact with the same ID already exists
|
||||
if (getContactById(contact.getId()) != null) {
|
||||
throw new IllegalArgumentException("Contact with the same ID already exists.");
|
||||
}
|
||||
// Add the contact to the list
|
||||
contacts.add(contact);
|
||||
}
|
||||
|
||||
public void deleteContact(String contactId) {
|
||||
// Find and remove the contact with the specified contactId
|
||||
boolean removed = contacts.removeIf(contact -> contact.getId().equals(contactId));
|
||||
if (!removed) {
|
||||
throw new IllegalArgumentException("Contact with ID " + contactId + " not found.");
|
||||
}
|
||||
}
|
||||
|
||||
public void updateContact(String contactId, String newFirstName, String newLastName, String newPhone,
|
||||
String newAddress) {
|
||||
// Find the contact by ID
|
||||
Contact contactToUpdate = getContactById(contactId);
|
||||
|
||||
if (contactToUpdate == null) {
|
||||
throw new IllegalArgumentException("Contact with ID " + contactId + " not found.");
|
||||
}
|
||||
|
||||
// Update the contact's fields if new values are provided
|
||||
if (newFirstName != null) {
|
||||
contactToUpdate.setFirstName(newFirstName);
|
||||
}
|
||||
if (newLastName != null) {
|
||||
contactToUpdate.setLastName(newLastName);
|
||||
}
|
||||
if (newPhone != null) {
|
||||
contactToUpdate.setPhone(newPhone);
|
||||
}
|
||||
if (newAddress != null) {
|
||||
contactToUpdate.setAddress(newAddress);
|
||||
}
|
||||
}
|
||||
|
||||
public Contact getContactById(String contactId) {
|
||||
// Find and return the contact with the specified ID, or null if not found
|
||||
for (Contact contact : contacts) {
|
||||
if (contact.getId().equals(contactId)) {
|
||||
return contact;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Contact> getAllContacts() {
|
||||
// Return a list of all contacts
|
||||
return new ArrayList<>(contacts);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue