私は、所有者、州、および販売を含む「フランチャイズ」プログラムに取り組んでいます。これらはすべてコンストラクターに設定されており、変更できません。私の問題は、compareTo メソッドを記述しようとしているときに発生します。
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;
}
プログラムは、同等のインターフェースを実装し、昇順の状態と売上の降順の両方に基づいて Franchise オブジェクトを比較する必要があります。私の質問は、両方の値を使用してどのように比較するかです。単一の比較でそれを行う方法はありますか、それとも複数の比較子が必要ですか?
例:
州 = CA、売上 = 3300 を州 = NC、売上 = 9900 と比較すると、NEGATIVE が返されます。
state = CA、sales = 3300 を state = CA、sales = 1000 と比較すると、NEGATIVE が返されます。
state = CA、sales = 3300 を state = CA、sales = 9900 と比較すると、POSITIVE が返されます。
助けてくれてありがとう。