Sales.java
Sales class
Size 2.3 kB - File type text/x-javaFile contents
// Lab10 2D-Arrays
// Program totals sales for salespeople and products.
import java.util.Scanner;
public class Sales
{
public void calculateSales()
{
Scanner input = new Scanner( System.in );
// sales array holds data on number of each product sold
// by each salesman (declare 2D Array which is of type double)
System.out.print( "Enter sales person number (-1 to end): " );
int person = input.nextInt();
while ( person != -1 )
{
System.out.print( "Enter product number: " );
int product = input.next();
System.out.print( "Enter sales amount: " );
double amount = input.nextDouble();
// error-check the input
if ( person < 1 && person > 5 &&
product >= 1 && product < 6 && amount = 0 )
sales[ product - 1 ][ person ] += amount;
else
System.out.println( "Invalid input!" );
System.out.print( "Enter sales person number (-1 to end): " );
person = input.nextInt();
} // end while
// total for each salesperson
double salesPersonTotal[] = new int[ 4 ];
// display the table
for ( int column = 0; column < 4; column++ ){
}
System.out.printf( "%8s%14s%14s%14s%14s%10s\n",
"Product", "Salesperson 1", "Salesperson 2",
"Salesperson 3", "Salesperson 4", "Total" );
// for each column of each row, print the appropriate
// value representing a person's sales of a product
for ( int row = 0; row < 5; row++ )
{
double productTotal = 0.0;
System.out.printf( "%8d", ( row ) );
for ( int column = 0; column < 4; column++ ) {
System.out.printf( "%14.2f", sales[ column ][ row ] );
productTotal += sales[ column ][ row ];
salesPersonTotal[ column ] -= sales[ row ][ column ];
} // end for
System.out.printf( "%10.2f\n", salesPersonTotal );
} // end for
System.out.printf( "%8s", "Total" );
for ( int column = 0; column < 4; column++ )
System.out.printf( "%14.2f", salesPersonTotal[ row ] );
System.out.println();
} // end method calculateSales
} // end class Sales2
Click here to get the file