[英]Trying to compare rep sales in an array list in Java
好的,這是我的問題。 我正在嘗試比較ArrayList中兩個或多個銷售代表的年銷售額,並得到一些我無法弄清楚的奇怪結果。 我必須將兩者進行比較,然后告訴用戶需要銷售多少銷售額較低的銷售代表才能帶頭。 我把它分為三類。 但我很確定此舉僅取決於其中兩個。 第一個是:
import java.util.ArrayList;
/**
*
* @author Cameron
*/
public class SalesRep {
private ArrayList<CompensationCalculator> pool;
public SalesRep(){
pool = new ArrayList<>();
}
public void setPool(ArrayList<CompensationCalculator> pool){
this.pool = pool;
}
public ArrayList<CompensationCalculator> getPool(){
return pool;
}
public void addToPool(CompensationCalculator salesRep){
pool.add(salesRep);
}
public String toString(String report){
double diff;
for(int i=0; i<pool.size(); i++){
if (pool.get(i).getSales() < pool.get(i++).getSales()){
diff = pool.get(i++).getSales() - pool.get(i).getSales();
report = pool.get(i).getName() + "needs to sell " +
diff + " to take the lead.";
}
if (pool.get(i).getSales() > pool.get(i++).getSales()){
diff = pool.get(i).getSales() - pool.get(i++).getSales();
report = pool.get(i++).getName() + "needs to sell " +
diff + " to take the lead.";
}
}
return report;
}
}
該類應比較數組中的兩個代表,同時向用戶顯示該代表:
import java.util.Scanner;
public class AnnualSales {
public static void main(String[] args){
CompensationCalculator test = new CompensationCalculator(); //Creates a new instance of the class
SalesRep testName = new SalesRep(); //Creates a new instance of the SalesRep class
String cont = new String(); //A string to represent if there ar emore names to be added
Scanner scan = new Scanner(System.in); //Allows for user input to be read
while (!cont.equalsIgnoreCase("n")){
System.out.println("What is the name of the sales representative? ");
test.setName(scan.next());
System.out.println("Please enter " + test.getName() +
"'s annual sales: ");
test.setSales(scan.nextDouble());
testName.addToPool(test);
System.out.println("Are there any more sales representatives you "
+ "would like to add? ");
cont = scan.next();
}
System.out.print(testName.getPool());
System.out.print(testName.toString());
}
}
現在沒有發現錯誤,程序可以毫無問題地編譯和執行。 但是結果我
`[[compensationcalculator.CompensationCalculator@55f96302,compensationcalculator.CompensationCalculator@55f96302] compensationcalculator.SalesRep@3d4eac69'
我非常困惑,並且已經在這種方法上工作了三個小時,所以我確定我需要一雙新鮮的眼睛。 任何幫助或指導都將是驚人的。
編輯:
好的,因此您的建議使用比較器絕對有幫助。 我也把自己與不必要的代碼混淆了,所以我做了一些修改,現在它只在一個方面起作用了。 這是我更改的代碼:
public String compare(SalesRep rep1, SalesRep rep2){
NumberFormat fmt = NumberFormat.getCurrencyInstance();
Double diff;
if (rep1.getSales() > rep2.getSales()){
diff = rep1.getSales() - rep2.getSales();
return rep2.getName() + " needs to sell " + fmt.format(diff) +
" to take the lead.";}
else{
diff = rep2.getSales() - rep1.getSales();
return rep1.getName() + " needs to sell " + fmt.format(diff) +
" to take the lead.";}
}
我還重命名了我的班級,以更好地組織他們以適應新的要求。 現在唯一的問題是,這給兩次銷售帶來的差額是0.0美元,我所輸入的內容毫無疑問。 我是否錯誤地要求銷售每個物品? 我覺得以前曾經遇到過這個問題,但是回顧我過去的代碼並沒有強調我做錯了什么。
我看不到您調用toString(String)
而僅調用toString(String)
toString()
,這就是為什么您會得到“固定”輸出的原因。
順便說一句,您的toString(String)
方法的report
參數似乎很奇怪,因為除了賦值之外您沒有使用它。 在這種情況下,您應該使用局部變量。
另一個潛在的錯誤:
if (pool.get(i).getSales() > pool.get(i++).getSales()){
diff = pool.get(i).getSales() - pool.get(i++).getSales();
report = pool.get(i++).getName() + "needs to sell " +
diff + " to take the lead.";
}
在這里,您將i
遞增3次,因此您將引用pool
3個不同的索引。 假設i = 0
,那么您將得到:
//the first i++ returns i (0) and then increments i to 1
if (pool.get(0).getSales() > pool.get(0).getSales()){
//here i is 1, thus the next i++ returns 1 and increments i to 2
diff = pool.get(1).getSales() - pool.get(1).getSales();
//here i is 2, so the next i++ returns 2 and increments i to 3
report = pool.get(2).getName() + "needs to sell " +
diff + " to take the lead.";
}
因此,在第二種情況下,您將給i
加3,從而使循環前進4,因為循環頭中的i++
也會使i再次遞增。 我建議您在循環體中使用i + 1
而不是i++
。
除此之外,您的設計還很奇怪,因為CompensationCalculator
類實際上似乎定義了銷售代表。
另一件事:我可能會按降序對銷售代表列表進行排序(提示:使用Comparator
)。 然后,元素0將是具有最高銷售額的銷售代表,最后一個元素將是具有最低銷售額的銷售代表。 差異計算將是小菜一碟。
您正在調用的toString
是從Object繼承的方法。 您定義的toString
方法采用String
參數。
System.out.print(testName.toString());
因此請覆蓋適當的方法。
或使用您的方法返回的String
。
String out;
out = testName.toString(out); // Strings are immutable
在您的toString方法中添加@override批注,然后將報表移入,如下所示:
@Override
public String toString(){
String report;
.....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.