Personal tools
You are here: Home → Comp 170 Introduction to OOP → Lab Materials → Lab 10 → Sales.java
Document Actions

Sales.java

by Mit Patel — last modified 2006-03-28 04:47 PM

Sales class

Click here to get the file

Size 2.3 kB - File type text/x-java

File 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



« October 2027
Su Mo Tu We Th Fr Sa
12
3456789
10111213141516
17181920212223
24252627282930
31
 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: