Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Java Help > Re: Comparator ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 11 Topic 16040 of 16078
Post > Topic >>

Re: Comparator in the driver

by thufir <hawat.thufir@[EMAIL PROTECTED] > Apr 30, 2008 at 12:35 PM

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@[EMAIL PROTECTED]
 
thufir@[EMAIL PROTECTED]
 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@[EMAIL PROTECTED]
  
thufir@[EMAIL PROTECTED]
 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@[EMAIL PROTECTED]
 
thufir@[EMAIL PROTECTED]
 



Guest is a very sill class, it just holds ContactInfo, for what it's 
worth.

thanks,

Thufir




 11 Posts in Topic:
Comparator in the driver
thufir <hawat.thufir@[  2008-04-30 08:11:35 
Re: Comparator in the driver
"Matt Humphrey"  2008-04-30 07:51:59 
Re: Comparator in the driver
thufir <hawat.thufir@[  2008-04-30 12:35:32 
Re: Comparator in the driver
thufir <hawat.thufir@[  2008-04-30 12:51:54 
Re: Comparator in the driver
Lew <lew@[EMAIL PROTEC  2008-04-30 21:15:06 
Re: Comparator in the driver
Roedy Green <see_websi  2008-04-30 13:43:44 
Re: Comparator in the driver
Thufir <hawat.thufir@[  2008-04-30 16:29:40 
Re: Comparator in the driver
Roedy Green <see_websi  2008-05-02 10:56:20 
Re: Comparator in the driver
thufir <hawat.thufir@[  2008-05-01 17:01:32 
Re: Comparator in the driver
Lew <lew@[EMAIL PROTEC  2008-05-01 18:23:34 
Re: Comparator in the driver
thufir <hawat.thufir@[  2008-05-02 04:57:13 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri May 16 11:41:00 CDT 2008.