user2387429
user2387429

Reputation: 19

I am stuck on homework assignment Commission Calculation

I need to compare the total annual sales of at least three people. I need my app to calculate the additional amount that each must achieve to match or exceed the highest earner. I figured out most of it and know how to do it if there were only two people in the scenario, but getting third into the equation is throwing me for a loop! Any help is appreciated and thanks in advance! Here's what I have so far, but obviously at the end where the calculations are not going to be right.

package Commission3;
import java.util.Scanner;
public class MainClass {

    public static void main(String[] args) {
        // Create a new object AnnualCompensation
        Commission3 salesPerson[] = new Commission3[2];
        // creat two object
        salesPerson[0] = new Commission3();
        salesPerson[1] = new Commission3();
        salesPerson[2] = new Commission3();
        //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 your third salesperson's name?");
        salesPerson[2].name = keyboard.next();


        //get salesperson3 sales total
        System.out.println("Enter annual sales of third salesperson: ");
        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[2].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));
        } else if (total2 > total1) {
            System.out.print("Salesperson " + salesPerson[0].name + "'s additional amount
        of sales that he must " + " achieve 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);
        }
    }
}

Upvotes: 0

Views: 3230

Answers (1)

Travis
Travis

Reputation: 646

When you take the input from the user, keep track of the highest sales thus far, and the name of the salesperson with the most sales.

Then, instead of checking total1 and total2, you can loop through all three, and compare them to the max. If the current total is less than the max, then calculate the difference. Otherwise, the current total is equal to the max, and you don't need to do the calculation.

I'll leave the actual code for you to figure out.

Upvotes: 5

Related Questions