Tracking Sales File Sales.java contains a Java program that prompts for and read
ID: 3759936 • Letter: T
Question
Tracking Sales File Sales.java contains a Java program that prompts for and reads in the sales for each of 6 salespeople in a company. It then prints out the id and amount of sales for each salesperson and the total sales. Study the code, then compile and run the program to see how it works. Now modify the program as follows:
1. Compute and print the average sale. (You can compute this directly from the total; no loop is necessary.)
2. Find and print the maximum sale. Print both the id of the salesperson with the max sale and the amount of the sale, e.g., "Salesperson 3 had the highest sale with $4600." Note that you don't need another loop for this; you can do it in the same loop where the values are read and the sum is computed.
3. Do the same for the minimum sale.
4. After the list, sum, average, max and min have been printed, ask the user to enter a sales goal value. There is one restriction, the number must be greater than zero and even. Then print the id of each salesperson who exceeded that amount, and the amount of their sales. Also print the total number of salespeople whose sales exceeded the sales goal.
5. The salespeople being a picky bunch want a number greater than zero and even. Modify your program so that the ids run in the correct sequence, 2, 4, 6, 8,.… Hint: Do not modify the array.
6. Instead of always reading in 6 sales amounts, at the beginning ask the user for the number of sales people and then create an array that is just the right size. But since this is a product by Even Software Solutions we want to consider only even numbers as valid sizes. The program can then proceed as before.
// ****************************************************************
// Sales.java
//
// Reads in and stores sales for each of 5 salespeople. Displays
// sales entered by salesperson id and total sales for all salespeople.
//
// ****************************************************************
import java.util.Scanner;
public class Sales
{
public static void main(String[] args)
{
final int SALESPEOPLE = 6;
int[] sales = new int[SALESPEOPLE];
int sum;
Scanner scan = new Scanner(System.in);
for (int i=0; i<sales.length; i++)
{
System.out.print("Enter sales for salesperson " + i + ": ");
sales[i] = scan.nextInt();
}
System.out.println(" Salesperson Sales");
System.out.println("--------------------");
sum = 0;
for (int i=0; i<sales.length; i++)
{
System.out.println(" " + i + " " + sales[i]);
sum += sales[i];
}
System.out.println(" Total sales: " + sum);
}
}
Explanation / Answer
import java.util.Scanner;
public class Sales
{
public static void main(String[] args)
{
final int SALESPEOPLE = 6;
int[] sales = new int[SALESPEOPLE];
int sum,max=0,min=0,c=0,i;
Scanner scan = new Scanner
(System.in);
for (i=0; i<sales.length; i++)
{
System.out.print("Enter
sales for salesperson " + i + ": ");
sales[i] = scan.nextInt();
if(sales[i]>max)
max=sales[i];
if(sales[i]<min)
min=sales[i];
}
System.out.println(" Salesperson
Sales");
System.out.println
("--------------------");
sum = 0;
for (i=0; i<sales.length; i++)
{
System.out.println(" " + i
+ " " + sales[i]);
sum += sales[i];
}
System.out.println(" Total sales: " +
sum);
System.out.println(" Average sales: "
+ sum/(i-1));
System.out.println(" Salesperson "+i
+" had the highest sale with $"+max);
System.out.println(" Salesperson "+i
+" had the lowest sale with $"+min);
System.out.println("Enter Sales
Goal");
int g=scan.nextInt();
for ( i=0; i<sales.length; i++)
{
if(sales[i]>g)
{
System.out.println(" Sales Person " + i +" Sale is" + sales[i]);
c++;
}
}
System.out.println("Total number of
Selaepersons exceeding sales goal are ="+c);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.