Use a two-dimensional array to solve the following problem. A company has four salespeople (1 to 4) who sell five different products (1 to 5). Once a day, each salesperson passes in a slip for each different type of product sold.

Each slip contains the following:
a) The salesperson number
b) The product number
c) The total dollar value of that product sold that day

Thus, each salesperson passes in between 0 and 5 sales slips per day. Assume that the information from all of the slips for last month is available. Write a program that will read all this information for last month’s sales and summarize the total sales by salesperson by product. All totals should be stored in the two-dimensional array sales. After processing all the information for last month, print the results in tabular format with each of the columns representing a particular salesperson and each of the rows representing a particular product. Cross total each row to get the total sales of each product for last month; cross total each column to get the total sales by salesperson for last month. Your tabular printout should include these cross totals to the right of the totaled rows and to the bottom of the totaled columns


Could someone run my code and help me fix it please?
There's no compiler error but I couldn't output the data and totalsales
Here are my 2 codes, the second one create and manipulate code 1 object.
Thank you.

import java.util.Scanner;

public class TwoDimensionalSales
{
   // declaration of variables
   double sales[][];
   int sp;
   int p;
   double amt;
   int row, col;
   
   
   public void Sales()
   {
      Scanner input = new Scanner(System.in);
	  
	  // sales array holds data on number of each product sold
	  // by each of 4 salesman
	  sales = new double[5][4];
	  
	  System.out.print ("Enter sales person number (-1 to end) : ");
	  sp = input.nextInt();
	  
	  while (sp != -1)
	  {
	    System.out.print("Enter product number: ");
        p = input.nextInt();
        System.out.print("Enter sales amount: ");
        amt = input.nextDouble();
		
		if (sp < 1 && sp > 5 && p >= 1 && p < 6 && amt >= 0)
		sales[sp-1][p-1] += amt;

        if (p > 5)  
		    System.out.print("Invalid input!\n");
		// end if 
		
		System.out.print("Enter sales person number (-1 to end): ");
		sp = input.nextInt(); 
   			
	  } // end while
	  
	  // total for each salesperson
	  double personTotal[] = new double[4];
	  
	  // display the table
	  for ( col = 0; col < 4; col++)
	     personTotal[col] = 0;
		 
	  System.out.printf("%7s%14s%14s%14s%14s%14s\n", "Product", "Salesperson 1",
	     "Salesperson 2", "Salesperson 3", "Salesperson 4","Total");
	
	  for ( row = 0; row < 5; row++)
	  {
	     double productTotal = 0.0;
		 System.out.printf("%7d", (row + 1));
		 
		 for (int col = 0; col < 4; col++)
		 {
		    System.out.printf("%14.2f", sales[row][col]);
			productTotal += sales[row][col];
			personTotal[col] += sales[row][col];
		 } // end inner loop
		 
		 System.out.printf("%14.2f\n", productTotal);
		 
	  }// end for
	  
	  System.out.printf("%7s", "Total");
	  
	  for (int col = 0; col < 4; col++)
	     System.out.printf("%14.2f", personTotal[col]);
	  
	  System.out.println();
	  
   }// end method sales
   
} // end class TwoDimensionalSales
public class TwoDimensionalSalesTest
{
   // main begins execution here
   public static void main( String args[])
   {
      TwoDimensionalSales mySales = new TwoDimensionalSales();
	  mySales.Sales();
	  
   }// end main

}// end class TwoDimensionalSalesTest

Can you show what the program inputs and outputs and describe what's wrong with the output?

Here's my output


Enter sales person number (-1 to end) : 2
Enter product number: 4
Enter sales amount: 100
Enter sales person number (-1 to end): 4
Enter product number: 7
Enter sales amount: 250
Invalid input!
Enter sales person number (-1 to end): 3
Enter product number: 1
Enter sales amount: 400
Enter sales person number (-1 to end): -1
Product Salesperson 1 Salesperson 2 Salesperson 3 Salesperson 4 Total
1 0.00 0.00 0.00 0.00 0.00
2 0.00 0.00 0.00 0.00 0.00
3 0.00 0.00 0.00 0.00 0.00
4 0.00 0.00 0.00 0.00 0.00 5 0.00 0.00 0.00 0.00 0.00
Total 0.00 0.00 0.00 0.00

It's not supposed to be 0.00

if (sp < 1 && sp > 5 && p >= 1 && p < 6 && amt >= 0)
		sales[sp-1][p-1] += amt;

here is where you put values into the array.
What happens if the if() test fails? Nothing goes into the array.
Add an else clause to println() the values of sp, amt and p when it fails.

You should NOT ignore error or unexpected conditions.

Change if (sp < 1 && sp > 5 && p >= 1 && p < 6 && amt >= 0) to
if (sp >= 1 && sp < 5 && p >= 1 && p < 6 && amt >= 0)

Hi gianfranco Welcome to DaniWeb

Thanks for your post, but did you notice the question was 3 years old? It's unlikely he is still waiting foir an answer!

Also, we prefer to help people learn Java rather than just do their homework for them. Just giving a solution they can copy/paste isn't the best way. Try to explain what's wrong with their code and give them some direction about how to fix it, but leave the to work out the solutuin themselves. That way they will learn the most. NormR1's last post was a good example.
Thanks
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.