program TVProg;
{Figures out TV watching statistics}
{Ronald Begg}

uses Crt;


var
   num : integer;
   highestDayName,highestAvgName : string[30];
   totalAvg,highestDay,highestAvg : real;

procedure pageBorder;    {This procedure puts a border around the page}
var
   i : integer;
begin
   textColor(12);
   for i := 1 to 79 do
   begin
      gotoXY(i,1); write('Û');
      gotoXY(i,25);write('Û');
   end;
   for i := 1 to 25 do
   begin
      gotoXY(1,i); write('Û');
      gotoXY(79,i);write('Û');
   end;
end;

function day (number : integer): string;
{This function returns the day name from a number 1-7}
begin
   case number of
   1: day := 'Monday';
   2: day := 'Tuesday';
   3: day := 'Wednesday';
   4: day := 'Thursday';
   5: day := 'Friday';
   6: day := 'Saturday';
   7: day := 'Sunday';
   end;
end;

procedure questions;
label query;
var
   i,code : integer;
   studentName : string[30];
   stringHours : string;
   studentAvg,hours : real;
begin
      studentAvg := 0;    {I put this in because pascal was giving huge}
      clrscr;             {numbers for the studentAvg if I didn't do this}
      textColor(15);
      num := num + 1;                    {Add one for the extra student}
      write ('Please enter your name: ');
      readln (studentName);    {Get the student name}
      for i := 1 to 7 do        {Get the hours of tv watched for each day}
         begin
         query:
         write ('Please enter the hours of TV you watched on ');
         textColor(11);                   {Make the days appear blue}
         write (day(i));
         textColor(15);
         write (' :');
         readln (stringHours);
         val(stringHours,hours,code);
         if (hours = 0) and (stringHours[1] <> '0') then
            begin              {Check if a number was entered}
            writeln ('Please enter a number as your input!');
            while not keypressed do; {Wait for a key to be pressed}
            readKey;                 {Clear the keyboard buffer}
            goto query;
            end
         else if (hours > 24) or (hours < 0) then
            begin               {Check if number is valid}
            writeln ('Please enter a value between 0 and 24!');
            while not keypressed do;
            readKey;
            goto query;
            end;
         if hours > highestDay then
            begin
            highestDay := hours;
            highestDayName := studentName;
            end;
         studentAvg := studentAvg + hours;{Add hours to the avg}
         end;
      studentAvg := studentAvg / 7;  {Divide the total by seven to get Avg}
      totalAvg := (totalAvg * (num-1) + studentAvg)/num; {Get the new total average}

      if studentAvg > highestAvg then
         begin
         highestAvg := studentAvg;
         highestAvgName := studentName;
         end;
      {Output for the student}
      textColor(14);
      writeln;
      writeln;
      writeln ('The average hours of TV watched a day for ',studentName);
      writeln ('was ', studentAvg:0:1,' hours a day (1dp)');
      while not keyPressed do;  {Wait for key to be pressed}
      readKey;                  {Clear the keyboard buffer}
   end;

procedure overall;
begin
   clrscr;
   pageborder;
   textColor(15);
   gotoXY(5,8);     {Show overall things like--number of students}
   write('The total number of students interviewed was: ',num);
   gotoXY(5,10);                              {--Highest hours in a day}
   write('The highest recorded time for one day was   : ',highestDay:0:1,' hrs');
   gotoXY(5,11);
   write('This time was achieved by: ',highestDayName);
   gotoXY(5,13);                              {--Highest average hours}
   write('The highest recorded average time was       : ',highestAvg:0:1,' hrs a day');
   gotoXY(5,14);
   write('This average was achieved by: ',highestAvgName);
   gotoXY(5,16);
   write('The average amount of TV watched a day, overall, was: ', totalAvg:0:1, ' hrs a day');
   while not keypressed do;   {Wait for a key to be pressed}
   readKey;                 {Clear keyboard buffer}
end;

procedure clearStats;    {As you can see, this clears all the overall stats}
begin
   num := 0;
   totalAvg := 0;
   highestDay := 0;
   highestAvg := 0;
   highestDayName := '';
   highestAvgName := '';
   clrscr;
   pageborder;
   textColor (14);
   gotoXY(25,12);
   write ('THE STATS HAVE BEEN CLEARED');
   textColor(12);
   gotoXY (1,25); {Hide the cursor in the page border}
   while not keypressed do; {Wait for a key to be pressed}
   readKey;                 {Clear the keyboard buffer}
end;

procedure showMainMenu(option : integer);
begin
   {This shows the menu highlighting the selected option}
   textColor (14);
   textBackground (12);
   gotoXY(20,9);  write('           TV WATCHING STATS           ');
   textBackground (3);
   if option = 1 then textBackground (5);
   gotoXY(20,10); write('            Another Student            ');
   textBackground(3);
   if option = 2 then textBackground (5);
   gotoXY(20,11); write('           View Overall Stats          ');
   textBackground(3);
   if option = 3 then textBackground (5);
   gotoXY(20,12); write('           Clear Overall Stats         ');
   textBackground(3);
   if option = 4 then textBackground (5);
   gotoXY(20,13); write('                  Exit                 ');
   textBackground(0);
   textColor(12); gotoXY(1,25); {hide the cursor in the page border}
end;

var
   option : integer;
   key : char;
begin
   option := 1;
   clrscr;
   pageBorder;
while 1=1 do
   begin
   textBackground (0);
   showMainMenu(option);

   while not keypressed do;
   key := readKey;

   case key of
   'H':     begin               {If the up arrow is pressed go up one}
            option := option - 1;
            if option < 1 then option := 1;
            end;
   'P':     begin               {If the down arrow is pressed go down one}
            option := option + 1;
            if option > 4 then option := 4;
            end;
    chr(13),' ': begin {If enter is pressed then do the current option}
                    case option of
                    1: questions;
                    2: overall;
                    3: clearstats;
                    4: halt;
                    end;
                 clrscr;
                 pageborder;
                 end;
        end;
    end;
end.

