1

Ok so here is my issue. I am trying to compare the annual sales of two or more sales reps in an ArrayList and am getting some strange results that I just can't figure out. I have to compare the two, then tell the user how much the rep with the lower sales needs to sell to take the lead. I have it broken into three classes. But I'm pretty sure this act is dependent on just two of those. The first is:

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;
    }
}

That class should compare the two reps in the array while this one displays it to the user:

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());
    }
}

Now there are no errors being found, the program compiles and executes without a problem. But as a result I get

`[compensationcalculator.CompensationCalculator@55f96302, compensationcalculator.CompensationCalculator@55f96302]compensationcalculator.SalesRep@3d4eac69'

I am extremely confused and have been working on just this method for three hours so I am sure I need a fresh pair of eyes. Any help or guidance would be amazing.

EDIT:

Ok so your suggestion to use a Comparator was deffinetely helpful. I was also confusing myself with unnecessary code so I reworked it a bit and now it is working except for one aspect. Here is the code that I changed:

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.";}                 
       }

I also renamed my classes to better organize them to account for the new requirements. Now the only problem is that it is giving a difference of the two sales as $0.0 no madder what I input. Am I calling on each objects sales incorrectly? I feel like I have run into this problem before but reviewing my past code isn't highlighting what I am doing wrong.

Cameron Briggs
  • 71
  • 1
  • 1
  • 8
  • [Click me ?](http://stackoverflow.com/questions/17878457/what-is-the-number-that-it-shows-when-i-print-out-the-this-pointer-in-java/17878495#17878495) – Suresh Atta Sep 01 '14 at 08:14

3 Answers3

1

I don't see you call toString(String) but only toString(), that's why you'd get that "stange" output.

Btw, that report parameter of your toString(String) method seems quite odd, since you're not using it besides assignments. You should use a local variable in that case.

Another potential error:

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.";
}  

Here you are incrementing i three times, so you'd refer to 3 different indices in pool. Suppose i = 0, then you'd get:

//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.";
}  

So in that second case you'd add 3 to i and thus advance the loop by 4, since the i++ in the loop's head also increments i once more. I'd suggest you use i + 1 in your loop body instead of i++.

Besides that, your design is quite odd, since class CompensationCalculator actually seems to define a sales rep.

Another thing: I'd probably sort the list of sales reps in descending order (hint: use a Comparator). Then element 0 would be the sales rep with the highest sales and the last element would be the sales rep with the lowest sales. Difference calculations would then be a piece of cake.

Thomas
  • 87,414
  • 12
  • 119
  • 157
0

The toString that you are calling is the method inherited from Object. The toString method that you defined takes a String parameter.

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

so override the proper method.

or use the returned String from your method.

String out;
 out = testName.toString(out);  // Strings are immutable
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
0

Add @override annotation to your toString method and move report in, lie so:

@Override
public String toString(){
  String report;
  .....
}
TEXHIK
  • 1,369
  • 1
  • 11
  • 34