// FILE: stattest.cxx
// An interactive test program for the statistician class
// Originally written by Michael Main (main@colorado.edu), Aug 2, 1996
// Modified by J. Rogers, Jan/2001
// Modified by J. Rogers, Jan/2002. Modified course number.


#include <cctype>    // Provides toupper
#include <iomanip.h>   // Provides setw to set the width of an output
#include <iostream.h>  // Provides cout, cin
#include <cstdlib>   // Provides EXIT_SUCCESS
#include "stats.h"
using namespace CS256_Asgn_2;
using namespace std;

// PROTOTYPES of functions used in this test program:
void print_menu( );
// Postcondition: A menu of choices for this program has been written to cout.
// Library facilties used: iostream.h

char get_user_command( );
// Postcondition: The user has been prompted to enter a one character command.
// A line of input (with at least one character) has been read, and the first
// character of the input line is returned.

double get_number( );
// Postcondition: The user has been prompted to enter a real number. The
// number has been read, echoed to the screen, and returned by the function.

void print_values(const statistician& s);
// Postcondition: The length, sum, minimum, mean, and maximum of s have been
// written to cout, using a field width of 10 for each of these values.
// (If length is zero, then minimum, mean, and maximum are not written.)

statistician& get_statistician( statistician& st1, 
                                statistician& st2, 
                                statistician& st3 );
// Precondition: The user has been prompted to enter the number of a 
//               statistician. 
// Postconditions: The return value references the statistician chosen
//    by the user or, if input has failed, st1.


int main( )
{
  statistician s1, s2, s3;  // Three statisticians for us to play with
  char choice;              // A command character entered by the user
  double x;                 // Value for multiplication x*s1
  statistician left, right; // Temporary statisticians 

  cout << "Three statisticians s1, s2, and s3 are ready to test." << endl;
  
  do
    {
      cout << endl;
      print_menu( );
      choice = toupper(get_user_command( ));
      switch (choice)
        {
        case 'R': cout << "Which one should I reset (1, 2, 3) " << endl;
          (get_statistician( s1, s2 ,s3 )).reset( );
          cout << "The statistician has been reset." << endl;
          break;
        case '1': s1.next(get_number( ));
          break;
        case '2': s2.next(get_number( ));
          break;
        case '3': s3.next(get_number( ));
          break;
        case 'T': cout << "The values are given in this table:" << endl;
          cout << "        LENGTH       SUM"
               << "   MINIMUM      MEAN   MAXIMUM" << endl;
          cout << "  s1";
          print_values(s1);
          cout << "  s2";
          print_values(s2);
          cout << "  s3";
          print_values(s3);
          break;
        case 'E': cout << "Which statistician to compare? " << endl;
          left = get_statistician( s1, s2, s3 );
          cout << "Which statistician to compare it to? " << endl;
          right = get_statistician( s1, s2, s3 );
          if (left == right)
            cout << "The statisticians are equal." << endl;
          else
            cout << "The statisticians are not equal." << endl;
          break;
        case '+': cout << "Which statistician to add? " << endl;
          left = get_statistician( s1, s2, s3 );
          cout << "Which statistician to add to it? " << endl;
          right = get_statistician( s1, s2, s3 );
          cout << "Which statistician gets the result? " << endl;
          get_statistician( s1, s2, s3 ) = left + right;
          cout << "The third statistician is now the sum of the first two."
               << endl;
          break;
        case '*': cout << "Which statistician to multiply? " << endl;
          left = get_statistician( s1, s2, s3 );
          cout << "What value to multiply it by? ";
          cin >> x;
          cout << "Which statistician gets the result? " << endl;
          get_statistician( s1, s2, s3 ) = x * left;
          cout << "The second statistician is now " << x 
               << " times the first." << endl;
          break;
        case 'Q': cout << "No witty saying should ever be said more than once."
                       << endl;
          break;
        default: cout << choice << " is invalid. Sorry." << endl;
        }
    }
  while (cin && (choice != 'Q'));
  
  return EXIT_SUCCESS;
  
}

void print_menu( )
{
    cout << endl;
    cout << "The following choices are available: " << endl;
    cout << " R  Activate one of the reset( ) functions" << endl;
    cout << " 1  Add a new number to the 1st statistician s1" << endl;
    cout << " 2  Add a new number to the 2nd statistician s2" << endl;
    cout << " 3  Add a new number to the 3rd statistician s3" << endl;
    cout << " T  Print a table of values from the statisticians" << endl;
    cout << " E  Test whether two statisticians are ==" << endl;
    cout << " +  Set a statistician to the sum of two statisticians" << endl;
    cout << " *  Set a statistician to the product of a scalar and a"
         << "statistician" << endl;
    cout << " Q  Quit this test program" << endl;
}

char get_user_command( )
// Library facilties used: iostream.h
{
    char command;

    cout << "Enter choice: ";
    cin >> command; 
   
    return command;
}

double get_number( )
// Library facilties used: iostream.h
{
    double result;

    cout << "Please enter the next real number for the sequence: ";
    cin  >> result;
    cout << result << " has been read." << endl;
    return result;
}

void print_values(const statistician& s)
// Library facilties used: iostream.h
{
    cout << setw(10) << s.length( );
    cout << setw(10) << s.sum( );
    if (s.length( ) != 0)
    {
        cout << setw(10) << s.minimum( );
        cout << setw(10) << s.mean( );
        cout << setw(10) << s.maximum( );
    }
    else
        cout << "      none      none      none";
    cout << endl;
}

statistician& get_statistician( statistician& st1, 
                                statistician& st2, 
                                statistician& st3 )
{
  char st_num;
  bool inRange;

  do
    {
      st_num = get_user_command( );
      inRange = ( st_num == '1' ) || ( st_num == '2' ) || ( st_num == '3' );
      if ( ! inRange )
        cout << "Statistician must be between 1 and 3" << endl;
    }  while ( cin && ! inRange );
  switch (st_num)
    {
    case '1': return st1;
    case '2': return st2;
    case '3': return st3;
    }
  
  return st1;
}

