CISC 1115 Assignment 2 Fall 2020 Due Sept. 29

Write a Java program, including comments, to do the following:

Outline:

– The program will compute statistics for how salespeople did. The program will read in the id number of a salesperson, the number of customers who bought the product, the number of customers who did not buy, and the quota (this is the number of customers who must buy from this salesperson). The program will compute various things about the salesperson, and it will print all of this out. Then it will repeat the process for a new salesperson, over and over again, until the entire set of data has been taken care of.

Here are the details:

– The program will read in the id number of a salesperson (see step 6 below). The program will read in (and print) the number of customers who bought the product, the number who did not buy, and the quota. For example, the program could read in the following:

1234 20 5 30 [idno 1234, 20 bought 5 didn’t quota is 30]

-The program will compute and print the total number of customers (the number who bought plus the number who didn’t). It will also compute how many more the salesperson needs to get to the quota (maybe 0), or how many the salesperson is above the quota.

Neatly print all this information. If the salesperson reached or is over the quota, print a message of congratulations. Otherwise, print a message saying the salesperson must try harder.

The program will compute the salesperson’s sales percentage, which is a decimal value between 0 and 1. The sales percentage is defined to be the number of customers who bought divided by the total number of customers. (You can assume the salesperson always has at least one customer–see optional 2.)

For the data values shown for idno 1234 above, the total is 25 and the sales percentage is
20 / 25 = 0.800.

Print this value as shown, with exactly 3 decimal places.

  • The program should also assign the salesperson a bonus. The bonus is the number of customers who bought times 2, minus the number of customers who did not buy. For example, idno 1234 got a bonus of 35 (20 bought x 2 – 5 who didn’t buy). However, the bonus cannot be negative. For example, if 5 bought and 20 didn’t, the bonus would be 0. (Be sure to test this in your program.) The program should print the bonus assigned to the salesperson.
  • The program should determine if the salesperson had more customers who bought than did not, the same number of each, or more who did not buy. It should print an appropriate message in each of these three cases.
  • Then the program should skip a few lines of output and repeat the entire series of steps for the next salesperson, and so on until the last salesperson has been processed. (You must decide how to recognize that the last salesperson has been processed. Explain in a comment)
  • At that point, print the total number of salespersons (you must keep track of this as the program executes), then stop.

DATA: Be sure to read in data for at least 12 salespersons. Include a salesperson with no one who bought and a salesperson with everyone who bought (but still below the quota). Have some salespersons who don’t make the quota, some who do exactly, and some who are over the quota. Have some with a positive bonus and some with a 0 bonus. (Have at least two of each type described.) Be sure to make up a good set of data covering all cases.

Here is a complete set of output from two typical salespersons:

(Use these 2 plus at least 12 more)

id 1234 id 7890
10 bought 15 didn’t 21 bought 14 didn’t
quota 22 quota 21
total customers 25 total customers 35
12 below quota try harder made quota congratulations
bonus is 15 bonus is 28
more didn’t buy more bought than didn’t
sales pct. is 0.400 sales pct. is 0.600

OPTIONALS (If you do an optional, be sure to include comments):

If a salesperson has a negative value for any amount, print an error message, skip the rest of the processing for that person, and go on to the next salesperson. Keep track of how many invalid sets of data were entered and print this out at the end.

Add one or two salespersons who had 0 customers–no one bought, no one didn’t buy. (Make sure that you do not divide by 0 if the salesperson had 0 customers.) In this case, print a special message and go on to the next salesperson.

Keep track of the salesperson with the best sales percentage, and of the salesperson with the highest bonus. (These do not have to be the same–to test, make sure they are different.) Print these out at the end of the processing.

Answer

The following code illustrates an answer to the question above. This code is running and can be tested using any Java or Java-enabled code editor.

import java.util.ArrayList;
 import java.util.List;
 import java.util.Scanner;
