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