ok Here is where I am now. I had the code written and it displayed correctly as long as salesPerson1 was the highest earner. I added the additional code if total2 > total1 and so on and now I do not get the calculations just display total commission of 1,2, and 3. I tried posting my code but it is too long. I am going to try to start with a new question and see if I can post it there. I need to calculate the additional amount of sales each salesperson must achieve to match or exceed the highest earner.

Here's my code

package Commission3;
import java.util.Scanner;



    public static void main(String[] args) {
        // Creat a new object AnnualCompensation
        AnnualCompensation salesPerson[] = new AnnualCompensation[3];
        // creat three object
        salesPerson[0] = new AnnualCompensation();
        salesPerson[1] = new AnnualCompensation();
         salesPerson[2] = new AnnualCompensation();
        //new scanner input
        Scanner keyboard = new Scanner(System.in);

        //get salesperson1 name
        System.out.println("What is your first salesperson's name?");
        salesPerson[0].name = keyboard.nextLine();

        //get salesperson1 sales total
        System.out.println("Enter annual sales of first salesperson: ");
        double val = keyboard.nextDouble();
        salesPerson[0].setAnnualSales(val);

        //get salesperson2 name
        System.out.println("What is your second salesperson's name?");
        salesPerson[1].name = keyboard.next();

        //get salesperson2 sales total
        System.out.println("Enter annual sales of second salesperson: ");
        val = keyboard.nextDouble();
        salesPerson[1].setAnnualSales(val);

       //get salesperson3 name
       System.out.println("What is the name of your third salesperson?: ");
       salesPerson[2].name = keyboard.next();

       //get salesperson3 sales total
       System.out.println("Enter annual sales of third salesperon: ");
       val = keyboard.nextDouble();
       salesPerson[2].setAnnualSales(val);


      double total1, total2, total3;
      total1 = salesPerson[0].getTotalSales();
            System.out.println("Total sales of " + salesPerson[0].name +" is: $" + total1);
      total2 = salesPerson[1].getTotalSales();
            System.out.println("Total sales of " + salesPerson[1].name +" is: $" + total2);
       total3 = salesPerson[2].getTotalSales();
            System.out.println("Total sales of " + salesPerson[2].name +" is: $" + total3);
      if (total1 > total2) {
            System.out.print("Salesperson " + salesPerson[1].name + "'s additional amount of        sales that he must " + " achieve to match or exceed the higher of the salesperson " + salesPerson[0].name);
            System.out.println(" $" + (total1 - total2));
    if (total1 > total3) {
         System.out.print("Salesperson " + salesPerson[2].name + "'s additional amount of sales that must " + " be achieved to match or exceed the higher of the salesperson " + salesPerson[0].name);
            System.out.println(" $" + (total1 - total3));

    if (total2 > total1){
            System.out.println("Salesperson" + salesPerson[1].name + "'s additional amount of sales that must " + " be acheived to match or exceed the higher of the salesperson " + salesPerson[2].name);
            System.out.println(" $" + (total2 - total1));

     if (total2 > total3){
            System.out.println("Salesperson" + salesPerson[3].name + "'s additional amount of sales that must " + " be acheived to match or exceed the higher of the salesperson " + salesPerson[2].name);
            System.out.println(" $" + (total2 - total3));

     if (total3 > total1){
            System.out.println("Salesperson" + salesPerson[1].name + "'s additional amount of sales that must " + " be acheived to match or exceed the higher of the salesperson " + salesPerson[3].name);
            System.out.println(" $" + (total3 - total1));

     if (total3 > total2){
            System.out.println("Salesperson" + salesPerson[2].name + "'s additional amount of sales that must " + " be acheived to match or exceed the higher of the salesperson " + salesPerson[3].name);
            System.out.println(" $" + (total2 - total3));

      } else if (total2 > total1) {
            System.out.print("Salesperson " + salesPerson[0].name + "'s additional amount of sales that must " + " be achieved to match or exceed the higher of the salesperson " + salesPerson[1].name);
            System.out.println(" $" + (total2 - total1));
       }
         else {
            System.out.println("Both have same compensation $" + total1);
           } 
          } 
}
}

Recommended Answers

All 2 Replies

Lots of if (...) { without any matching } that I can see. Indent the code according to the actual brackets and see if that makes sense

To lessen the amount of lines the code takes up you could make a few small modifications such as when creating the objects, instead of asking every time on different lines for the same information. You could implement a loop, for example:

private static Scanner console = new Scanner(System.in);
    public static void main(String[] args){

        System.out.println("Please enter the number of employees");
        int n = console.nextInt();

        // use arraylist to allow for dynamic expansion/shrinking
        ArrayList<AnnualComp> salesPersons = new ArrayList<>(n);

        for(int i = 1; i <= n; i ++){
            //create salesperson(1) name
            System.out.println("Name of salesperson " + i + ": ");      
            //assign name to object salesperson(0)
            String employeeName = console.next();

            //create salesperson(1) annual sales
            System.out.println("Annual sales of salesperson " + i + ": ");
            //assign value to object salesperson(0)
            double annualSales = console.nextDouble();

            //create object with specified values
            salesPersons.add(new AnnualComp(employeeName, annualSales));
        }

This will make it so, you have an arraylist that you can add as many or take away as many salesmen as you want. In order to create the objects, you are only asking for each piece of information once, and allowing the loop to do all the repetitive work for you. By the end you will have the same number of salesPersons objects as you have for the number of components in the ArrayList.

Then to reduce the number of if/else statements that you have going on, you could create a double variable (I named it difference) and take the values of each salesPersons annualSales amount and compare them to eachother using a ternary operator. For example:

difference = 
                ((salesPersons.get(0).getAnnualSales() > salesPersons.get(1).getAnnualSales())?
                salesPersonsTotals[0]-salesPersonsTotals[1]:
                salesPersonsTotals[1]-salesPersonsTotals[0]);

This will eliminate the double checks that you do, referencing your code here:

if (total1 > total2) 
else if (total2 > total1) 

Now, another thing that I noticed that you may want to implement is your own toString() method that way you can have the output the way that you want. So, for example here is how I wrote your program (minus the solution, just the toString() part)

@Override
        public String toString(){
            NumberFormat formatter = NumberFormat.getCurrencyInstance();
            return employeeName + "'s annual sales are currently: " + formatter.format(annualSales);          
        }

This will output each employee's name, followed by there annual sales total and utilizes the .getCurrencyInstance() which enables the program to pickup on the default currency mint in your location, and outputs the correct result for example:
$12.00

Feel free to ask questions, these are just a few of the things I would suggest.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.