An Application Using Arrays Joe s Gift Cart Monthly Product Sales Joe s Gift Cart sells 5 gift products Joe wants us to develop a program to track the number of each product sold for the first six months of this year At the end of the period Joe wants us to show The product name and total number sold for the period He also wants to know the total number of products sold by month He wants the report to look like Product Name balls hats total Jan 10 5 15 Feb 15 11 25 Mar 20 11 31 April 33 11 44 May 10 11 21 June 33 11 44 total sales 121 60 We will need to store the following data items Five product names Six months of sales figures for the five products 30 values Total sales for each product 5 values Total products sold for each month 6 values We can declare a variable for each value to be stored Or We can use arrays Product Names Sales per month Balls 100 Hats 80 Bears 123 T Shirts 79 Mugs 200 Total sales per month Total Product Sales 582 90 75 110 60 120 555 To store the names we need a 5 X 1 array of Strings Or simply a 5 element array To store the monthly totals we need a 1 X 6 array of integers Or simply a 6 element array To store the total sales for each product we need a 5 X 1 array of integers Or simply a 5 element array To store the products sold for each month we need a 5 X 6 array of integers Final int numProducts 5 Final int numMonths 6 int salesArray new int numProducts numMonths String productNames new String numProducts int monthlySalesTotals new int numMonths int productSalesTotals new int numProducts The calculations we need to perform are Input the product names and sales for EACH product Calculate the total sales for each product Calculate the total sales for each month Print the report Input the product names and sales for EACH product We need to loop through all 5 products Then for each product We need to enter the products name Loop through each month entering sales figures for each month Enter the product names and sales for i 0 i numProducts i System out println Please enter the products name productNames i input nextLine for j 0 j numMonths j System out println Please enter the sales for Product productNames i for month j 1 salesArray i j input nextInt end inner loop input nextLine end outter loop Calculate the total sales for each product For each product Loop through each month and sum the sales Store the sum in the productSalesTotal element for that product Product 1 in element 0 etc calculate the total sales for each product for i 0 i numProducts i sum 0 for j 0 j numMonths j sum salesArray i j end inner loop productSalesTotals i sum Calculate the total sales for each month For each month Add together the sales for each product Store the sum in the element of the array monthlySalesTotals for that month January 0 February 2 etc calculate total sales for each month for j 0 j numMonths j sum 0 for i 0 i numProducts i sum salesArray i j end inner loop monthlySalesTotals j sum end outter loop Print the report Print Headings For each product Print the product name Print the sales for each month Print the total sales for that product Print the totals sales for each month System out printf 10s t 8s t 8s t 8s t 8s t 8s t 8s t 8s n Product January February March April May June Total for i 0 i numProducts i System out printf 10s productNames i for j 0 j numMonths j System out printf t 8d salesArray i j end inner loop System out printf t 8d n productSalesTotals i end outter loop System out printf 10s Total for j 0 j numMonths j System out printf t 8d monthlySalesTotals j System out printf n
View Full Document
Unlocking...