001    package market.swing;
002    
003    import java.io.Serializable;
004    import java.util.Comparator;
005    
006    import market.UCustomer;
007    import market.statistics.Statistics;
008    
009    /**
010     * Compares {@link market.UCustomer UCustomers} by special criteria.
011     */
012    public class CmpCustomersNumbers implements Comparator, Serializable {
013    
014        public static final int DISCOUNT = 0;
015        public static final int SALES_VOLUME = 1;
016    
017        private int option;
018    
019        /**
020         * @param option <ul><li>0: Users are compared by their discount</li>
021         *                  <li>1: Users are compared by their volume of sales</li></ul>
022         */
023        public CmpCustomersNumbers(int option) {
024            this.option = option;
025        }
026    
027        /**
028         * The actual comparison.
029         * @param o1 the first customer.
030         * @param o2 the second customer.
031         * @return an int representing the result of the comparison.
032         */
033        public int compare(Object o1, Object o2) {
034            UCustomer u1 = (UCustomer)o1;
035            UCustomer u2 = (UCustomer)o2;
036            if (option == 0) {
037                return new Double(u1.getDiscount()).intValue() - new Double(u2.getDiscount()).intValue();
038            }
039            if (option == 1) {
040                int i1 = Statistics.getAllowableCustomerRevenue(u1);
041                int i2 = Statistics.getAllowableCustomerRevenue(u2);
042                return i1 - i2;
043            }
044            return 0;
045        }
046    }