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

  1.                        -- Chapter 26 - Programming exercise 1
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure CH26_1 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.    task body Johns_Day is
  46.       My_Name : PERSON := JOHN;
  47.    begin
  48.       delay 0.4 * HOURS;
  49.       Restaurant.Eat_A_Meal(My_Name);
  50.       delay 0.4 * HOURS;
  51.       Restaurant.Eat_A_Meal(My_Name);
  52.       delay 4.0 * HOURS;
  53.       Restaurant.Eat_A_Meal(My_Name);
  54.    end Johns_Day;
  55.  
  56.    task body Restaurant is
  57.    begin
  58.       loop
  59.          select
  60.             accept Eat_A_Meal(Customer : PERSON) do
  61.                Put(Customer);
  62.                Put_Line(" is ordering at the restaurant");
  63.                delay 0.5 * HOURS;
  64.                Put(Customer);
  65.                Put_Line(" is eating at the restaurant");
  66.                delay 0.5 * HOURS;
  67.             end Eat_A_Meal;
  68.          or
  69.             terminate;
  70.          end select;
  71.       end loop;
  72.    end Restaurant;
  73.  
  74.    task body Burger_Boy is
  75.    begin
  76.       loop
  77.          select
  78.             accept Eat_A_Meal(Customer : PERSON) do
  79.                Put(Customer);
  80.                Put_Line(" is ordering at the Burger Boy");
  81.                delay 0.1 * HOURS;
  82.                Put(Customer);
  83.                Put_Line(" is eating at the Burger Boy");
  84.                delay 0.1 * HOURS;
  85.             end Eat_A_Meal;
  86.          or
  87.             terminate;
  88.          end select;
  89.       end loop;
  90.    end Burger_Boy;
  91.  
  92. begin
  93.    null;
  94. end CH26_1;
  95.  
  96.  
  97.  
  98.  
  99. -- Result of execution
  100.  
  101. -- JOHN is ordering at the restaurant
  102. -- JOHN is eating at the restaurant
  103. -- BILL is ordering at the Burger Boy
  104. -- BILL is eating at the Burger Boy
  105. -- JOHN is ordering at the restaurant
  106. -- BILL is ordering at the Burger Boy
  107. -- BILL is eating at the Burger Boy
  108. -- JOHN is eating at the restaurant
  109. -- BILL is ordering at the restaurant
  110. -- BILL is eating at the restaurant
  111. -- JOHN is ordering at the restaurant
  112. -- JOHN is eating at the restaurant
  113.  
  114.