尝试比较Java中数组列表中的rep销售额

时间:2014-09-01 08:04:59

标签: java arrays

好的,这是我的问题。我试图比较一个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;
    }
}

该类应比较数组中的两个rep,同时将其显示给用户:

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美元。我是否错误地打电话给每个对象销售?我觉得我之前遇到过这个问题,但是回顾一下我过去的代码并不突出我做错了什么。

3 个答案:

答案 0 :(得分:1)

我没有看到你拨打toString(String),但只是toString(),这就是为什么你会得到这个&#34; stange&#34;输出

顺便说一下,report方法的toString(String)参数似乎很奇怪,因为除了作业之外你还没有使用它。在这种情况下,您应该使用局部变量。

另一个潜在错误:

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递增三次,因此您在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.";
}  

因此,在第二种情况下,您将{3}添加3,从而使循环前进4,因为循环头中的i也会再次增加i。我建议您在循环体中使用i++而不是i + 1

除此之外,你的设计很奇怪,因为类i++实际上似乎定义了销售代表。

另一件事:我可能按降序排序销售代表列表(提示:使用CompensationCalculator)。然后,元素0将是具有最高销售额的销售代表,最后一个元素将是具有最低销售额的销售代表。差异计算将是一块蛋糕。

答案 1 :(得分:0)

您正在调用的toString是从Object继承的方法。您定义的toString方法采用String参数。

System.out.print(testName.toString());

所以覆盖正确的方法。

或使用您方法中返回的String

String out;
 out = testName.toString(out);  // Strings are immutable

答案 2 :(得分:0)

将@override注释添加到toString方法并移入报告,如下:

@Override
public String toString(){
  String report;
  .....
}