public class Main {
public static void main(String[] args) {

    /*
     * create salesperson arraylist to store the
     * created salesperson objects 
     * We shall then print the objects from the list
     */

    List<SalesPerson> allSalesPersons = new ArrayList<SalesPerson>();

    /*
     * prompt the user for the total number of salespersons intended to be added
     * the total number of the sales persons determines the size of the dataset
     * as well as the number of loops the system should make to get all the data
     * values in
     */

    System.out.println("Please enter desired number of Salespersons");

    Scanner reader = new Scanner(System.in);

    int dataSize = reader.nextInt(); // store the total number of salespersons entered in a variable to use later 

    /*
     * here, we create a for loop that loops until all the values are entered
     * Before, looping, we tell the user how to enter the data in the program
     * Notice that each time the counter runs, it is incremented by 1 using the i++ part of the for loop
     * 
     */

    System.out.println("Enter salesperson data separate by a space in the format"
            + "\n- salespersonid,"
            + "\n- number of persons who bought,"
            + "\n- number of persons who did not buy,"
            + "\n- the quota for the salesperson");

    for(int i=0; i<dataSize; i++) {
        /*
         * here, we alert the user to enter the data for the current counter number
         * in the loop. Note that we add 1 to the counter (i+1). This ensures
         * that the counter visible to the user starts from 1 rather than 0 as 
         * the computer starts counting from 0
         */
        System.out.println("Salesperson  :  " + (i+1));

        /*
         * collect the information for the current sales person and store in  a string array 
         * this is one of the ways we can read integers separated by white space in java
         * we then access the different values according to the input format stated above
         * and create a new salesperson object ready to be inflated with the data entered
         */
        Scanner scanner = new Scanner(System.in);
        String mixedDataArray[]  = scanner.nextLine().split(" "); // store the data in a string[]array

        //create the new salesperson object 
        SalesPerson person = new SalesPerson();
        /*
         * now, add the different parts of the mixedDataArray[] to the
         * sales person created 
         * We also must convert the string value into an integer (Integer.parseInt()) as follows        
         */
        person.setsalesPersonID(Integer.parseInt(mixedDataArray[0]));//set salesperson ID
        person.setBuyingCustomerNum(Integer.parseInt(mixedDataArray[1])); // set number of customers who bought
        person.setDidNotBuyCustomerNum(Integer.parseInt(mixedDataArray[2]));// set number of customers who did not buy
        person.setSalesQuota(Integer.parseInt(mixedDataArray[3]));

        //add the person object to the salesperson list
        allSalesPersons.add(person);        

    }

    /*
     * after getting all the data into the arrayList, we can now print the salespersons data 
     * 
     * do check if the arrayList has data or nor
     */
    if(allSalesPersons.size()>0) { 
        //this code shall only run if the allSalesPersons List has data
        for(int j=0; j<allSalesPersons.size();j++) {
            allSalesPersons.get(j).printData();
        }
        System.out.println("The total number of salesperson was : " + allSalesPersons.size());
    }
}
}

class SalesPerson{
/**this class creates the salesperson object
 * the sales person class contains the different attributes
 * for the salesperson object
 * the class also contains the getter and setter methods
 * for setting and getting the different attributes for the
 * sales person
 */
int salesPersonID;
int buyingCustomerNum;
int didNotBuyCustomerNum;
int customerTotal;
int salesQuota;

public int getsalesPersonID() {
    return salesPersonID;
}

public void setsalesPersonID(int salesPersonID) {
    this.salesPersonID = salesPersonID;
}

public int getBuyingCustomerNum() {
    return buyingCustomerNum;
}

public void setBuyingCustomerNum(int buyingCustomerNum) {
    this.buyingCustomerNum = buyingCustomerNum;
}

public int getDidNotBuyCustomerNum() {
    return didNotBuyCustomerNum;
}

public void setDidNotBuyCustomerNum(int didNotBuyCustomerNum) {
    this.didNotBuyCustomerNum = didNotBuyCustomerNum;
}

public int getCustomerTotal() {
    return customerTotal;
}

public void setCustomerTotal(int customerTotal) {
    this.customerTotal = customerTotal;
}

public int getSalesQuota() {
    return salesQuota;
}

public void setSalesQuota(int salesQuota) {
    this.salesQuota = salesQuota;
}

/*
 * if a sales person sold more than their quota, print a congratulations message for them
 * if the person did not get to sell their quota, print a try harder message
 */
public void congratulatoryMessage() {
    if (buyingCustomerNum >= salesQuota) {
        System.out.println("Made Quota. Congratulations!");
    }else {
        System.out.println( (salesQuota - buyingCustomerNum )+ " below Quota! Try harder...");

    }
}

/*
 * calculate the sales percentage for the salesperson
 */
public float salesPercentage() {
    //here, we add the total number of customers then we divide by the number of those who bought
    int totalCustomers = buyingCustomerNum + didNotBuyCustomerNum;
    return ( (float) buyingCustomerNum / totalCustomers);
}

//calculate the sales bonus
public int printBonus() {
    int bonus = (buyingCustomerNum * 2) - didNotBuyCustomerNum;
    if(bonus>0) {
        return bonus; //the bonus was positive 
    }else {
        return 0; //the bonus was negative
    }
}

//calculate different cases for buying customers vs not buying customers
public void checkBuyingVersusNonBuyingCustomers() {
    if (buyingCustomerNum > didNotBuyCustomerNum) {
         System.out.println("More bought than didn't");
    }else if (buyingCustomerNum < didNotBuyCustomerNum){
            System.out.println("More didn't buy");

    }else if (buyingCustomerNum == didNotBuyCustomerNum ) {
           System.out.println("Both buying and not buying was equal");

    }

}
//print the salesPerson details
public void printData() {
    System.out.println("\n ID : "+ salesPersonID);
    System.out.println(buyingCustomerNum + " bought " + didNotBuyCustomerNum + " didn't");
    System.out.print("Quota is : " + salesQuota +"\n");
    congratulatoryMessage();
    System.out.println("bonus is " + printBonus());
    checkBuyingVersusNonBuyingCustomers();
    System.out.printf("%.3f", salesPercentage()); //print the value to 3 decimals 
    System.out.println("\n"+ "================================");

    }
 } 

