In: Computer Science
Create a file and save it as Contact.java
The class would look like this as below (I have added cooments as of to mention what each function would do:
//Create a class that implements the Comparable interface. As the comparision is with another Contact object, we specify Comparable<Contact>.
public class Contact implements Comparable<Contact>
{
//define the private variables as required
private String firstName;
private String lastName;
private String phoneNumber;
private String streetAddress;
private String city;
private String state;
//public constructor with all fields
public Contact(String firstName, String lastName, String phoneNumber, String streetAddress, String city, String state) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
}
//public constructor with name and phoneNumber
public Contact(String firstName, String lastName, String phoneNumber) {
//Calling another contructor and passing the supplied parameters. For others, passing empty string
this(firstName,lastName,phoneNumber,"","","");
}
//Getters
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getPhoneNumber(){
return phoneNumber;
}
public String getStreetAddress(){
return streetAddress;
}
public String getCity(){
return city;
}
public String getState(){
return state;
}
//update method to change information
public void update(String firstName, String lastName, String phoneNumber, String streetAddress, String city, String state) {
this.firstName = firstName;
this.lastName = lastName;
this.phoneNumber = phoneNumber;
this.streetAddress = streetAddress;
this.city = city;
this.state = state;
}
//check if the two contacts are equal
public boolean equals(Object obj){
if(obj instanceof Contact) {
Contact c = (Contact)obj;
if(this.firstName.equals(c.getFirstName()) && this.lastName.equals(c.getLastName()))
return true;
else
return false;
}
return false;
}
//Contact to string
public String toString(){
StringBuilder builder = new StringBuilder();
builder.append(firstName);
builder.append(" ");
builder.append(lastName);
builder.append(" Phone number: ");
builder.append(phoneNumber);
builder.append(" ");
builder.append(System.getProperty("line.separator")); //to create a new line
builder.append(streetAddress);
builder.append(System.getProperty("line.separator")); //to create a new line
builder.append(city);
builder.append(",");
builder.append(state);
return builder.toString();
}
public int compareTo(Contact another){
if(!lastName.equals(another.getLastName())){
if(lastName.compareTo(another.getLastName()) > 1)
return 1;
else if(lastName.compareTo(another.getLastName()) < 1)
return -1;
}
else if(!firstName.equals(another.getFirstName())) {
if (firstName.compareTo(another.getFirstName()) > 1)
return 1;
else if (firstName.compareTo(another.getFirstName()) < 1)
return -1;
}
return 0;
}
//main program to run the class
public static void main(String[] args){
Contact contact1 = new Contact("Albert","Einstein","1111111111","101, some avenue","Some city", "NY");
Contact contact2 = new Contact("Isaac","Newton","1231231231");
Contact contact3 = new Contact("Albert","Einstein","1111111111");
//Print the contact
System.out.println(contact1);
//Check if two contacts are equal
System.out.println(contact1.equals(contact2)); //false
System.out.println(contact1.equals(contact3)); //true
//Compare two contacts
System.out.println(contact1.compareTo(contact2)); //-1
System.out.println(contact1.compareTo(contact3)); //0
//update contact
contact1.update("Albert","Einstein","1011111101","101, some avenue","Some city", "NY");
System.out.println(contact1);
//test for Getters
System.out.println(contact1.getFirstName());
System.out.println(contact1.getLastName());
System.out.println(contact1.getPhoneNumber());
System.out.println(contact1.getStreetAddress());
System.out.println(contact1.getCity());
System.out.println(contact1.getState());
}
}