Re: Comparator in the driver
On Wed, 30 Apr 2008 07:51:59 -0400, Matt Humphrey wrote:
What does the ContactInfo class look like?
Due to the requirements, I can only use a Comparator for the last name
(rather than for City as I had mentioned in my post). I'd like to move
the Comparator outside of the driver, but for now it's fine where it is.
I cleaned up the driver a bit, here's the Comparator from the driver and
ContactInfo:
thufir@arrakis:~/bcit-comp2611-lab3$
thufir@arrakis:~/bcit-comp2611-lab3$ cat src/a00720398/labs/Lab3.java -n
| head -n 48 | tail -n 13
36
37 static final Comparator<Guest> BY_NAME =
new Comparator<Guest>(){
38 public int compare(Guest g1, Guest g2){
39 ContactInfo c1 = g1.getContactInfo();
40 ContactInfo c2 = g2.getContactInfo();
41 System.out.println("******************");
42 String s1 = c1.getLastName();
43 String s2 = c2.getLastName();
44 int foo = s1.compareTo(s2);
45 System.out.println(foo);
46 return 0;
47 }
48 };
thufir@arrakis:~/bcit-comp2611-lab3$
thufir@arrakis:~/bcit-comp2611-lab3$ cat src/a00720398/data/
ContactInfo.java
/**
* ContactInfo.java
*
*/
package a00720398.data;
public class ContactInfo
{
private String
lastName = "foo",
firstName = "foo",
phoneNumber = "foo",
emailAddress = "foo",
street = "foo",
city = "foo",
province = "foo",
postalCode = "foo",
country = "foo";
/**forbidden constructor*/
private ContactInfo (){}
/**only this constructor is currently allowed*/
public ContactInfo (String[] guestData, String[] addressData){
lastName = (guestData[0]);
firstName = (guestData[1]);
phoneNumber = (guestData[2]);
emailAddress = (guestData[3]);
street = addressData[0];
city = addressData[1];
province = addressData[2];
postalCode = addressData[3];
country = addressData[4];
}
public void setLastName(String lastName){this.lastName =
lastName;}
public void setFirstName(String firstName){this.firstName =
firstName;}
public void setPhoneNumber(String phoneNumber){this.phoneNumber =
phoneNumber;}
public void setEmailAddress(String emailAddress)
{this.emailAddress = emailAddress;}
public void setStreet(String street){this.street = street;}
public void setCity(String city){this.city = city;}
public void setProvince(String province){this.province =
province;}
public void setPostalCode(String postalCode){this.postalCode =
postalCode;}
public void setCountry(String country){this.country = country;}
public String getFirstName(){ return firstName;}
public String getLastName(){return lastName;}
public String getPhoneNumber(){return phoneNumber;}
public String getEmailAddress(){return emailAddress;}
public String getStreet(){return street;}
public String getCity(){return city;}
public String getProvince(){return province;}
public String getPostalCode(){return postalCode;}
public String getCountry(){return country;}
public int compareTo(ContactInfo c2) {return getCity().compareTo
(c2.getCity());}
public int hashCode(){return 31*getFirstName().hashCode() +
getLastName().hashCode();}
public boolean equals(Object o){
if (!(o instanceof ContactInfo))
return false;
ContactInfo c = (ContactInfo)o; //only cast if
it's a ContactInfo
return c.getFirstName().equals(getFirstName()) &&
c.getLastName().equals
(getLastName());
}
public String toString (){
return "\nCONTACT INFO" +
"\nfirstName:\t" + getFirstName() +
"\nlastname:\t" + getLastName() +
"\nphone number:\t" + getPhoneNumber () +
"\ne-mail address:\t" + getEmailAddress () +
"\nstreet:\t\t" + getStreet() +
"\ncity:\t\t" + getCity() +
"\nprovince:\t" + getProvince() +
"\npostal code:\t" + getPostalCode() +
"\ncountry:\t" + getCountry() + "\n";
}
}
thufir@arrakis:~/bcit-comp2611-lab3$
thufir@arrakis:~/bcit-comp2611-lab3$
Guest is a very sill class, it just holds ContactInfo, for what it's
worth.
thanks,
Thufir