I'm working on a school project and I have the JUnit test almost here but I cannot for the life of me get this to work.
Contact looks like:
public class Contact {
private String contactID;
private String firstName;
private String lastName;
private String phoneNumber;
private String address;
public Contact (String contactID, String firstName, String lastName, String phoneNumber, String address)
{
//ContactTests Tests
//test to see if contact ID is NOT null or above 10 characters
if (contactID == null || contactID.length()>10)
{
throw new IllegalArgumentException("Invalid contact ID");
//not updating because says do not update it
}
//test to see if first name is NOT null or above 10 characters
if (firstName == null || firstName.length()>10)
{
throw new IllegalArgumentException("Invalid first name");
}
if (lastName == null || lastName.length()>10)
{
throw new IllegalArgumentException("Invalid last name");
}
//test to see if phone number is NOT null or not 10 characters
if (phoneNumber == null || phoneNumber.length()>10 || phoneNumber.length()<10)
{
throw new IllegalArgumentException("Invalid phone number");
}
//test to see if address is NOT null or more than 30 characters
if (address == null || address.length()>30)
{
throw new IllegalArgumentException("Invalid address");
}
//apply them
this.contactID = contactID;
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.address = address;
}
//return the variables
//used within tests
public String getContactID() {
return contactID;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getAddress() {
return address;
}
//for contactService need to create values here to get variables to set into the array
//used for First Name, Last Name, Phone Number, Address
//used within ContactServices
public void setFirstName (String firstName)
{
this.firstName = firstName;
}
public void setLastName (String lastName)
{
this.lastName = lastName;
}
public void setPhoneNumber (String phoneNumber)
{
this.phoneNumber = phoneNumber;
}
public void setAddress (String address)
{
this.address = address;
}
//test if two contacts are the same
//used in contactService and contactServiceTest
//geeksforgeeks.org/overriding-equals-method-in-java/ helped me in this idea of changing equals
//to becoming more complex
/*@Override
public boolean equals(Object test)
{
if (this == test)
{
return true;
}
if ((getClass() != test.getClass()) || (test == null))
{
return false;
}
Contact another = (Contact) test;
if (contactID == null)
{
if (another.contactID != null || (!contactID.equals(another.contactID)))
{
return false;
}
}
if (firstName == null)
{
if (another.firstName != null || (!firstName.equals(another.firstName)))
{
return false;
}
}
if (lastName == null)
{
if (another.lastName != null || (!lastName.equals(another.lastName)))
{
return false;
}
}
if (phoneNumber == null)
{
if (another.phoneNumber != null || (!phoneNumber.equals(another.phoneNumber)))
{
return false;
}
}
if (address == null)
{
if (another.address != null || (!address.equals(another.address)))
{
return false;
}
}
return true;
}*/
}
ContactService and ContactServiceTest look like;
import java.util.ArrayList;
import contact.Contact;
public class ContactService {
//will contain our list of contacts
//list was removed as working solely with array list now
private ArrayList<Contact> contacts;
public ContactService()
{
//beginning call for the array list
contacts = new ArrayList<>();
}
//need to have an add contact, remove contact and update contact feature
//set add contact to have all values
public boolean addContact(Contact contact)
{
boolean contactAlready = false;
//run through all the contacts in the list made
for (Contact contactList:contacts)
{
//test to see if already a contact
//if so make contactAlready true
if (contactList.equals(contact))
{
contactAlready = true;
}
}
//if not a contact add it as one
if (!contactAlready)
{
contacts.add(contact);
//after adding is now true
return true;
}
else
{
//ending false statement
return false;
}
}
//delete needed via contactID
public boolean deleteContact(String contactID)
{
//run through list of contacts
for (Contact contactList:contacts)
{
//if equals to contactID will remove and return
if (contactList.getContactID().equals(contactID))
{
//remove and return true
contacts.remove(contactList);
return true;
}
}
//else fail and return false
return false;
}
//update is trickiest due to needing to make sure still fits parameters
//"" means no change
public boolean updateContact(String contactID, String firstName, String lastName, String phoneNumber, String address)
{
//run through loop again
for (Contact contactList:contacts)
{
//if contactID matches, run through each with making sure not "" and meets requirements
//then return true as it did equal update.
if (contactList.getContactID().equals(contactID))
{
//set each of the values as long as meet's requirements nor empty
if(!firstName.equals("") && !(firstName.length()>10))
{
contactList.setFirstName(firstName);
}
if(!lastName.equals("") && !(lastName.length()>10))
{
contactList.setFirstName(lastName);
}
if(!phoneNumber.equals("") && (phoneNumber.length()==10))
{
contactList.setFirstName(phoneNumber);
}
if(!address.equals("") && !(address.length()>30))
{
contactList.setFirstName(address);
}
//return true as did update
return true;
}
}
//else fail and return false
return false;
}
}
ContactServiceTest
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import static org.junit.Assert.*;
import contact.Contact;
import contact.ContactService;
class ContactServiceTest {
//need to test add, delete and update
//templates
/*
* Contact("1413252", "Jane", "Doe", "4444444444", "Sample 24 Drive");
Contact("1309403", "Malleus", "Draconia", "2187123404", "Valley of Thorns");
Contact("9752319", "Vil", "Schoenheit", "9215501793", "Land of Proxynee");
*/
@Test
public void testAdd()
{
ContactService cs = new ContactService();
Contact contact = new Contact("1413252", "Jane", "Doe", "4444444444", "Sample 24 Drive");
assertTrue(cs.addContact(contact));
//assertEquals(true, cs.addContact(contact));
}
}
I would appreciate any help on this as I'm completely lost and I've gotten no help, sorry for the code dump I just wanted you to have a full picture of what's going on if something -or everything- is wrong.