#!/usr/bin/perl -w
######################
#
# read-and-report.pl
#
# Read in names and ages from the terminal.
# Report various properties from the stored data.
#
# Here's what running the program should look like.
# The lines starting with ">" are output by the computer;
# the others are typed by the user.
#
#   > Read and Report (version 0.2)
#   > What would you like to do ? ( input, summarize, list, clear, quit )
#   Go fly a kite.
#   >   Oops - I don't know how to '$choice'.
#   > What would you like to do ? ( input, summarize, list, clear, quit )
#   input
#   >   Enter a name. (Or a blank line to go back to the main menu.)
#   John Smith
#   >   How old is 'John Smith'?
#   32
#   >   Enter a name. (Or a blank line to go back to the main menu.)
#   Jane Doe
#   > How old is 'Jane Doe'?
#   82
#   >   Enter a name. (Or a blank line to go back to the main menu.)
#   Ducky
#   >   How old is 'Ducky'?
#   9
#   >   Enter a name. (Or a blank line to go back to the main menu.)
# 
#   > What would you like to do ? ( input, summarize, list, clear, quit )
#   summarize
#   >     There are 2 people in the list.
#   >     The oldest is Jane Doe, 82.
#   >     The youngest is Ducky, 9.
#   >     Their average age is 41.
#   > What would you like to do ? ( input, summarize, list, clear, quit )
#   list 
#   >   List all people older than what age?
#   20
#   >   The people older than 20 are
#   >     John Smith
#   >     Jane Doe
#   > What would you like to do ? ( input, summarize, list, clear, quit )
#   clear
#   >   List cleared.
#   > What would you like to do ? ( input, summarize, list, clear, quit )
#   summarize
#   >   The list is empty.
#   > What would you like to do ? ( input, summarize, list, clear, quit )
#   quit
#   > Bye.
#
# Version history
#
#   * 0.1_jm  9/15 partial program outline created
#   * 0.2_jim 9/22 filled in the blanks
#
######################
use strict;
my $VERSION = "0.2_jm";    # Change this to a higher number with your initials.
my $DEBUG = 1;             # 1 => print debugging diagnostics; 0 => don't.

print "Read and Report (version $VERSION). \n\n";

# Store the ( name => age) data in this hash.
# For example, 
#     $name_age_data{'Belgarath the Sorcerer'} = 7024;
my %name_age_data;

my $is_running = 1;
while ($is_running) {

  # Ask user what to do, and read in the choice.
  print "> What would you like to do ? ";
  print "( input, summarize, list, clear, quit )\n";
  my $choice = <>;            # Read in one line from the terminal.
  chomp($choice);             # Remove the carriage return.

  # Do different things depending on the user's choice:

  #  -- input --
  if ($choice eq 'input'){

    my $doing_input = 1;
    while ($doing_input){       # LOOP over name/age inputs.     
      print ">   Enter a name.";
      print " (Or a blank line to go back to the main menu.)\n";
      my $name = <>;
      chomp($name);
      if (not $name){
        $doing_input = 0;       # time to stop this input loop.
      }
      else {
        print ">   How old is '$name'?\n";
        my $age = <>;
        chomp($age);
        $name_age_data{$name} = $age;    # remember this pair.
      }
    }
  }

  #  -- list --
  elsif ($choice eq 'list'){
    print ">   List all people older than what age?\n";
    my $cutoff = <>;
    chomp($cutoff);
    print ">   The people older than $cutoff are\n";
    foreach my $key (keys %name_age_data){
      if ( $name_age_data{$key} > $cutoff ){
        print ">     $key\n";
      } 
    }
  }

  #  -- clear --
  elsif ($choice eq 'clear'){
    %name_age_data = ();
  }

  #  -- summarize --
  elsif ($choice eq 'summarize'){
    my $how_many = scalar(keys %name_age_data);
    if ($how_many == 0){
      print ">   The list is empty.\n";
    }
    else {
      my $oldest   = '';
      my $youngest = '';
      my $old_age  = -1;      # smaller 
      my $young_age= 1e10;    # bigger than anyone's age.
      my $average  = 0.0;
      for my $key (keys %name_age_data){                   # loop over hash
        if ( $name_age_data{$key} > $old_age ){            # find oldest
          $oldest = $key;
          $old_age = $name_age_data{$key};
        }
        if ( $name_age_data{$key} < $young_age ){          # find youngest
          $youngest = $key;
          $young_age  = $name_age_data{$key};
        }
        $average += $name_age_data{$key};
      }
      $average /= $how_many;                        # average is sum/how_many.
      print ">     There are $how_many people in the list.\n";
      print ">     The oldest is $oldest, $old_age.\n";
      print ">     The youngest is $youngest, $young_age.\n";
      print ">     Their average age is $average.\n";
    }
  }

  #  -- quit --
  elsif ($choice eq 'quit'){
    $is_running = 0;
  }

  #  -- complain if the choice wasn't understood --
  else {
    print ">   Oops - I don't know how to '$choice'.\n";
  }

}
print "> Bye.\n";
