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.