package addresses;

public class Address implements Comparable {

    private int streetNumber;

    private String street;

    private String city;

    private int postCode;

    private String country;

     

    public Address(int streetNumber, String street, String city,

            int postCode, String country) {

        this.streetNumber = streetNumber;

        this.street = street;

        this.city = city;

        this.postCode = postCode;

        this.country = country;

    }

}

 

 

 

 

 

@Override

public boolean equals(Object obj) {

    if (this == obj)

        return true;

    if (obj == null)

         return false;

    if (getClass() != obj.getClass())

        return false;

    Address other = (Address) obj;

    if (city == null) {

        if (other.city != null)

            return false;

    } else if (!city.equals(other.city))

         return false;

    if (country == null) {

         if (other.country != null)

             return false;

    } else if (!country.equals(other.country))

        return false;

    if (postCode != other.postCode)

        return false;

    if (street == null) {

         if (other.street != null)

             return false;

    } else if (!street.equals(other.street))

         return false;

    if (streetNumber != other.streetNumber)

         return false;

    return true;

}

 

 

 

 

 

@Override

public int hashCode() {

    final int prime = 31;

    int result = 1;

    result = prime * result + ((city == null) ? 0 :

        city.hashCode());

    result = prime * result + ((country == null) ? 0 :

         country.hashCode());

    result = prime * result + postCode;

    result = prime * result + ((street == null) ? 0 :

        street.hashCode());

    result = prime * result + streetNumber;

    return result;

}

 

 

 

 

 

@Override

public int compareTo(Object o) {

    if (o instanceof Address) {

        Address other = (Address)o;

        return city.compareTo(other.getCity());

    } else {

        return 0;

    }

}

 

 

 

 

 

 

package addresses;

import java.util.ArrayList;

import java.util.List;

 

public class ListsMain {

     public static void main(String[] args) {

        List addresses = new ArrayList();

        Address a1 = new Address(799, "E Dragham",

             "Tucson", 85705, "USA");

        Address a2 = new Address(200, "Main Street",

              "Phoenix", 85123, "USA");

        Address a3 = new Address(100, "Main Street",

              "Seattle", 98104, "USA");

        Address a4 = new Address(8400 , "London Place",

               "Washington", 20521, "USA");

        Address a5 = new Address(8400 , "London Place",

               "Washington", 20521, "USA");

        addresses.add(a1);

        addresses.add(a2);

        addresses.add(a3);

        addresses.add(a4);

        addresses.add(a5);         

        System.out.println(addresses);

    }

}

 

 

 

 

 

 

public static void main(String[] args) {

    Set addresses = new HashSet();

    Address a1 = new Address(799, "E Dragham",

        "Tucson", 85705, "USA");

     Address a2 = new Address(200, "Main Street",

         "Phoenix", 85123, "USA");

     Address a3 = new Address(100, "Main Street",

         "Seattle", 98104, "USA");

     Address a4 = new Address(8400 , "London Place",

          "Washington", 20521, "USA");

     Address a5 = new Address(8400 , "London Place",

          "Washington", 20521, "USA");

    addresses.add(a1);

    addresses.add(a2);

    addresses.add(a3);

    addresses.add(a4);

    addresses.add(a5);       

    System.out.println("Size = " + addresses.size());

    System.out.println(addresses);

}

 

 

 

 

 

package addresses;

public class Person {

    String firstName;

    String lastName;

     

    public Person(String firstName, String lastName ) {

        this.firstName = firstName;

        this.lastName = lastName;

    }

    @Override

    public String toString() {

        return "Person [firstName=" + firstName +

            ", lastName=" + lastName + "]";

    }

 

    @Override

    public int hashCode() {

        final int prime = 31;

        int result = 1;

        result = prime * result +

           ((firstName == null) ? 0 : firstName.hashCode());

        result = prime * result +

           ((lastName == null) ? 0 : lastName.hashCode());

        return result;

    }

 

    @Override

    public boolean equals(Object obj) {

        if (this == obj)

            return true;

        if (obj == null)

             return false;

        if (getClass() != obj.getClass())

            return false;

        Person other = (Person) obj;

        if (firstName == null) {

            if (other.firstName != null)

                return false;

        } else if (!firstName.equals(other.firstName))

            return false;

        if (lastName == null) {

            if (other.lastName != null)

                return false;

        } else if (!lastName.equals(other.lastName))

            return false;

        return true;

    }      

}

 

 

 

 

 

package addresses;

 

import java.util.HashMap;

import java.util.Map;

 

public class MapMain {

    public static void main(String[] args) {

        Address a1 = new Address(799, "E Dragham",

            "Tucson", 85705, "USA");

        Address a2 = new Address(200, "Main Street",

            "Phoenix", 85123, "USA");

        Address a3 = new Address(100, "Main Street",

            "Seattle", 98104, "USA");

           

        Person p1 = new Person("Dane", "Cameron");

        Person p2 = new Person("James", "Smith");

        Person p3 = new Person("Keith", "Rogers");

        Person p4 = new Person("Owen", "Heart");

           

        Map people = new HashMap();

        people.put(p1, a3);

        people.put(p2, a1);

        people.put(p3, a2);

        people.put(p4, a1);

           

        System.out.println("Dane lives at " + people.get(p1));

        people.put(p1, a2);

        System.out.println("Now Dane lives at " + people.get(p1));

    }

}

 

 

 

 

 

public static void main(String[] args) {

    List addresses = new LinkedList();

    Address a1 = new Address(799, "E Dragham",

        "Tucson", 85705, "USA");

    addresses.add(a1);

    Person p1 = new Person("Dane", "Cameron");

    addresses.add(p1);

    Address a = (Address) addresses.get(1);

}

 

 

 

 

 

public static void main(String[] args) {

    List<Square> squares = new ArrayList<>();

    List<Circle> circles = new ArrayList<>();

    squares.add(new Square());

    circles.add(new Circle());

    printFirstElement(squares);

    printFirstElement(circles);

}    

private static void printFirstElement(List<Shape> shapes) {

    System.out.println(shapes.get(0));

}

 

 

 

 

 

public static void main(String[] args) {

    List<Address> addresses = new ArrayList<>();

    addresses.add(new Address(799, "E Dragham",

        "Tucson", 85705, "USA"));

    addresses.add(new Address(200, "Main Street",

        "Phoenix", 85123, "USA"));

    addresses.add(new Address(100, "Main Street",

        "Seattle", 98104, "USA"));

    addresses.add(new Address(8400 , "London Place",

        "Washington", 20521, "USA"));                

    for (Address a : addresses) {

        System.out.println(a);

    }

}

 

 

 

 

 

public static void main(String[] args) {

    List<Address> addresses = new ArrayList<>();

    addresses.add(new Address(799, "E Dragham",

        "Tucson", 85705, "USA"));

    addresses.add(new Address(200, "Main Street",

        "Phoenix", 85123, "USA"));

    addresses.add(new Address(100, "Main Street",

        "Seattle", 98104, "USA"));

    addresses.add(new Address(8400 , "London Place",

        "Washington", 20521, "USA"));          

    java.util.Iterator<Address> iter = addresses.iterator();

    while (iter.hasNext()) {

        Address a = iter.next();

        System.out.println(a);

    }

}