001package edu.pdx.cs410J.core;
002
003import edu.pdx.cs410J.lang.*;
004import java.util.*;
005
006/**
007 * This class is a <code>Comparator</code> that compares
008 * <code>Person</code>s based on their shoe size.
009 */
010public class PersonComparator implements Comparator<Person> {
011
012  public int compare(Person o1, Person o2) {
013    double size1 = o1.shoeSize();
014    double size2 = o2.shoeSize();
015
016    if (size1 > size2) {
017      return 1;
018
019    } else if (size1 < size2) {
020      return -1;
021
022    } else {
023      return 0;
024    }
025  }
026
027  /**
028   * Creates a set of <code>Person</code>s and prints out the
029   * contents.
030   */
031  public static void main(String[] args) {
032    Set<Person> set = new TreeSet<Person>(new PersonComparator());
033    set.add(new Person("Quan", 10.5));
034    set.add(new Person("Jerome", 11.0));
035    set.add(new Person("Dave", 10.5));
036    set.add(new Person("Nandini", 8.0));
037
038    // Print out the people
039    Iterator iter = set.iterator();
040    while (iter.hasNext()) {
041      Person p = (Person) iter.next();
042      System.out.println(p);
043    }
044  }
045
046}