Results 1 to 5 of 5
  1. #1
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default [SOLVED] [newbie] compareTo that returns an Enum

    I would like to create an implementation of the compareTo method that returns an Enum, rather than an Integer, since an Enum is more self-documenting generally.

    Is it possible to override the existing interface compareTo, whilst not loosing the ability to use Arrays.sort?

    I'm aware that this is a forum named 'New to Java' so please just let me know if I'm thinking too laterally :)

    :D

    Java Code:
    public class Employee extends Person implements Comparable<Employee> {
    ...
    
    @Override
    	public int compareTo(Employee anotherEmployee) {
    		//TODO Introduce some seniority rules	
    		if (_salary < anotherEmployee.Salary()) return CompareType.SMALLER;
    		if (_salary > anotherEmployee.Salary()) return CompareType.GREATER;
    		return CompareType.EQUAL;
    	}
    ...
    
    package homenetwork.bkr.training;
    
    public enum CompareType {SMALLER, GREATER, EQUAL}
    
    ...
    This is how I call it:
    
    Employee[] staff = new Employee[3];
    staff[0] = new Employee("Harry Jones", 40000, 1999, 10,1);
    staff[1] = new Employee("Larry Brown", 40000, 1999, 10,2);
    staff[2] = new Employee("Joe Cocks", 30000, 2008, 10,2);
    		
    		if (staff[0].compareTo(staff[1]) == 0) 
    			System.out.println("harry and larry have the same salary.");
    		else
    			System.out.println("harry and larry do NOT have the same salary.");
    		
    		Arrays.sort(staff);

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You really don't want to do this as this breaks the interface's contract. So in a word: don't. Return the int.

  3. #3
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    it does :) I'll keep this on my wishlist :P

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Of course, you can always use constants if you find them easier to read:

    Java Code:
    public static final int EQUAL = 0;
    public static final int LESS_THAN = -1;
    public static final int GREATER_THAN = 1;
    If your salary is a double (and there are problems with using float for currency -- ask fishtoprecord), you could even do something like this:
    Java Code:
       public int compareTo(Employee anotherEmployee) {
          return new Double(_salary).compareTo(new Double(anotherEmployee.Salary()));
       }

  5. #5
    jon80's Avatar
    jon80 is offline Senior Member
    Join Date
    Feb 2008
    Location
    Malta (EU)
    Posts
    211
    Rep Power
    7

    Default

    Thanks. Just to quote my book:

    "Floating-point numbers are not suitable for financial calculation in which roundoff errors cannot be tolerated" (and trust me they cause a lot of havoc to accountants, translating into issues :)

    Core Java Fundamentals Vol I (8th Ed)
    ISBN: 978-0-13-235476-9 Pg. 42

Similar Threads

  1. Replies: 24
    Last Post: 05-18-2009, 10:23 AM
  2. What's the need for co-variant returns ?
    By ajeeb in forum New To Java
    Replies: 0
    Last Post: 03-23-2009, 09:56 AM
  3. Replies: 8
    Last Post: 03-19-2009, 09:38 PM
  4. compareTo()
    By Tsiliadoros in forum Advanced Java
    Replies: 5
    Last Post: 10-03-2008, 01:18 PM
  5. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 07:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •