home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog4 / answers / ch26_2.ada < prev    next >
Encoding:
Text File  |  1991-07-01  |  2.9 KB  |  123 lines

  1.                        -- Chapter 26 - Programming exercise 2
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure CH26_2 is
  6.  
  7.    HOURS : constant := 1;
  8.    type PERSON is (BILL, JOHN);
  9.  
  10.    package Enum_IO is new Text_IO.Enumeration_IO(PERSON);
  11.    use Enum_IO;
  12.  
  13.    task Bills_Day;
  14.  
  15.    task Johns_Day;
  16.  
  17.    task Restaurant is
  18.       entry Eat_A_Meal(Customer : PERSON);
  19.    end Restaurant;
  20.  
  21.    task Burger_Boy is
  22.       entry Eat_A_Meal(Customer : PERSON);
  23.    end Burger_Boy;
  24.  
  25.    task body Bills_Day is
  26.       My_Name : PERSON := BILL;
  27.    begin
  28.       delay 1.0 * HOURS;
  29.       select
  30.          Restaurant.Eat_A_Meal(My_Name);
  31.       else
  32.          Burger_Boy.Eat_A_Meal(My_Name);
  33.       end select;
  34.       delay 1.0 * HOURS;
  35.       select
  36.          Restaurant.Eat_A_Meal(My_Name);
  37.       or
  38.          delay 0.1 * HOURS;
  39.          Burger_Boy.Eat_A_Meal(My_Name);
  40.       end select;
  41.       delay 1.0 * HOURS;
  42.       Restaurant.Eat_A_Meal(My_Name);
  43.    end Bills_Day;
  44.  
  45.    procedure Johns_Choice(Name : PERSON) is
  46.    begin
  47.       select
  48.          Burger_Boy.Eat_A_Meal(Name);
  49.       else
  50.          Restaurant.Eat_A_Meal(Name);
  51.       end select;
  52.    end;
  53.  
  54.    task body Johns_Day is
  55.       My_Name : PERSON := JOHN;
  56.    begin
  57.       delay 0.4 * HOURS;
  58.       Johns_Choice(My_Name);
  59.       delay 0.4 * HOURS;
  60.       Johns_Choice(My_Name);
  61.       delay 4.0 * HOURS;
  62.       Johns_Choice(My_Name);
  63.    end Johns_Day;
  64.  
  65.    task body Restaurant is
  66.    begin
  67.       loop
  68.          select
  69.             accept Eat_A_Meal(Customer : PERSON) do
  70.                Put(Customer);
  71.                Put_Line(" is ordering at the restaurant");
  72.                delay 0.5 * HOURS;
  73.                Put(Customer);
  74.                Put_Line(" is eating at the restaurant");
  75.                delay 0.5 * HOURS;
  76.             end Eat_A_Meal;
  77.          or
  78.             terminate;
  79.          end select;
  80.       end loop;
  81.    end Restaurant;
  82.  
  83.    task body Burger_Boy is
  84.    begin
  85.       loop
  86.          select
  87.             accept Eat_A_Meal(Customer : PERSON) do
  88.                Put(Customer);
  89.                Put_Line(" is ordering at the Burger Boy");
  90.                delay 0.1 * HOURS;
  91.                Put(Customer);
  92.                Put_Line(" is eating at the Burger Boy");
  93.                delay 0.1 * HOURS;
  94.             end Eat_A_Meal;
  95.          or
  96.             terminate;
  97.          end select;
  98.       end loop;
  99.    end Burger_Boy;
  100.  
  101. begin
  102.    null;
  103. end CH26_2;
  104.  
  105.  
  106.  
  107.  
  108. -- Result of execution
  109.  
  110. -- JOHN is ordering at the Burger Boy
  111. -- JOHN is eating at the Burger Boy
  112. -- BILL is ordering at the restaurant
  113. -- JOHN is ordering at the Burger Boy
  114. -- JOHN is eating at the Burger Boy
  115. -- BILL is eating at the restaurant
  116. -- BILL is ordering at the restaurant
  117. -- BILL is eating at the restaurant
  118. -- BILL is ordering at the restaurant
  119. -- JOHN is ordering at the Burger Boy
  120. -- JOHN is eating at the Burger Boy
  121. -- BILL is eating at the restaurant
  122.  
  123.