Answer to Question #271557 in Java | JSP | JSF for TOSH

Question #271557

Write a Java application and use a two-dimensional array that will store the five top artist sales for 2017. The application must also hold the artist sales for CDs, DVDs and BLU RAY items. Do not forget to use the marking guideline on the next page to see how the marks are allocated for this question. Your program must: Q.1.1 Create a two-dimensional array to contain the three sale items for five different artists. A single array must be used to store the artist names. ARTIST NAME CD SALES DVD SALES BLU RAY SALES Ed Sheeran 900 000 800 000 500 000 Pink 700 000 500 000 500 000 Bruno Mars 800 000 100 000 50 000 Foo Fighters 100 000 200 000 200 000 Taylor Swift 300 000 100 000 50 000 Q.1.2 Allow a user to enter in a number, ranging from 1 to 5, which will represent the artist position with regards to the album sales. Q.1.3 Printout the artist name including the CD, DVD, BLU RAY sales amounts and total sales


1
Expert's answer
2021-11-25T16:24:38-0500
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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS