home *** CD-ROM | disk | FTP | other *** search
- -- Chapter 25 - Programming exercise 3
- with Text_IO, Calendar;
- use Text_IO, Calendar;
-
- procedure CH25_3 is
-
- package Int_IO is new Text_IO.Integer_IO(INTEGER);
- use Int_IO;
- package Fix_IO is new Text_IO.Fixed_IO(DAY_DURATION);
- use Fix_IO;
-
- Number_Of_Dogs : INTEGER := 0;
-
- -- The main program adds and deletes
- -- hot dogs to/from the shelf.
-
- task Five_Dogs; -- This task adds five hot dogs to stock
-
- task Remove_Five_Dogs; -- This task deletes five from stock
-
- task How_Many_Are_Left; -- This task lists the number on shelf
-
- task Retail_Hot_Dogs is
- entry Stock_With_A_Hot_Dog; -- This adds a hot dog to stock
- entry Deliver_A_Hot_Dog; -- This deletes one from stock
- end Retail_Hot_Dogs;
-
- task body Retail_Hot_Dogs is
- begin
- for Index in 1..18 loop
- select
- when Number_Of_Dogs < 8 =>
- accept Stock_With_A_Hot_Dog do
- Number_Of_Dogs := Number_Of_Dogs + 1;
- Put("Add a hot dog to the shelf, number =");
- Put(Number_Of_Dogs,3);
- New_Line;
- end Stock_With_A_Hot_Dog;
- or
- when Number_Of_Dogs > 0 =>
- accept Deliver_A_Hot_Dog do
- Put_Line("Remove a hot dog from the shelf");
- Number_Of_Dogs := Number_Of_Dogs - 1;
- end Deliver_A_Hot_Dog;
- end select;
- end loop;
- end Retail_Hot_Dogs;
-
- task body Five_Dogs is
- begin
- for Index in 1..5 loop
- delay 0.1;
- Retail_Hot_Dogs.Stock_With_A_Hot_Dog;
- end loop;
- end Five_Dogs;
-
- task body Remove_Five_Dogs is
- begin
- for Index in 1..5 loop
- delay 0.6;
- Retail_Hot_Dogs.Deliver_A_Hot_Dog;
- end loop;
- end Remove_Five_Dogs;
-
- task body How_Many_Are_Left is
- Time_And_Date : TIME;
- Start, Seconds : DAY_DURATION;
- Year, Month, Day : INTEGER;
- begin
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Start);
- for Index in 1..10 loop
- delay 0.3;
- Time_And_Date := Clock;
- Split(Time_And_Date,Year,Month,Day,Seconds);
- Put("Elapsed time =");
- Put(Seconds - Start,3,3,0);
- Put(", and there are");
- Put(Number_Of_Dogs,3);
- Put_Line(" hotdogs left on the shelf.");
- end loop;
- end How_Many_Are_Left;
-
- begin
- for Index in 1..4 loop
- delay 0.9;
- Retail_Hot_Dogs.Stock_With_A_Hot_Dog;
- Retail_Hot_Dogs.Deliver_A_Hot_Dog;
- end loop;
- end CH25_3;
-
-
-
-
- -- Result of execution (With no changes)
-
- -- Add a hot dog to the shelf, number = 1
- -- Add a hot dog to the shelf, number = 2
- -- Add a hot dog to the shelf, number = 3
- -- Elapsed time = 0.330, and there are 3 hotdogs left on the shelf.
- -- Add a hot dog to the shelf, number = 4
- -- Add a hot dog to the shelf, number = 5
- -- Remove a hot dog from the shelf
- -- Elapsed time = 0.660, and there are 4 hotdogs left on the shelf.
- -- Add a hot dog to the shelf, number = 5
- -- Remove a hot dog from the shelf
- -- Elapsed time = 0.990, and there are 4 hotdogs left on the shelf.
- -- Remove a hot dog from the shelf
- -- Elapsed time = 1.320, and there are 3 hotdogs left on the shelf.
- -- Elapsed time = 1.650, and there are 3 hotdogs left on the shelf.
- -- Remove a hot dog from the shelf
- -- Add a hot dog to the shelf, number = 3
- -- Elapsed time = 1.980, and there are 3 hotdogs left on the shelf.
- -- Remove a hot dog from the shelf
- -- Elapsed time = 2.290, and there are 2 hotdogs left on the shelf.
- -- Remove a hot dog from the shelf
- -- Elapsed time = 2.620, and there are 1 hotdogs left on the shelf.
- -- Elapsed time = 2.950, and there are 1 hotdogs left on the shelf.
- -- Add a hot dog to the shelf, number = 2
- -- Remove a hot dog from the shelf
- -- Remove a hot dog from the shelf
- -- Elapsed time = 3.280, and there are 0 hotdogs left on the shelf.
- -- Add a hot dog to the shelf, number = 1
- -- Remove a hot dog from the shelf
-
-