Sample output

After running the Java code above for a dataset of 12 salespersons, the out was as follows;

Please enter desired number of Salespersons

12

Enter salesperson data separate by a space in the format

– salespersonid,

– number of persons who bought,

– number of persons who did not buy,

– the quota for the salesperson

Salesperson  :  1

1 0 20 30

Salesperson  :  2

2 20 0 20

Salesperson  :  3

3 20 0 30

Salesperson  :  4

4 30 15 35

Salesperson  :  5

5 25 0 25

Salesperson  :  6

6 25 12 28

Salesperson  :  7

7 40 2 30

Salesperson  :  8

8 0 0 25

Salesperson  :  9

9 0 6 15

Salesperson  :  10

10 20 20 20

Salesperson  :  11

11 25 25 20

Salesperson  :  12

12 20 40 30

 ID : 1

0 bought 20 didn’t

Quota is : 30

30 below Quota! Try harder…

bonus is 0

More didn’t buy

0.000

================================

 ID : 2

20 bought 0 didn’t

Quota is : 20

Made Quota. Congratulations!

bonus is 40

More bought than didn’t

1.000

================================

 ID : 3

20 bought 0 didn’t

Quota is : 30

10 below Quota! Try harder…

bonus is 40

More bought than didn’t

1.000

================================

 ID : 4

30 bought 15 didn’t

Quota is : 35

5 below Quota! Try harder…

bonus is 45

More bought than didn’t

0.667

================================

 ID : 5

25 bought 0 didn’t

Quota is : 25

Made Quota. Congratulations!

bonus is 50

More bought than didn’t

1.000

================================

 ID : 6

25 bought 12 didn’t

Quota is : 28

3 below Quota! Try harder…

bonus is 38

More bought than didn’t

0.676

================================

 ID : 7

40 bought 2 didn’t

Quota is : 30

Made Quota. Congratulations!

bonus is 78

More bought than didn’t

0.952

================================

 ID : 8

0 bought 0 didn’t

Quota is : 25

25 below Quota! Try harder…

bonus is 0

Both buying and not buying was equal

NaN

================================

 ID : 9

0 bought 6 didn’t

Quota is : 15

15 below Quota! Try harder…

bonus is 0

More didn’t buy

0.000

================================

 ID : 10

20 bought 20 didn’t

Quota is : 20

Made Quota. Congratulations!

bonus is 20

Both buying and not buying was equal

0.500

================================

 ID : 11

25 bought 25 didn’t

Quota is : 20

Made Quota. Congratulations!

bonus is 25

Both buying and not buying was equal

0.500

================================

 ID : 12

20 bought 40 didn’t

Quota is : 30

10 below Quota! Try harder…

bonus is 0

More didn’t buy

0.333

================================

The total number of salesperson was : 12

Need this Answer? – Tutorsploit can help!

We have professional tutors ready to offer you the complete answer to this question and many more! Create a student account or login now and post a task for free!


Calculate The Price

$10.00