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<Object>, Serializable {
013    
014        /**
015             * ID for serialization.
016             */
017            private static final long serialVersionUID = 3439845809286224151L;
018            
019            public static final int DISCOUNT = 0;
020        public static final int SALES_VOLUME = 1;
021    
022        private int option;
023    
024        /**
025         * @param option <ul><li>0: Users are compared by their discount</li>
026         *                  <li>1: Users are compared by their volume of sales</li></ul>
027         */
028        public CmpCustomersNumbers(int option) {
029            this.option = option;
030        }
031    
032        /**
033         * The actual comparison.
034         * @param o1 the first customer.
035         * @param o2 the second customer.
036         * @return an int representing the result of the comparison.
037         */
038        public int compare(Object o1, Object o2) {
039            UCustomer u1 = (UCustomer) o1;
040            UCustomer u2 = (UCustomer) o2;
041            if (option == 0) {
042                return new Double(u1.getDiscount()).intValue() - new Double(u2.getDiscount()).intValue();
043            }
044            if (option == 1) {
045                int i1 = Statistics.getAllowableCustomerRevenue(u1);
046                int i2 = Statistics.getAllowableCustomerRevenue(u2);
047                return i1 - i2;
048            }
049            return 0;
050        }
051    }