Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this milestone, you will focus on delivering the contact services. The purpose of these services is to add, update, and delete contact objects within

For this milestone, you will focus on delivering the contact services. The purpose of these services is to add, update, and delete contact objects within the application.
The contact service uses in-memory data structures to support storing contacts (no database required). In addition, there is no user interface for this milestone. You will verify the contact service through JUnit tests. The contact service contains a contact object along with the contact service. The requirements are outlined below.
Contact Class Requirements
The contact object shall have a required unique contact ID string that cannot be longer than 10 characters. The contact ID shall not be null and shall not be updatable.
The contact object shall have a required firstName String field that cannot be longer than 10 characters. The firstName field shall not be null.
The contact object shall have a required lastName String field that cannot be longer than 10 characters. The lastName field shall not be null.
The contact object shall have a required phone String field that must be exactly 10 digits. The phone field shall not be null.
The contact object shall have a required address field that must be no longer than 30 characters. The address field shall not be null.
Contact Service Requirements
The contact service shall be able to add contacts with a unique ID.
The contact service shall be able to delete contacts per contact ID.
The contact service shall be able to update contact fields per contact ID. The following fields are updatable:
firstName
lastName
Number
Address
To complete this project, you must submit a Contact Service zipped folder containing the following deliverables:
Contact.java
ContactService.java
ContactTest.java
ContactServiceTest.java
import java.util.HashMap;
public class ContactService {
private final HashMap contacts = new HashMap<>();
public void addContact(Contact contact){
if (contacts.containsKey(contact.getFirstName())){
throw new IllegalArgumentException("Contact with this ID already exists");
}
contacts.put(contact.getFirstName(), contact);
}
public void deleteContact(String contactID){
contacts.remove(contactID);
}
public void updateContact(String contactID, String newFirstName, String newLastName, String newPhone, String newAddress){
Contact contact = contacts.get(contactID);
if (contact == null){
throw new IllegalArgumentException("Contact not found");
}
contact.setFirstName(newFirstName);
contact.setLastName(newLastName);
contact.setPhone(newPhone);
contact.setAddress(newAddress);
}
public Contact getContact(String contactID){
return contacts.get(contactID);
}
}
public class Contact {
private final String contactID; // not updatable
private String firstName;
private String lastName;
private String phone;
private String address;
public Contact(String contactID, String firstName, String lastName, String phone, String address){
if (contactID == null || firstName == null || lastName == null || phone == null || address == null){
throw new IllegalArgumentException("Fields cannot be null");
}
if (contactID.length()>10|| firstName.length()>10|| lastName.length()>10|| phone.length()!=10|| address.length()>30){
throw new IllegalArgumentException("Invalid field lengths");
}
this.contactID = contactID;
this.firstName = firstName;
this.lastName = lastName;
this.phone = phone;
this.address = address;
}
// Getters and Setters for updatable fields
// Omitted getter for contactID since it's not updatable
public String getFirstName(){
return firstName;
}
public void setFirstName(String firstName){
if (firstName == null || firstName.length()>10){
throw new IllegalArgumentException("Invalid first name");
}
this.firstName = firstName;
}
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
if (lastName == null || lastName.length()>10){
throw new IllegalArgumentException("Invalid last name");
}
this.lastName = lastName;
}
public String getPhone(){
return phone;
}
public void setPhone(String phone){
if (phone == null || phone.length()!=10){
throw new IllegalArgumentException("Invalid phone");
}
this.phone = phone;
}
public String getAddress(){
return address;
}
public void setAddress(String address){
if (address == null || address.length()>30){
throw new IllegalArgumentException("Invalid address");
}
this.address = address;
}
}
Please help with test classes.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Understand the accounting for investments in debt securities.

Answered: 1 week ago

Question

Draw a schematic diagram of I.C. engines and name the parts.

Answered: 1 week ago

Question

What are the potential limitations of group discussion?

Answered: 1 week ago