Tracking Sales
File Sales.java contains a Java program that prompts
for and reads in the sales for each of 5 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. You'll be running the program multiple times, so you might want to copy and paste the input when needed to minimize your typing. In the sample at the bottom of this document I've used the input "45
123
65
32
99".
Now
modify the program as follows:
- Move the code that prompts the user and creates the sales array into a new method, int[] getSales(). Replace the code that does this is in the given Sales.java code with an invocation to getSales. You will need the method to return the integer array and you will need to store that return value in main's sales. So your code in main should be something like: int sales[] = getSales(); Test your program now--the output should be the same.
- Move the code that prints the table of sales into a new method printSales(). Replace the code that does this is in the given Sales.java code with an invocation to printSales. Test your program now--the output should be the same.
- Create a new method, printAverageSale(int[] sales) that takes an array of integers (sales, in this case) and prints the average sale. Call this method after the "Total sales" output. Test your program now. Make sure the output (the avearge) is correct.
- Create a new method, printMaximumSale(int[] sales) that takes an array of integers (sales, in this case) and finds and prints the largest sale and the ID of the
salesperson having the largest sale and the amount of that sale. E.g.,
"Salesperson 3 had the highest sale with $4500."
If more than one person has the maximum, just print the first one. Call the method just after the invocation of printAverageSale.
Test your program now.
- Do the same for the minimum sale via a method printMinimumSale(int[] sales). Call the method just after the invocation of printMaximumSale. Test your program now.
- Create a method printAchievers(int[] sales) that takes the array of sales as its argument, asks the
user to
enter a value, then prints 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 value entered. After the list of sales, and their sum, average, max and min have been printed in main, invoke this method. Test your program now.
- The salespeople are objecting to having an ID of 0 -- no one wants that
designation. Modify your program so that the IDs run from 1-5 instead of
0-4. Do not modify the array -- just make the information for
salesperson 1 reside in array location 0, and so on. You may have to modify your code in more than one place, depending on what you have done so far.
Test your program now.
- Instead of always reading in 5 sales amounts, in getSales
ask the user for
the number of sales people and then create an array that is just the right
size. The program can then proceed as before, though the loops should only iterate as many times as their are salespersons.
Test your program now.
Here's some sample output of the completed program:
Enter the number of sales people: 5
Enter sales for salesperson 1: 45
Enter sales for salesperson 2: 123
Enter sales for salesperson 3: 65
Enter sales for salesperson 4: 32
Enter sales for salesperson 5: 99
Salesperson Sales
--------------------
1 45
2 123
3 65
4 32
5 99
Total sales: 364
Average sale: 72
Maximum sales 123 by Salesperson 2
Minimum sales 32 by Salesperson 4
Enter a sales amount: 80
List of sales over 80
Salesperson Sales
2 123
5 99
2 salespeople had sales over 80