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

  1.                         -- Chapter 25 - Programming exercise 2
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure CH25_2 is
  6.  
  7.    package Int_IO is new Text_IO.Integer_IO(INTEGER);
  8.    use Int_IO;
  9.  
  10.    Number_Of_Dogs : INTEGER := 0;
  11.  
  12.                           -- The main program adds and deletes
  13.                           -- hot dogs to/from the shelf.
  14.  
  15.    task Five_Dogs;        -- This task adds five hot dogs to stock
  16.  
  17.    task Remove_Five_Dogs; -- This task deletes five from stock
  18.  
  19.    task How_Many_Are_Left; -- This task lists the number on shelf
  20.  
  21.    task Retail_Hot_Dogs is
  22.       entry Stock_With_A_Hot_Dog;  -- This adds a hot dog to stock
  23.       entry Deliver_A_Hot_Dog;     -- This deletes one from stock
  24.    end Retail_Hot_Dogs;
  25.  
  26.    task body Retail_Hot_Dogs is
  27.    begin
  28.       for Index in 1..18 loop
  29.          select
  30.             when Number_Of_Dogs < 8 =>
  31.             accept Stock_With_A_Hot_Dog do
  32.                Number_Of_Dogs := Number_Of_Dogs + 1;
  33.                Put("Add a hot dog to the shelf, number =");
  34.                Put(Number_Of_Dogs,3);
  35.                New_Line;
  36.             end Stock_With_A_Hot_Dog;
  37.          or
  38.             when Number_Of_Dogs > 0 =>
  39.             accept Deliver_A_Hot_Dog do
  40.                Put_Line("Remove a hot dog from the shelf");
  41.                Number_Of_Dogs := Number_Of_Dogs - 1;
  42.             end Deliver_A_Hot_Dog;
  43.          end select;
  44.       end loop;
  45.    end Retail_Hot_Dogs;
  46.  
  47.    task body Five_Dogs is
  48.    begin
  49.       for Index in 1..5 loop
  50.          delay 0.1;
  51.          Retail_Hot_Dogs.Stock_With_A_Hot_Dog;
  52.       end loop;
  53.    end Five_Dogs;
  54.  
  55.    task body Remove_Five_Dogs is
  56.    begin
  57.       for Index in 1..5 loop
  58.          delay 0.6;
  59.          Retail_Hot_Dogs.Deliver_A_Hot_Dog;
  60.       end loop;
  61.    end Remove_Five_Dogs;
  62.  
  63.    task body How_Many_Are_Left is
  64.    begin
  65.       for Index in 1..10 loop
  66.          delay 0.3;
  67.          Put("There are");
  68.          Put(Number_Of_Dogs,3);
  69.          Put_Line(" hotdogs left on the shelf.");
  70.       end loop;
  71.    end How_Many_Are_Left;
  72.  
  73. begin
  74.    for Index in 1..4 loop
  75.       delay 0.9;
  76.       Retail_Hot_Dogs.Stock_With_A_Hot_Dog;
  77.       Retail_Hot_Dogs.Deliver_A_Hot_Dog;
  78.    end loop;
  79. end CH25_2;
  80.  
  81.  
  82.  
  83.  
  84. -- Result of execution (With no changes)
  85.  
  86. -- Add a hot dog to the shelf, number =  1
  87. -- Add a hot dog to the shelf, number =  2
  88. -- Add a hot dog to the shelf, number =  3
  89. -- There are  3 hotdogs left on the shelf.
  90. -- Add a hot dog to the shelf, number =  4
  91. -- Add a hot dog to the shelf, number =  5
  92. -- Remove a hot dog from the shelf
  93. -- There are  4 hotdogs left on the shelf.
  94. -- Add a hot dog to the shelf, number =  5
  95. -- Remove a hot dog from the shelf
  96. -- There are  4 hotdogs left on the shelf.
  97. -- Remove a hot dog from the shelf
  98. -- There are  3 hotdogs left on the shelf.
  99. -- There are  3 hotdogs left on the shelf.
  100. -- Remove a hot dog from the shelf
  101. -- Add a hot dog to the shelf, number =  3
  102. -- There are  3 hotdogs left on the shelf.
  103. -- Remove a hot dog from the shelf
  104. -- There are  2 hotdogs left on the shelf.
  105. -- Remove a hot dog from the shelf
  106. -- There are  1 hotdogs left on the shelf.
  107. -- There are  1 hotdogs left on the shelf.
  108. -- Add a hot dog to the shelf, number =  2
  109. -- Remove a hot dog from the shelf
  110. -- Remove a hot dog from the shelf
  111. -- There are  0 hotdogs left on the shelf.
  112. -- Add a hot dog to the shelf, number =  1
  113. -- Remove a hot dog from the shelf
  114.  
  115.