001package edu.pdx.cs410J.core; 002 003import java.util.*; 004 005/** 006 * Compares two boxes of ceral by their price 007 */ 008public class CerealComparator implements Comparator<Cereal> { 009 public int compare(Cereal o1, Cereal o2) { 010 double price1 = o1.getPrice(); 011 double price2 = o2.getPrice(); 012 013 if (price1 > price2) { 014 return 1; 015 } else if (price1 < price2) { 016 return -1; 017 } else { 018 return 0; 019 } 020 } 021 022 public static void main(String[] args) { 023 Set<Cereal> set = new TreeSet<Cereal>(new CerealComparator()); 024 set.add(new Cereal("Cap'n Crunch", 2.59)); 025 set.add(new Cereal("Trix", 3.29)); 026 set.add(new Cereal("Count Chocula", 2.59)); 027 set.add(new Cereal("Froot Loops", 2.45)); 028 029 // Print out the cereals 030 for (Cereal c : set) { 031 System.out.println(c); 032 } 033 } 034}