//////// CST 112 -- Project 7 ////////

int many=20;
Person[] people = new Person[20];    // Array of people
String[] input=  new String[20];     // Data input:  List of names, with "M," or F," preceding.

int year=2010;
String title="";


void setup()
{
  size( 800, 600 );
  input = loadStrings( "names.txt" );        // Get names.  (See p.329-330)
  fill( people, many );
}

void draw()
//// Draw frame:  show bar charts, based on key pressed.
{
  background( 255,255, 200 );
  if (key == '?')
  {
    help();
  } else {
    text( title, width/3, 20 );
    text( "Press the ? key for help", width*2/3, 30);
    text( year, 20, 20 );
    showData( people, many );
  }
}
//// MAKE BUTTONS FOR THESE ////
void keyPressed()        //// Check which key was pressed.
{
  if (key == 'f')         //// Fill with new values.
  {    fill(people, many);  }
  if (key == 'a')        //// Sort by age 
  {   title=  "SORTED BY AGE";      sortByAge( people, many );  }
  if (key == 'h')        //// Sort by height 
  {    title=  "SORTED BY HEIGHT";   sortByHeight( people, many );  }
  if (key == 'w')        //// Sort by height 
  {    title=  "SORTED BY WEIGHT";    sortByWeight( people, many );  }
  if (key == 'b')        //// Sort by BMI 
  {    title=  "SORTED BY BMI";    sortByBMI( people, many );  }
  if (key == 'q')        //// Quit!
  {    exit();  }
  if (key == 'y')        //// Next year.
  {
    year++;
    for (int i=0; i<many; i++)
    {      people[i].nextYear();    }
  }
}
void help()        //// Explain which keys do what..
{
  float x=width/2, y=2;
  text( "KEYS:", x, 12*y++ );
  text( "f:  Fill the people array  with new values", x, 12*y++ );
  text( "a:  Sort by age", x, 12*y++ );
  text( "h:  Sort by height", x, 12*y++ );
  text( "w:  Sort by weight", x, 12*y++ );
  text( "b:  Sort by BMI", x, 12*y++ );
  text( "q:  Quit", x, 12*y++ );
  text( "y:  NEXT YEAR", x, 12*y++ );
}


//////// METHODS ////////
void fill( Person [] p, int many )
//// Fill the array of people, using random values.
{
  for (int i=0; i<many; i++)
  {
    //// Parse the next line of input.
    String[] mfname=  new String[2];
    mfname=  split(input[i], " ");
    println( mfname[0] );
    //// Create a random person.
    p[i]=  new Person( i, mfname[0].equals("M"), mfname[1] );

  }
}
void showData( Person[] p, int many )
//// Display
{
  int x=10, y=50;
  // First, show the labels.
  people[0].showLabels(x,y);
  y += 22;
  // Now, display the data for each element of the array.
  for (int i=0; i<many; i++)
  {
    p[i].show( x, y);
    y += 20;
  }
}




//////// SORTING METHODS ////////
void sortByHeight( Person[] p, int many )  // Sort by height
{
    WriteYourOwn.sortByHeight( p, many );
}
void sortByWeight( Person[] p, int many )  // Sort by weight
{
    WriteYourOwn.sortByWeight( p, many );
}
void sortByAge( Person[] p, int many )  // Sort by age
{
    WriteYourOwn.sortByAge( p, many );
}
void sortByBMI( Person[] p, int many )  // Sort by BMI
{
    WriteYourOwn.sortByBMI( p, many );
}


//// HOW TO CALCULATE THE BMI INDEX ////
  /*
  Formula: weight (lb) / [height (in)]2 x 703
  Calculate BMI by dividing weight in pounds (lbs) by height in inches (in) squared 
  and multiplying by a conversion factor of 703.
  */



