Use a two-dimensional array to solve the following problem: A company has four s
ID: 3928077 • Letter: U
Question
Use a two-dimensional array to solve the following problem: A company has four salespeople - Sales Person 1, Sales Person 2, Sales Person 3. and Sales Person 4. The company sells 5 products - Product 1, Product 2, Product 3, Product 4, and Product 5. Each day, a sales person hands in a slip with the following information: Sales Person Number (1-4), Product Number (1-5), and dollar value of that product sold. This dollar value should be entered in the appropriate place in the multi-dimensional array. Keep asking for data input until a -1 is entered for the sales person number (sentinel). Output: Your output should reflect the array in tabular format ALONG with cross-sectional totals. For example, your program should look something like this: Enter Sales Person Number (1-4) or-1 to quit and view data: 1 Enter the Product Number (1-5): 1 Enter the dollar value: 1000 Enter Sales Person Number (1-4) or -1 to quit and view data: 2 Enter the Product Number (1-5): 1 Enter the dollar value: 2000 Enter Sales Person Number (1-4) or-1 to quit and view data: 2 Enter the Product Number (1-5): 2 Enter the dollar value: 500 Enter Sales Person Number (1-4) or-1 to quit and view data: -1 Sales Person Product 1 Product 2 Product 3 Product 4 Product 5 Sales Person TotalsExplanation / Answer
Below is the full code, The code has been tested for the few inputs as stated in the question.
***************************************************************************
import java.util.*;
import java.lang.*;
import java.io.*;
class TestClass // test class
{
static int [][]salesReport=new int[4][7];
public static void SalesPersonTotals() //total the Sales
{
int i,j,sum;
for(i=0;i<4;i++)
{
sum=0;
for(j=1;j<6;j++)
{
sum=sum+salesReport[i][j];
}
salesReport[i][6]=sum;
}
}
public static void main (String[] args) throws java.lang.Exception // main function
{
int i,j;
Scanner userInput = new Scanner(System.in);
int salesPerson,productno,dollarval;
for(i=0;i<4;i++)
{
salesReport[i][0]=i+1;
for (j=1;j<7;j++)
{
salesReport[i][j]=0;
}
}
do
{
System.out.println("Enter Sales Person Number (1-4) or -1 to quit and view data:");
salesPerson=userInput.nextInt();
if (salesPerson==-1)
{
SalesPersonTotals();
System.out.println("SalesPerson | Product1 | Product2 | Product3 | Product4 | Product5 | SalesPersonTotals");
for(i=0;i<4;i++)
{
for(j=0;j<7;j++)
{
System.out.print(" "+salesReport[i][j]+" ");
}
System.out.println();
}
System.exit(0);
}
else if ((salesPerson>=1)||(salesPerson<=4))
{
System.out.println("Enter the Product Number (1-5):");
productno=userInput.nextInt();
if ((productno>=1)||(productno<=5))
{
System.out.println("Enter the dollar value:");
dollarval=userInput.nextInt();
salesReport[salesPerson-1][productno]=dollarval;
}
else
{
System.out.println("Invalid Product Number entered");
}
}
else
{
System.out.println("Invalid Sales Person Number entered");
}
}while(salesPerson!=-1);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.