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

  1.                                     -- Chapter 25 - Program 5
  2. with Text_IO;
  3. use Text_IO;
  4.  
  5. procedure Retail2 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 Retail_Hot_Dogs is
  20.       entry Stock_With_A_Hot_Dog;  -- This adds a hot dog to stock
  21.       entry Deliver_A_Hot_Dog;     -- This deletes one from stock
  22.    end Retail_Hot_Dogs;
  23.  
  24.    task body Retail_Hot_Dogs is
  25.    begin
  26.       for Index in 1..18 loop
  27.          Put("In loop => ");
  28.          select
  29.             when Number_Of_Dogs < 8 =>
  30.             accept Stock_With_A_Hot_Dog do
  31.                Number_Of_Dogs := Number_Of_Dogs + 1;
  32.                Put("Add a hot dog to the shelf, number =");
  33.                Put(Number_Of_Dogs,3);
  34.                New_Line;
  35.             end Stock_With_A_Hot_Dog;
  36.          or
  37.             when Number_Of_Dogs > 0 =>
  38.             accept Deliver_A_Hot_Dog do
  39.                Put_Line("Remove a hot dog from the shelf");
  40.                Number_Of_Dogs := Number_Of_Dogs - 1;
  41.             end Deliver_A_Hot_Dog;
  42.          end select;
  43.       end loop;
  44.    end Retail_Hot_Dogs;
  45.  
  46.    task body Five_Dogs is
  47.    begin
  48.       for Index in 1..5 loop
  49.          delay 0.1;
  50.          Retail_Hot_Dogs.Stock_With_A_Hot_Dog;
  51.       end loop;
  52.    end Five_Dogs;
  53.  
  54.    task body Remove_Five_Dogs is
  55.    begin
  56.       for Index in 1..5 loop
  57.          delay 0.6;
  58.          Retail_Hot_Dogs.Deliver_A_Hot_Dog;
  59.       end loop;
  60.    end Remove_Five_Dogs;
  61.  
  62. begin
  63.    for Index in 1..4 loop
  64.       delay 0.9;
  65.       Retail_Hot_Dogs.Stock_With_A_Hot_Dog;
  66.       Retail_Hot_Dogs.Deliver_A_Hot_Dog;
  67.    end loop;
  68. end Retail2;
  69.  
  70.  
  71.  
  72.  
  73. -- Result of execution (With no changes)
  74.  
  75. -- Add a hot dog to the shelf, number =  1
  76. -- Add a hot dog to the shelf, number =  2
  77. -- Add a hot dog to the shelf, number =  3
  78. -- Add a hot dog to the shelf, number =  4
  79. -- Add a hot dog to the shelf, number =  5
  80. -- Remove a hot dog from the shelf
  81. -- Add a hot dog to the shelf, number =  5
  82. -- Remove a hot dog from the shelf
  83. -- Remove a hot dog from the shelf
  84. -- Remove a hot dog from the shelf
  85. -- Add a hot dog to the shelf, number =  3
  86. -- Remove a hot dog from the shelf
  87. -- Remove a hot dog from the shelf
  88. -- Add a hot dog to the shelf, number =  2
  89. -- Remove a hot dog from the shelf
  90. -- Remove a hot dog from the shelf
  91. -- Add a hot dog to the shelf, number =  1
  92. -- Remove a hot dog from the shelf
  93.  
  94.  
  95.  
  96.  
  97.  
  98. -- Result of execution (With line 29 changed so limit is 3)
  99.  
  100. -- Add a hot dog to the shelf, number =  1
  101. -- Add a hot dog to the shelf, number =  2
  102. -- Add a hot dog to the shelf, number =  3
  103. -- Remove a hot dog from the shelf
  104. -- Add a hot dog to the shelf, number =  3
  105. -- Remove a hot dog from the shelf
  106. -- Add a hot dog to the shelf, number =  3
  107. -- Remove a hot dog from the shelf
  108. -- Add a hot dog to the shelf, number =  3
  109. -- Remove a hot dog from the shelf
  110. -- Add a hot dog to the shelf, number =  3
  111. -- Remove a hot dog from the shelf
  112. -- Remove a hot dog from the shelf
  113. -- Add a hot dog to the shelf, number =  2
  114. -- Remove a hot dog from the shelf
  115. -- Remove a hot dog from the shelf
  116. -- Add a hot dog to the shelf, number =  1
  117. -- Remove a hot dog from the shelf
  118.  
  119.  
  120.  
  121.  
  122.  
  123. -- Result of execution (With delays swapped in lines 49 and 57)
  124.  
  125. -- Add a hot dog to the shelf, number =  1
  126. -- Remove a hot dog from the shelf
  127. -- Add a hot dog to the shelf, number =  1
  128. -- Remove a hot dog from the shelf
  129. -- Add a hot dog to the shelf, number =  1
  130. -- Remove a hot dog from the shelf
  131. -- Add a hot dog to the shelf, number =  1
  132. -- Remove a hot dog from the shelf
  133. -- Add a hot dog to the shelf, number =  1
  134. -- Remove a hot dog from the shelf
  135. -- Add a hot dog to the shelf, number =  1
  136. -- Remove a hot dog from the shelf
  137. -- Add a hot dog to the shelf, number =  1
  138. -- Remove a hot dog from the shelf
  139. -- Add a hot dog to the shelf, number =  1
  140. -- Remove a hot dog from the shelf
  141. -- Add a hot dog to the shelf, number =  1
  142. -- Remove a hot dog from the shelf
  143.  
  144.