AddressBook/src/Address.java

Notes

Purpose
The purpose of the AddressBook.Address Java class is to collect together the data relating to a person and their postal address.

Address Constructor
The Address constructor stores the supplied address fields:

    this.id = id;
    this.surname = surname;
    this.firstname = firstname;
    this.street = street;
    this.district = district;
    this.city = city;
    this.postcode = postcode;

getId, getSurname, getFirstname, getStreet, getDistrict, getCity and getPostcode
These method return the various fields comprising an address.

getFullname
getFullname returns the firstname field and the surname field separated by a space.

getFulladdress
getFulladdress returns the street field, the district field, the city field and the postcode field separated by spaces.

compareTo
compareTo is used to compare two addresses using the surname as the primary key, and the first name as the secondary key:

    Address address = (Address) object;
    int compare = this.surname.compareToIgnoreCase (address.surname);
    if (compare == 0)
    {
        compare = this.firstname.compareToIgnoreCase (address.firstname);
    }
    return (compare);

compareTo can be used to sort an array of addresses.

Complete Source Code


//
//  Address.java
//

package AddressBook;

public class Address
implements Comparable
{
    private String id = null;
    private String surname = null;
    private String firstname = null;
    private String street = null;
    private String district = null;
    private String city = null;
    private String postcode = null;

    public Address (String id, String surname, String firstname, String street, String district, String city, String postcode)
    {
        this.id = id;
        this.surname = surname;
        this.firstname = firstname;
        this.street = street;
        this.district = district;
        this.city = city;
        this.postcode = postcode;
    }

    public String getId ()
    {
        return this.id;
    }

    public String getSurname ()
    {
        return this.surname;
    }

    public String getFirstname ()
    {
        return this.firstname;
    }

    public String getStreet ()
    {
        return this.street;
    }

    public String getDistrict ()
    {
        return this.district;
    }

    public String getCity ()
    {
        return this.city;
    }

    public String getPostcode ()
    {
        return this.postcode;
    }

    public String getFullname ()
    {
        return this.firstname + " " + this.surname;
    }

    public String getFulladdress ()
    {
        return this.street + " " + this.district + " " + this.city + " " + this.postcode;
    }

    public int compareTo (Object object)
    {
        Address address = (Address) object;
        int compare = this.surname.compareToIgnoreCase (address.surname);
        if (compare == 0)
        {
            compare = this.firstname.compareToIgnoreCase (address.firstname);
        }
        return (compare);
    }

}