In: Computer Science
In JAVA
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,2,3, or 4), Product Number (1,2,3,4, or 5), and dollar value of that product sold. This dollar value should be entered in the appropriate place in the multi-dimensional array with rows representing sales person (1-4) and columns representing products (1-5). Keep asking for data input until -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 Totals
1 1000 0 0 0 0 1000
2 2000 500 0 0 0 2500
3 0 0 0 0 0 0
4 0 0 0 0 0 0
_____________________________________________________________________________
Product Totals 3000 500 0 0 0
Hi Student, I have created the below JAVA code fulfiling the requirements you have asked for in the question.
import java.util. * ;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System. in );
int[][] dollarvalue = new int[4][5];
int input = 0;
while (true) {
System.out.println("Enter Sales Person Number (1-4) or -1 to quit and view data:");
input = sc.nextInt();
if (input == -1) {
break;
}
System.out.println("Enter the Product Number (1-5):");
int inputproduct = sc.nextInt();
System.out.println("Enter the dollar value:");
int inputvalue = sc.nextInt();
dollarvalue[input - 1][inputproduct - 1] = inputvalue;
}
System.out.println("Sales Person Product 1 Product 2 Product 3 Product 4 Product 5 Sales Person Totals");
for (int i = 0; i < 4; i++) {
int sum = 0;
System.out.print((i + 1) + " ");
for (int j = 0; j < 5; j++) {
System.out.print(dollarvalue[i][j] + " ");
sum = sum + dollarvalue[i][j];
}
System.out.print(sum);
System.out.println();
}
System.out.println("--------------------------------------------------------------------------");
System.out.println("Product Totals ");
for (int j = 0; j < 5; j++) {
int sum = 0;
for (int i = 0; i < 4; i++) {
sum = sum + dollarvalue[i][j];
}
System.out.print(sum + " ");
}
}
}
Hope it helps. Thanks!