0

I'm working on a 'Franchise' program which has a owner, state, and sales, that are all set in the constructor and can't be changed. My problem comes when I'm trying to write my compareTo method.

package prob2;

public class Franchise implements Comparable <Franchise>  {
    final String owner;
    final String state;
    final double sales;

protected Franchise(String owner, String state, double sales ) {
    this.owner = owner;
    this.state = state;
    this.sales = sales;
}

public String toString() {
    String str = state + ", " + sales + ", " + owner;
    return str;
}

public String getState() {
    return state;
}

public double getSales() {
    return sales;
}
public int compareTo(Franchise that) {
    double thatSales = that.getSales();
    if (this.getState().compareTo(that.getState()) <0) 
        return -1;
    else if (this.getSales() > thatSales)
        return -1;
    else if (this.getSales() < thatSales)
        return 1;
    else
        return 0;
}

The program should implement the comparable interface and should compare Franchise objects based on BOTH state ASCENDING and sales DESCENDING. My question is how would I compare using both of the values, is there a way to do it in a single compare, or do i need multiple compareators?

EXAMPLE:

state = CA, sales = 3300 compared to state = NC, sales = 9900 would return NEGATIVE

state = CA, sales = 3300 compared to state = CA, sales = 1000 would return NEGATIVE

state = CA, sales = 3300 compared to state = CA, sales = 9900 would return POSITIVE

Thanks for any and all help.

user2745043
  • 189
  • 2
  • 7
  • 24

3 Answers3

2

is there a way to do it in a single compare, or do i need multiple compareators?

In your case, you don't need multiple comparators. Just write the logic based on both the attributes, in single compareTo method, along the lines of:

public int compareTo(Franchise that) {
    if (this.getState().equals(that.getState()) {
        // Compare on the basis of sales (Take care of order - it's descending here)
    } else {
        // Compare on the basis of states.
    }
}
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • Ok so doing it this way I need to override the equals? – user2745043 Sep 10 '13 at 15:40
  • @user2745043. No. You are comparing strings using equals. – Rohit Jain Sep 10 '13 at 15:41
  • Being that my sales are of type double would simply casting something like if (this.getState().equals(that.getState())) { return (int) (that.sales - this.sales); be the correct way to do it? – user2745043 Sep 10 '13 at 15:54
  • @user2745043. No that won't be. Better to return value based on actual comparison result. Use `<`, and `>` for comparison. – Rohit Jain Sep 10 '13 at 15:57
  • I updated my code using your suggestion for the sales. However, i'm having trouble with the state. It doesn't let me use < or > when comparing them or -. Do I need to check sales using .equals here or is there something else i'm doing wrong? Thanks for all your help by the way. – user2745043 Sep 10 '13 at 16:10
  • Ok what do you expect this `if (this.sales < that.sales);` to do? You should at least do something in the `if` block. Perhaps `return 1;`. – Rohit Jain Sep 10 '13 at 16:13
  • Also note that String implements Comparable and you've also got `Double.compare`, so you could just substitute the logic with something like `return state.compareTo(that.state) - Double.compare(sales, that.sales);` and I don't think you'd have to worry about overflows in this case. – sgbj Sep 10 '13 at 16:18
  • @Rohit Jain. I fixed my code to your suggestion and looked over it a while, does it look like something like this would work without error? Mainly does it seem logically correct? – user2745043 Sep 10 '13 at 21:48
0

You need to create different comparators by implementing Comparator interface. Depending on the sort param you need to use the appropriate comparator class in Collections.sort method.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
0

Of course you can use only one compare method, in pseudo-code:

lessThan(this.state, a.state) && this.sales > a.sales

(or something like that)

Moritz Petersen
  • 12,902
  • 3
  • 38
  • 45