home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / ada / library / bd3 / bd3.spr < prev    next >
Text File  |  1989-04-30  |  54KB  |  914 lines

  1. STYLE LeftMargin 12,RightMargin 90,offset 0,Paper 11 in,PaperWidth 8.5 in,BottomMargin 6,TopMargin 6
  2. R 80,J,T 5
  3. CENTERPAGE .5 page
  4. BANK DEMO 3
  5.  
  6. A Demonstration of Tasking in Ada
  7.  
  8.  
  9.  
  10.  
  11. Version 1.0, 11 March 1989
  12.  
  13. by
  14. Richard Conn
  15.  
  16.  
  17.  
  18.  
  19.  
  20.     Bank Demo 3, or BD3 as the files and mainline procedure are named, is ademonstration program which illustrates how Ada and its tasking mechanism can beused to create a model of an activity in the real world.  BD3 is a model of abank, where the bank contains several tellers, implemented as Ada tasks, and isused by customers, also implemented as Ada tasks.  The user at the console canmonitor the status of the bank and the accounts of its customers and cause morecustomer tasks to execute, thereby increasing the level of activity at the bank.The tasks all operate in parallel, so a variety of events can be thought of ashappening at the same time, controlled by the Ada runtime system which is builtinto the Ada program by the Ada compilation system.
  21.  
  22.     This document and the associated software are presented as an introductionto the Ada language and its tasking capabilities.  While the author attempted tomake this document as easy to follow as possible, a limited knowledge of Ada isassumed.  Variable assignments, type definitions, and procedure and packagespecifications are presented in correct Ada syntax without much explanation.  Ifthis proves to be a problem, the ADA-TUTR shareware program will help a lot inbringing the user up to date with an understanding of Ada syntax.  Regardless,the program in the file BD3.EXE can be run without any knowledge of Ada at all,and the tasking demonstration can be observed.
  23.  
  24.  
  25. SET page=1
  26. BEGIN HEADER
  27. BD3 - A Demonstration of Tasking in Ada
  28. END HEADER
  29.  
  30. BANK DEMO 3
  31. A Demonstration of Tasking in Ada
  32.  
  33. by Richard Conn
  34.  
  35.  
  36. RESERVE 4
  37. SECTION Introduction
  38.  
  39.     BD3 is a model of a bank.  The Ada package called BANK represents the bankitself, and the bank contains four tellers, represented by an array, namedTELLER, of four tasks.  BD3 is written in an object-oriented fashion in the Adaprogramming language.
  40.  
  41. BEGIN FIGURE
  42. ======================================================================
  43.  
  44.   ADD_NEW_CUSTOMER
  45.     -- to create a new account for a customer and return
  46.     -- an ID for that customer
  47.   GET_BALANCE
  48.     -- to return the balance of a customer's account
  49.   MAKE_DEPOSIT
  50.     -- to deposit money into a customer's account
  51.   MAKE_WITHDRAWAL
  52.     -- to withdraw money from a customer's account
  53.  
  54. CAPTION The bank tellers can process four kinds of transactions.
  55. TAG TRANSACTION=figure
  56.  
  57. ======================================================================
  58. END FIGURE
  59.  
  60.     Each task, TELLER(1) to TELLER(4), supports one entry point, calledREQUEST, which is used by tasks outside the BANK package to communicate with aTELLER(n) task and request a transaction.  Four transaction requests arerecognized by a TELLER(n) task, as shown in Figure TRANSACTION.
  61.  
  62. BEGIN FIGURE
  63. ======================================================================
  64.  
  65.   MY_ID : BANK.CUSTOMER_ID;
  66.     -- MY_ID is a variable which holds the customer ID of
  67.     -- this task (type CUSTOMER_ID is defined in the package BANK)
  68.   AMOUNT : BANK.DOLLAR;
  69.     -- type DOLLAR represents the unit of currency, as defined
  70.     -- by the package BANK, and the variable AMOUNT is used to
  71.     -- hold the money passed in to or out of the TELLER(n) task
  72.      ...
  73.   BANK.TELLER(1).REQUEST (MY_ID, BANK.ADD_NEW_CUSTOMER, AMOUNT);
  74.     -- this calls the REQUEST entry point for the task TELLER(1)
  75.     -- in the package BANK; the ID of the customer is passed
  76.     -- out of the TELLER(1) task in the variable MY_ID and the
  77.     -- requested transaction is ADD_NEW_CUSTOMER, which is
  78.     -- defined in the package BANK; the variable AMOUNT is
  79.     -- passed as a parameter but not used
  80.   AMOUNT := 100.00;
  81.   BANK.TELLER(2).REQUEST (MY_ID, BANK.MAKE_DEPOSIT, AMOUNT);
  82.     -- the variable AMOUNT is set to $100, and the REQUEST
  83.     -- entry point of TELLER(2) in the package BANK is
  84.     -- called; the variable MY_ID, which was set by the call
  85.     -- to TELLER(1) above, is used to identify the customer,
  86.     -- and the requested action is MAKE_DEPOSIT, as defined by
  87.     -- the package BANK; AMOUNT is passed to the TELLER(2) task
  88.     -- as the amount to deposit
  89.  
  90. CAPTION This code segment shows that the TELLER(n) tasks are accessed likeprocedures.
  91. TAG CALLTELLER=figure
  92.  
  93. ======================================================================
  94. END FIGURE
  95.  
  96.     Each TELLER(n) task performs one of these four transactions at a time.  Anexternal task calls (makes a rendezvous with, in Ada terminology) a TELLER(n)task through its REQUEST entry point, passing one of these transaction names asa parameter, and the TELLER(n) task processes the request.  For example, a codesegment in an external task may look something like Figure CALLTELLER.  Thiscode segment was extracted from the body of the CUSTOMER task definition in thepackage CUSTOMER_WORLD (see later).
  97.     This demonstration program also contains several other tasks, calledcustomer tasks.  These tasks, hidden inside the package CUSTOMER_WORLD, operateindependently and interact with the various teller tasks.  The customer tasksmake requests to the teller tasks, asking to be added as a customer of the bank,making deposits, and making withdrawals.  Using the Ada inter-task rendezvousmechanism to communicate, if the teller task is already servicing anothercustomer task, the customer task "waits in line" (having been placed in theimplicit queue associated with the teller's entry point).  If queued, thecustomer task is serviced after the tasks in the teller's queue in front of himhave been serviced.  Indeed, the model is similar to the real world.
  98.     From the mainline procedure, the user can monitor the bank and itsactivity.  He can obtain a status report from the bank (which includes thebalanace of each customer, the number of transactions requested by eachcustomer, and the number of transactions processed by each teller), cause newcustomer tasks to start up, and shut down the system and exit the program.  Forthose Ada compilers which do not allow tasking to proceed while the current taskis waiting for input (such as many of the Ada compilers available for the IBMPC), a continuous display function is also provided to the user which displaysthe bank's status, delays five seconds (allowing the various tasks to run), anddisplays the bank's status again.  This command option displays ten statusreports before returning the user to his prompt.
  99. RESERVE 4
  100. SECTION Execution of the Program
  101.  
  102.     Figures EX1 to EX7 show various displays during the execution of theprogram.  These displays were generated on a SUN workstation, where the programwas compiled by the Verdix Ada compiler.  These displays may be different fromwhat you see if you compile the program on an IBM PC, particularly in thatVerdix Ada does not suspend all tasks while the current task is waiting forinput from the console.  While we are at the prompt in these figures, all tasksare running in parallel with our input activity.  On some compilers this willnot be the case, and the parallel operation of the tasks will not be seen untilthe user issues the 'c' (continuous bank status) command.
  103.  
  104. BEGIN FIGURE
  105. ======================================================================
  106.  
  107. Enter
  108.   b for bank status
  109.   c for continuous bank status
  110.   s for start next customer
  111.   x for exit: b
  112. The bank doesn't have any customers
  113.  
  114. CAPTION When BD3 starts, there are no customers.
  115. TAG EX1=figure
  116.  
  117. ======================================================================
  118. END FIGURE
  119.  
  120.     Figure EX1 shows the menu that is presented to the user when the programstarts and that no customer tasks are running yet.
  121.  
  122. BEGIN FIGURE
  123. ======================================================================
  124.  
  125. Enter
  126.   b for bank status
  127.   c for continuous bank status
  128.   s for start next customer
  129.   x for exit: s
  130. Enter
  131.   b for bank status
  132.   c for continuous bank status
  133.   s for start next customer
  134.   x for exit: b
  135. **** BANK STATUS REPORT ****
  136. Customer      Balance  Transactions  Attempted_Overdraws
  137.     1          100.00             2                    0
  138.  
  139. Teller           :        1       2       3       4
  140. Transaction_Count:        0       1       1       0
  141.  
  142. CAPTION The 's' command starts a new customer task.
  143. TAG EX2=figure
  144.  
  145. ======================================================================
  146. END FIGURE
  147.  
  148.     Figure EX2 shows the start of the first customer task via the 's' commandfollowed immediately by a status report of the bank (requested via the 'b'command).  The task we just started has had enough time between the start of thetask and the request of the status display to perform 2 transactions, and we cansee that tellers 2 and 3 were selected to perform these transactions.
  149.  
  150. BEGIN FIGURE
  151. ======================================================================
  152.  
  153. Enter
  154.   b for bank status
  155.   c for continuous bank status
  156.   s for start next customer
  157.   x for exit: b
  158. **** BANK STATUS REPORT ****
  159. Customer      Balance  Transactions  Attempted_Overdraws
  160.     1           95.26             3                    0
  161.  
  162. Teller           :        1       2       3       4
  163. Transaction_Count:        0       2       1       0
  164.  
  165. CAPTION Another status display shows just a little activity by the customer.
  166. TAG EX3=figure
  167.  
  168. ======================================================================
  169. END FIGURE
  170.  
  171.     Figure EX3 shows another bank status command, which was issued as soon asthe display above completed.  During this brief amount of time, the customer hashad time to perform another transaction.
  172.  
  173. BEGIN FIGURE
  174. ======================================================================
  175.  
  176. Enter
  177.   b for bank status
  178.   c for continuous bank status
  179.   s for start next customer
  180.   x for exit: s
  181. Enter
  182.   b for bank status
  183.   c for continuous bank status
  184.   s for start next customer
  185.   x for exit: s
  186. Enter
  187.   b for bank status
  188.   c for continuous bank status
  189.   s for start next customer
  190.   x for exit: s
  191. Enter
  192.   b for bank status
  193.   c for continuous bank status
  194.   s for start next customer
  195.   x for exit: s
  196. Enter
  197.   b for bank status
  198.   c for continuous bank status
  199.   s for start next customer
  200.   x for exit: s
  201. Enter
  202.   b for bank status
  203.   c for continuous bank status
  204.   s for start next customer
  205.   x for exit: b
  206. **** BANK STATUS REPORT ****
  207. Customer      Balance  Transactions  Attempted_Overdraws
  208.     1          100.59             5                    0
  209.     2          123.13             4                    0
  210.     3          117.75             3                    0
  211.     4          114.97             3                    0
  212.     5          100.00             2                    0
  213.     6           38.86             4                    0
  214.  
  215. Teller           :        1       2       3       4
  216. Transaction_Count:        5       7       7       2
  217.  
  218. CAPTION Five more tasks are started up.
  219. TAG EX4=figure
  220.  
  221. ======================================================================
  222. END FIGURE
  223.  
  224.     Five more customer tasks are started up in Figure EX4 and another bankstatus display is requested.  The fact that we have many tasks running inparallel now should become evident as we see more and more activity going onbetween each of our status displays.
  225.  
  226. BEGIN FIGURE
  227. ======================================================================
  228.  
  229. Enter
  230.   b for bank status
  231.   c for continuous bank status
  232.   s for start next customer
  233.   x for exit: c
  234. **** BANK STATUS REPORT ****
  235. Customer      Balance  Transactions  Attempted_Overdraws
  236.     1          134.37             7                    0
  237.     2          131.77             5                    0
  238.     3          202.01             5                    0
  239.     4          129.76             4                    0
  240.     5          100.00             2                    0
  241.     6           57.83             5                    0
  242.  
  243. Teller           :        1       2       3       4
  244. Transaction_Count:        9       9       7       3
  245.  
  246.     ...
  247.  
  248. **** BANK STATUS REPORT ****
  249. Customer      Balance  Transactions  Attempted_Overdraws
  250.     1           73.44            29                    3
  251.     2          206.04            24                    0
  252.     3          165.82            27                    0
  253.     4           58.48            24                    3
  254.     5           71.03            22                    0
  255.     6           41.02            27                    2
  256.  
  257. Teller           :        1       2       3       4
  258. Transaction_Count:       60      36      38      19
  259.  
  260. CAPTION A continous bank status display shows a lot of activity over a shorttime.
  261. TAG EX5=figure
  262.  
  263. ======================================================================
  264. END FIGURE
  265.  
  266.     Figure EX5 shows a 'c' (continue bank status) command and its resultingdisplay.  To save space, I have shown only the first and the last of the elevenbank status report displays generated by this command.
  267.  
  268. BEGIN FIGURE
  269. ======================================================================
  270.  
  271. Enter
  272.   b for bank status
  273.   c for continuous bank status
  274.   s for start next customer
  275.   x for exit: s
  276. Enter
  277.   b for bank status
  278.   c for continuous bank status
  279.   s for start next customer
  280.   x for exit: s
  281. Enter
  282.   b for bank status
  283.   c for continuous bank status
  284.   s for start next customer
  285.   x for exit: b
  286. **** BANK STATUS REPORT ****
  287. Customer      Balance  Transactions  Attempted_Overdraws
  288.     1          108.96            31                    3
  289.     2          226.90            26                    0
  290.     3          106.79            30                    0
  291.     4           87.86            26                    3
  292.     5           57.83            24                    0
  293.     6           23.36            30                    3
  294.     7           50.89             3                    0
  295.     8          100.00             2                    0
  296.  
  297. Teller           :        1       2       3       4
  298. Transaction_Count:       68      42      42      21
  299.  
  300. CAPTION Two more customer tasks are started.
  301. TAG EX6=figure
  302.  
  303. ======================================================================
  304. END FIGURE
  305.  
  306.     In Figure EX6, two more customer tasks are started and the result is shown. There are now 8 customer tasks, 4 teller tasks, and the mainline procedure (asanother task) running in this Ada system.
  307.  
  308. BEGIN FIGURE
  309. ======================================================================
  310.  
  311. Enter
  312.   b for bank status
  313.   c for continuous bank status
  314.   s for start next customer
  315.   x for exit: b
  316. **** BANK STATUS REPORT ****
  317. Customer      Balance  Transactions  Attempted_Overdraws
  318.     1          127.88            34                    3
  319.     2          232.11            28                    0
  320.     3           56.17            33                    0
  321.     4          110.24            28                    3
  322.     5           81.60            28                    0
  323.     6            4.68            32                    3
  324.     7           31.60             6                    1
  325.     8           54.42             4                    0
  326.  
  327. Teller           :        1       2       3       4
  328. Transaction_Count:       79      48      44      22
  329. Enter
  330.   b for bank status
  331.   c for continuous bank status
  332.   s for start next customer
  333.   x for exit: b
  334. **** BANK STATUS REPORT ****
  335. Customer      Balance  Transactions  Attempted_Overdraws
  336.     1           63.89            37                    3
  337.     2          147.16            32                    0
  338.     3           48.62            38                    0
  339.     4          272.81            36                    3
  340.     5           62.22            34                    0
  341.     6           74.99            36                    4
  342.     7           59.31            10                    1
  343.     8           72.42             9                    0
  344.  
  345. Teller           :        1       2       3       4
  346. Transaction_Count:       94      59      56      24
  347.  
  348. CAPTION Two more status reports are shown, each after some delay.
  349. TAG EX7=figure
  350.  
  351. ======================================================================
  352. END FIGURE
  353.  
  354.     Figure EX7 shows two more bank status displays.  The user of the programhas delayed for some time before issuing these commands so that a significantlevel of activity can be shown.
  355.     More activity could be displayed, and more customer tasks could be started,but the displays shown so far should be enough to give the reader a good feelfor the program's operation.  When the user is finished, the 'x' (for exit)command will terminate all tasks and return the user to the operating system.
  356.     The displays presented so far were generated by the BD3 mainline procedurerunning on a UNIX system, having been compiled by the Verdix Ada compiler. BD3.EXE provided in the distribution was compiled under the IntegrAdaenvironment.  The reader will see similar displays when BD3.EXE is executed withthe exception that no other tasks will run when the prompt is displayed.  Inorder for multitasking to be observed when running BD3.EXE, the user must issuethe 'c' (for continuous bank status) command.  Once the delay statement in themainline is encountered, the Ada runtime system is allowed to come into play andother tasks are then permitted to run.
  357. RESERVE 4
  358. SECTION Discussion of the Design
  359.  
  360.     BD3 is an object-oriented design.  The objects contained in this Ada systemare a console device (defined by the Ada package CONSOLE) which supports inputand output from and to the user's console terminal, a random number generator(defined by the Ada package RANDOM) which generates random numbers of type FLOATin the range from 0.0 to 1.0, the bank (defined by the Ada package BANK) whichcontains the four TELLER(n) objects (defined by the Ada task type TELLER_PERSON)and the basic type definitions (such as TRANSACTION, CUSTOMER_ID, and DOLLAR)which are used to communicate with a TELLER(n) object, and the base of customers(defined by the Ada package CUSTOMER_WORLD) which interacts with the tellers inthe bank.
  361.     The specification to the package CONSOLE is shown in Figures CONSOLE1,CONSOLE2, and CONSOLE3.  Each line is numbered for the convenience of the reader(of course, these numbers do not appear in the files which are compiled by anAda compiler).
  362.  
  363. BEGIN FIGURE
  364. ======================================================================
  365.  
  366.   1 package CONSOLE is
  367.   2 --------------------------------------------------------------------------
  368.   3 --| BEGIN PROLOGUE
  369.   4 --| DESCRIPTION            : CONSOLE is a package which implements an
  370.   5 --|                        : abstract state machine, a console terminal,
  371.   6 --|                        : that maps to the user's terminal.  CONSOLE
  372.   7 --|                        : provides routines to output characters,
  373.   8 --|                        : strings, integers, and floats (real numbers)
  374.   9 --|                        : to the user's terminal.  CONSOLE provides
  375.  10 --|                        : a routine to input a string of text from the
  376.  11 --|                        : user's terminal.  Finally, CONSOLE provides
  377.  12 --|                        : a function which can trim leading spaces
  378.  13 --|                        : from a string of text (useful in outputting
  379.  14 --|                        : text which was input by the read routine).
  380.  15 --|                        :
  381.  16 --| REQUIREMENTS SUPPORTED : A simple I/O package for Ada programs
  382.  17 --|                        :
  383.  18 --| LIMITATIONS            : Text input by CONSOLE.READ can be no more
  384.  19 --|                        : than 80 characters long.  Only objects of
  385.  20 --|                        : type CHARACTER, STRING, INTEGER, and FLOAT
  386.  21 --|                        : can be output.
  387.  22 --|                        :
  388.  23 --| AUTHOR(S)              : Richard Conn
  389.  24 --| CHANGE LOG             : 08/30/88  RLC  Initial Design and Code
  390.  25 --|                        :
  391.  26 --| REMARKS                : None
  392.  27 --|                        :
  393.  28 --| PORTABILITY ISSUES     : Uses TEXT_IO, so is very portable; no known
  394.  29 --|                        : portability problems.
  395.  30 --| END PROLOGUE
  396.  31 --------------------------------------------------------------------------
  397.  32  
  398.  
  399. CAPTION Specification of Package CONSOLE, Part 1 of 4
  400. TAG CONSOLE1=figure
  401.  
  402. ======================================================================
  403. END FIGURE
  404.  
  405.     In Figure CONSOLE1, the prologue in the specification of the packageCONSOLE is given.  Line 1 is the only piece of processed code; the rest of thelines are Ada comments which describe the package and give other informationabout it.  I developed this package some time ago for use in my Ada classes,feeling that the package TEXT_IO (which is supplied with all Ada compilers) istoo complex for most beginners to work with.  Package CONSOLE starts tointroduce an object-oriented design to Ada code design and is simple to use.
  406.  
  407. BEGIN FIGURE
  408. ======================================================================
  409.  
  410.  33     -- Special items to print
  411.  34     type    SPECIAL_ITEM is (NEW_LINE, TAB, BACKSPACE);
  412.  35  
  413.  36     -- Type of string used by READ procedure
  414.  37     subtype OUTSTRING    is STRING(1 .. 80);
  415.  38 
  416.  39     procedure WRITE(ITEM : in STRING);
  417.  40     procedure WRITE(ITEM : in CHARACTER);
  418.  41     -- Print strings and characters
  419.  42     -- Examples:
  420.  43     --  Ada procedure call                      Prints (without quotes)
  421.  44     --  ============================            =======================
  422.  45     --  CONSOLE.WRITE ("This is a test");       "This is a test"
  423.  46     --  CONSOLE.WRITE ('?');                    "?"
  424.  47  
  425.  48     procedure WRITE(ITEM : in SPECIAL_ITEM);
  426.  49     -- Print special items
  427.  50     -- Example:
  428.  51     --  Ada procedure call                      Prints (without quotes)
  429.  52     --  ============================            =======================
  430.  53     --  CONSOLE.WRITE (CONSOLE.NEW_LINE);       <advances to next line>
  431.  54  
  432.  55 -- 
  433.  56 -- Package CONSOLE
  434.  57 
  435.  58     procedure WRITE(ITEM : in INTEGER; WIDTH : in NATURAL := 0);
  436.  59     -- Print integers
  437.  60     -- Examples:
  438.  61     --  Ada procedure call                      Prints (without quotes)
  439.  62     --  ============================            =======================
  440.  63     --  CONSOLE.WRITE (25);                     "25"
  441.  64     --  CONSOLE.WRITE (-3);                     "-3"
  442.  65     --  CONSOLE.WRITE (25, 5);                  "   25"
  443.  66  
  444.  
  445. CAPTION Specification of Package CONSOLE, Part 2 of 4
  446. TAG CONSOLE2=figure
  447.  
  448. ======================================================================
  449. END FIGURE
  450.  
  451.     Figure CONSOLE2 contains more of the specification of package CONSOLE,showing the two type definitions used by procedures within the package and fourprocedure specifications, all for procedures named WRITE (see lines 39, 40, 48,and 58).  These procedures, all similarly named, differ in the types ofarguments they accept, and Ada determines which procedure is reference by thetypes of the arguments used when the call to the procedure is made.  The commentblocks in lines 41-46, 49-53, and 59-65 contain examples of how these procedureswould be called from outside of package CONSOLE.
  452.     Note that the WRITE procedure on line 58 accepts two parameters, an INTEGERand a NATURAL number with a default value.  The default value of 0 for the WIDTHparameter tells the procedure to use as many spaces as necessary to output thenumber.  As indicated by lines 63-65, the WIDTH parameter does not have to bereferenced when the procedure is called.
  453.  
  454. BEGIN FIGURE
  455. ======================================================================
  456.  
  457.  67     procedure WRITE(ITEM           : in FLOAT;
  458.  68                     BEFORE_DECIMAL : in NATURAL := 5;
  459.  69                     AFTER_DECIMAL  : in NATURAL := 5);
  460.  70     procedure WRITE_SCIENTIFIC(ITEM          : in FLOAT;
  461.  71                                AFTER_DECIMAL : in NATURAL := 8);
  462.  72     -- Print floats
  463.  73     -- Examples:
  464.  74     --  Ada procedure call                      Prints (without quotes)
  465.  75     --  ============================            =======================
  466.  76     --  CONSOLE.WRITE (25.21);                  "   25.21000"
  467.  77     --  CONSOLE.WRITE (-36.2);                  "  -36.20000"
  468.  78     --  CONSOLE.WRITE (-36.2, 1, 1);            "-36.2"
  469.  79     --  CONSOLE.WRITE (25.21, 3);               " 25.21000"
  470.  80     --  CONSOLE.WRITE (25.21, 3, 2);            " 25.21"
  471.  81     --  CONSOLE.WRITE_SCIENTIFIC (23.0);        " 2.30000000e+01"
  472.  82     --  CONSOLE.WRITE_SCIENTIFIC (5.7, 2);      " 5.70E+00"
  473.  83     --  CONSOLE.WRITE_SCIENTIFIC (-4.5e-24, 4); "-4.5000E-24"
  474.  84  
  475.  
  476. CAPTION Specification of Package CONSOLE, Part 3 of 4
  477. TAG CONSOLE3=figure
  478.  
  479. ======================================================================
  480. END FIGURE
  481.  
  482.     Figure CONSOLE3 contains the next part of the specification for packageCONSOLE.  Lines 67-69 define the specification for a WRITE procedure which workson objects of type FLOAT (floating point) to output them in the form "nn.nn"while lines 70-71 define the specification for a WRITE_SCIENTIFIC procedurewhich also works on objects of type FLOAT but outputs them in scientificnotation.
  483.  
  484. BEGIN FIGURE
  485. ======================================================================
  486.  
  487.  85     procedure READ(ITEM : out OUTSTRING);
  488.  86     -- Read strings
  489.  87     -- Example (note: <CR> refers to the RETURN key):
  490.  88     --  MY_STRING : CONSOLE.OUTSTRING; -- 80-char string
  491.  89     --  Ada procedure call              User Types      In MY_STRING
  492.  90     --  ====================            ==========      ============
  493.  91     --  CONSOLE.READ (MY_STRING);       "Hi<CR>"        "Hi"<and 78 spaces>
  494.  92  
  495.  93     function TRIM(ITEM : in STRING) return STRING;
  496.  94     -- Generate a string which has no trailing spaces
  497.  95     -- Example of use:
  498.  96     --  MY_STRING : CONSOLE.OUTSTRING;
  499.  97     --  CONSOLE.READ(MY_STRING);
  500.  98     --  CONSOLE.WRITE("Hello, ");
  501.  99     --  CONSOLE.WRITE(CONSOLE.TRIM(MY_STRING));
  502. 100     --  CONSOLE.WRITE(", how are you?");
  503. 101     -- If the CONSOLE.READ statement returns "Joe" followed by 77 spaces,
  504. 102     -- the output will look like "Hello, Joe, how are you?"
  505. 103  
  506. 104 end CONSOLE;
  507.  
  508. CAPTION Specification of Package CONSOLE, Part 4 of 4
  509. TAG CONSOLE4=figure
  510.  
  511. ======================================================================
  512. END FIGURE
  513.  
  514.     Figure CONSOLE4 shows the last part of package CONSOLE.  Procedure READ online 85 always returns an object of type OUTSTRING, which is 80 characters long. When this procedure is called, the next line typed by the user is returned.  Ifit is less than 80 characters long, trailing spaces are appended to it.  If itis more than 80 characters long, the first 80 characters are returned and thenext call to READ returns the next 80.  For the convenience of the user, thefunction TRIM (line 93) is provided to convert a string of any length to one ofanother length that contains all characters of the input string except thetrailing spaces.
  515.  
  516. BEGIN FIGURE
  517. ======================================================================
  518.  
  519.   MY_NUMBER : FLOAT; -- variable definition
  520.   ...
  521.   MY_NUMBER := RANDOM.NUMBER; -- compute the next random number
  522.  
  523. CAPTION Calling the Random Number Generator
  524. TAG RANDOMS=figure
  525. ======================================================================
  526.  
  527. END FIGURE
  528.  
  529.     The random number generator object used by the Bank Demo Ada system isdefined by the Ada package named RANDOM.  Figure RANDOM contains thespecification of this package.  RANDOM contains only one function, named NUMBER,which requires no input parameters and returns the next random number as anobject of type FLOAT.  A code segment like the one in Figure RANDOMS is used tocall the NUMBER function in package RANDOM:
  530.  
  531. BEGIN FIGURE
  532. ======================================================================
  533.  
  534.   1 package RANDOM is
  535.   2 --------------------------------------------------------------------------
  536.   3 --| BEGIN PROLOGUE
  537.   4 --| DESCRIPTION            : Package RANDOM contains the function NUMBER
  538.   5 --|                        : which returns a pseudo-random number
  539.   6 --|                        : of type FLOAT in the range 0.0 .. 1.0.
  540.   7 --|                        : 
  541.   8 --| REQUIREMENTS SUPPORTED : Random Number Generator
  542.   9 --|                        : 
  543.  10 --| LIMITATIONS            : None
  544.  11 --|                        : 
  545.  12 --| AUTHOR(S)              : Richard Conn (RLC) from Bill Whitaker's work
  546.  13 --| CHANGE LOG             : 09/30/88  RLC  Design, code, test from
  547.  14 --|                        :                 Bill Whitaker's original work
  548.  15 --|                        : 10/11/88  RLC  Modified based on ideas from
  549.  16 --|                        :                 Ron Bell and his RAN2 Package
  550.  17 --|                        : 
  551.  18 --| REMARKS                : None
  552.  19 --|                        : 
  553.  20 --| PORTABILITY ISSUES     : Uses 16-bit integers, so should be quite
  554.  21 --|                        : portable
  555.  22 --| END PROLOGUE
  556.  23 --------------------------------------------------------------------------
  557.  24 
  558.  25     function NUMBER return FLOAT;
  559.  26     -- Return a floating point pseudo-random number
  560.  27 
  561.  28 end RANDOM;
  562.  
  563. CAPTION Specification of Package RANDOM
  564. TAG RANDOM=figure
  565.  
  566. ======================================================================
  567. END FIGURE
  568.  
  569.     If you look at the body of package RANDOM, which is included in the sourcecode distributed with Bank Demo, you will find that RANDOM contains a hiddenprocedure called SEED that sets the first random number in the sequence based onthe time-of-day clock value returned by the Ada-standard package calledCALENDAR.  Procedure SEED, however, is not needed by the outside world, sopackage RANDOM does not make it available.
  570.  
  571. BEGIN FIGURE
  572. ======================================================================
  573.  
  574.   1 package BANK is
  575.   2 --------------------------------------------------------------------------
  576.   3 --| BEGIN PROLOGUE
  577.   4 --| DESCRIPTION            : BANK defines a bank, the TELLER objects
  578.   5 --|                        : within it, and the procedure PRINT_REPORT
  579.   6 --|                        : (which reports on the status of the BANK).
  580.   7 --|                        :
  581.   8 --|                        : BANK is an abstract state machine, defining
  582.   9 --|                        : a BANK object which contains TELLER objects.
  583.  10 --|                        :
  584.  11 --| REQUIREMENTS SUPPORTED : Bank Demonstration Program to show
  585.  12 --|                        : object-oriented design and tasking
  586.  13 --|                        : with Ada
  587.  14 --|                        :
  588.  15 --| LIMITATIONS            : None
  589.  16 --| AUTHOR(S)              : Richard Conn (RLC)
  590.  17 --| CHANGE LOG             : 1/16/89  RLC  Initial Design and Code
  591.  18 --| CHANGE LOG             : 2/25/89  RLC  Final Review Prior to Release
  592.  19 --| REMARKS                : None
  593.  20 --| PORTABILITY ISSUES     : None
  594.  21 --| END PROLOGUE
  595.  22 --------------------------------------------------------------------------
  596.  23  
  597.  
  598. CAPTION Specification of Package BANK, Part 1 of 4
  599. TAG BANK1=figure
  600.  
  601. ======================================================================
  602. END FIGURE
  603.  
  604.     Figure BANK1 contains the prologue of the specification of package BANK,which defines the bank object.  Note that the tellers are implemented as tasks(I refer to the tellers in a general sense as TELLER(n), where n is 1 to 4 toindicate which teller).
  605.  
  606. BEGIN FIGURE
  607. ======================================================================
  608.  
  609.  24     -- TRANSACTION requested of a TELLER
  610.  25     type TRANSACTION  is (ADD_NEW_CUSTOMER,
  611.  26                           GET_BALANCE,
  612.  27                           MAKE_DEPOSIT,
  613.  28                           MAKE_WITHDRAWAL);
  614.  29 
  615.  30     -- Unit of currency
  616.  31     type DOLLAR       is new FLOAT;
  617.  32  
  618.  33     -- Identification of a CUSTOMER
  619.  34     type CUSTOMER_ID  is new NATURAL range 1 .. NATURAL'LAST;
  620.  35  
  621.  
  622. CAPTION Specification of Package BANK, Part 2 of 4
  623. TAG BANK2=figure
  624.  
  625. ======================================================================
  626. END FIGURE
  627.  
  628.     Figure BANK2 shows more of the specification of package BANK.  The typedefinitions are presented first.  Lines 25-28 define type TRANSACTION, which arethe transactions which may be requested of a TELLER(n) object.  The transactionswhich may be performed by a teller are ADD_NEW_CUSTOMER, GET_BALANCE,MAKE_DEPOSIT, and MAKE_WITHDRAWAL.  Line 31 defines type DOLLAR, which is theunit of currency for the bank.  DOLLAR is implemented as a floating pointnumber, derived from type FLOAT, but DOLLAR objects are unique from FLOATobjects by this type definition (DOLLAR is a derived type in Ada) and we cannotinterchange the use of a DOLLAR object and a FLOAT object.  Line 34 defines typeCUSTOMER_ID, which is the ID number associated with each customer.  When atransaction is made with a teller, the customer presents his ID number toidentify himself along with his transaction request.  Like DOLLAR, CUSTOMER_IDis a derived type, but CUSTOMER_ID is derived from type NATURAL and can take onvalues from 1 to NATURAL'LAST (the largest value allowed for objects of typeNATURAL).
  629.  
  630. BEGIN FIGURE
  631. ======================================================================
  632.  
  633.  36     -- Index of and Number of TELLER objects within the bank
  634.  37     type TELLER_INDEX is new NATURAL range 1 .. 4;
  635.  38 
  636.  39     -- A TELLER_PERSON is an object to which a CUSTOMER may make a REQUEST
  637.  40     -- The BANK tells the TELLER_PERSON to START_WORK, giving the
  638.  41     -- TELLER_PERSON its TELLER_NUMBER
  639.  42     task type TELLER_PERSON is
  640.  43         entry REQUEST(ID     : in out CUSTOMER_ID;
  641.  44                       KIND   : in TRANSACTION;
  642.  45                       AMOUNT : in out DOLLAR);
  643.  46     end TELLER_PERSON;
  644.  47     -- These are the TELLER objects available at the bank
  645.  48     TELLER : array(TELLER_INDEX) of TELLER_PERSON;
  646.  49  
  647.  
  648. CAPTION Specification of Package BANK, Part 3 of 4
  649. TAG BANK3=figure
  650.  
  651. ======================================================================
  652. END FIGURE
  653.  
  654.     In Figure BANK3, line 37 defines type TELLER_INDEX, which is used toidentify the desired teller.  Type TELLER_INDEX is derived from type NATURAL andrestricted in range from 1 to 4.  If the user wishes to modify this program andadd more tellers to the model, he need only change the 4 on line 37 to thedesired number.  The parts of the code which need to know how many tellers areavailable at the bank reference TELLER_INDEX'LAST, which is the largest valuethat objects of type TELLER_INDEX can take on.
  655.     Task type TELLER_PERSON on lines 42-46 defines the interface to all tasksof type TELLER_PERSON.  A TELLER_PERSON task contains one entry point, calledREQUEST, which can be called by tasks external to package BANK.  Threeparameters must be specified when the REQUEST entry point is called: the ID ofthe customer (type CUSTOMER_ID), the KIND of transaction (type TRANSACTION), andthe AMOUNT associated with the transaction (type DOLLAR).
  656.     The task body of TELLER_PERSON is not contained in the specification ofpackage BANK, but it is hidden in the body of package BANK.  You can see thiscode by looking at the source code provided in the distribution.  In summary,tasks of type TELLER_PERSON are quite simple in their operation.  When a task oftype TELLER_PERSON starts running, it calls the internal task TELLER_ASSIGNER(which is hidden inside of package BANK) in order to get its ID number.  As eachcustomer has an ID, so each teller also has an ID.  The teller's ID is used tokeep a running record of the number of transactions each teller makes.  After itgets it's ID number, a task of type TELLER_PERSON enters an infinite loop inwhich it waits for an external task to call its REQUEST entry point, processesthe request from this external task based on the KIND of transaction requested,and then resumes the loop.  It is left as an exercise to the reader to read thecode and figure out exactly what happens for each KIND of transaction.
  657.     Line 48 defines the array, named TELLER, of tellers in the bank.  Eachelement of this array is of type TELLER_PERSON, making each element anindependent task as opposed to what the reader may normally think of as an array(which is an array of values stored in memory).  The array TELLER is indexed byTELLER_INDEX, taking on index values from 1 to 4.  Hence, four tasks are createdby the array declaration on line 48: TELLER(1), TELLER(2), TELLER(3), andTELLER(4).  Each task starts running during initialization of the Ada system before the first line of code of the mainline procedure is executed.
  658.  
  659. BEGIN FIGURE
  660. ======================================================================
  661.  
  662.  50     -- PRINT_REPORT gives the transaction log of all the bank
  663.  51     -- customers
  664.  52     procedure PRINT_REPORT;
  665.  53  
  666.  54     -- STOP_WORK terminates all TELLER tasks
  667.  55     procedure STOP_WORK;
  668.  56 end BANK;
  669.  
  670. CAPTION Specification of Package BANK, Part 4 of 4
  671. TAG BANK4=figure
  672.  
  673. ======================================================================
  674. END FIGURE
  675.  
  676.     Package BANK also exports two procedures, as shown in Figure BANK4:PRINT_REPORT and STOP_WORK.  PRINT_REPORT (whose specification is on line 52)prints a report of the status of the bank.  This report indicates the number oftransactions requested by each customer, the balance of each customer's account,and the number of transactions processed by each teller.  These items ofinformation are updated constantly by the TELLER(n) tasks by means of a database kept internal to package BANK.  The outside world does not need to knowthat this data base exists or what form it exists in; the outside world onlyneeds to know that it can print this report based on information in the database.  PRINT_REPORT is called by the BD3 mainline procedure whenever the user atthe console asks for it or during the running of the continuous status displaycommand issued by the user.  STOP_WORK (whose specification is on line 55)terminates all of the TELLER(n) tasks.  STOP_WORK is called by the BD3 mainlineprocedure when the user asks for the program to shut down (the exit command). All tasks of the Ada system must be terminated before the mainline procedure,which is also a task, may terminate.  STOP_WORK represents a function that wouldbe performed by a bank when it closes and it is reasonable to provide it as partof the specification of package BANK as opposed to relying upon the mainlineprocedure to abort each of the TELLER(n) tasks on a task-by-task basis.
  677.  
  678. BEGIN FIGURE
  679. ======================================================================
  680.  
  681. 282 package CUSTOMER_WORLD is
  682. 283 --------------------------------------------------------------------------
  683. 284 --| BEGIN PROLOGUE
  684. 285 --| DESCRIPTION            : CUSTOMER_WORLD is an abstract state machine
  685. 286 --|                        : which defines the collection of all CUSTOMERs
  686. 287 --|                        : of the BANK.  It allows the mainline procedure
  687. 288 --|                        : to ADD a new CUSTOMER and TERMINATE_ALL
  688. 289 --|                        : current CUSTOMERs.  The CUSTOMER itself acts
  689. 290 --|                        : as an independent task and requires no
  690. 291 --|                        : direct interaction with the mainline once
  691. 292 --|                        : it starts.
  692. 293 --|                        :
  693. 294 --| REQUIREMENTS SUPPORTED : Bank Demonstration Program to show
  694. 295 --|                        : object-oriented design and tasking
  695. 296 --|                        : with Ada
  696. 297 --|                        :
  697. 298 --| LIMITATIONS            : None
  698. 299 --| AUTHOR(S)              : Richard Conn (RLC)
  699. 300 --| CHANGE LOG             : 1/16/89  RLC  Initial Design and Code
  700. 301 --| CHANGE LOG             : 2/25/89  RLC  Final Review Prior to Release
  701. 302 --| REMARKS                : None
  702. 303 --| PORTABILITY ISSUES     : None
  703. 304 --| END PROLOGUE
  704. 305 --------------------------------------------------------------------------
  705. 306  
  706. 307     -- Add another CUSTOMER task to the system
  707. 308     procedure ADD;
  708. 309  
  709. 310     -- Terminate all CUSTOMERs in the system
  710. 311     procedure TERMINATE_ALL;
  711. 312  
  712. 313 end CUSTOMER_WORLD;
  713.  
  714. CAPTION Specification of Package CUSTOMER_WORLD
  715. TAG CUSTOMER=figure
  716.  
  717. ======================================================================
  718. END FIGURE
  719.  
  720.     Figure CUSTOMER contains the specification of the package CUSTOMER_WORLD. This package contains, hidden within it, all of the customer tasks which arerunning in the system.  Internally, package CUSTOMER_WORLD keeps track of thesetasks by creating a linked list which points to each of them.  The procedure ADD(whose specification is on line 308) causes the package CUSTOMER_WORLD to spawnanother customer task, updating the linked list when it does so.  When eachcustomer task is spawned, it begins running immediately, performing thefunctions of an independent customer.  There are no entry points to a customer;the outside world does not request access to a customer, and the customer taskseach act independently of each other, requesting access to the TELLER(n) tasksin the bank to perform transactions as they need them.  Each time the userissues the "start new customer" command from the console, procedureCUSTOMER_WORLD.ADD is called to perform this function, and a new customer taskstarts running.
  721.     As mentioned in the discussion of package BANK, all children tasks have tobe aborted before the mainline procedure can terminate, and packageCUSTOMER_WORLD exports the procedure TERMINATE_ALL to do this.  TERMINATE_ALLmoves through the linked list of tasks, aborting each one as it is encountered. The BD3 mainline procedure calls CUSTOMER_WORLD.TERMINATE_ALL just like it callsBANK.STOP_WORK when the exit command is processed.
  722.     The task specification and body of a customer (the task type is namedCUSTOMER) is hidden in the body of package CUSTOMER_WORLD.  Each customer taskperforms the following sequence of operations:
  723.  
  724.   1. The customer selects a TELLER(n) task at random using the random numbergenerator (function NUMBER in package RANDOM).  The customer issues anADD_NEW_CUSTOMER transaction through the REQUEST entry point of the selectedTELLER(n) task.  The selected TELLER(n) task returns the customer task's IDnumber, which the customer task stores for use on all future transactions withall TELLER(n) tasks.
  725.   2. The customer, using his ID number, then selects a TELLER(n) task at randomand issues a MAKE_DEPOSIT transaction through that teller's REQUEST entry point. The customer deposits $100.00 (see lines 325 and 379-380 in the full listing ofBD3 distributed with this document).
  726.   3. The customer now enters an infinite loop, which he stays in for the rest ofhis life (until he is aborted by the user at the console issuing the exitcommand).  This loop (see lines 389-399 of the source listing) consists of thefollowing steps:
  727.   3.1.  Delay a random amount of time from 0 to 5 seconds.
  728.   3.2.  Generate a random amount of money from -$50.00 to $50.00.  If the amountis negative, select the current transaction to be MAKE_WITHDRAWAL and make theamount positive.  If the amount is positive, select the current transaction tobe MAKE_DEPOSIT.
  729.   3.3.  Select a TELLER(n) task at random, call that teller's REQUEST entrypoint, passing to it the his ID number, the desired transaction, and the amount. The TELLER(n) task will then process the transaction, allowing a withdrawal onlyif the customer has sufficient funds to cover the request.
  730.  
  731.     Figures BD1 to BD4 contain the source code of the mainline procedure forthis Ada system, procedure BD3 (short for Bank Demo 3).  This procedure is wherethe execution of the mainline begins.  All tasks which were declared staticallyas objects, such as the TELLER(n) tasks of package BANK, will have already beeninitialized and begun execution before the first line of this procedure isexecuted.  The customer tasks will be dynamically created during the executionof this procedure, where a new customer task will be created each time the userissues the "start next customer" command from the console.
  732.  
  733. BEGIN FIGURE
  734. ======================================================================
  735.  
  736. 442 with BANK;
  737. 443 with CUSTOMER_WORLD;
  738. 444 with CONSOLE;
  739. 445 procedure BD3 is                           -- BANK_DEMO_3
  740. 446 ------------------------------------------------------------------------
  741. 447 --| BEGIN PROLOGUE
  742. 448 --| DESCRIPTION            : Procedure BD3 (BANK DEMO 3) is a mainline
  743. 449 --|                        : which demonstrates the operation of the
  744. 450 --|                        : BANK.  Upon invocation, the console becomes
  745. 451 --|                        : a command processor for the bank manager.
  746. 452 --|                        : The bank manager can obtain the status of
  747. 453 --|                        : the bank (balances, number of transactions,
  748. 454 --|                        : and attempted overdraws of all customers
  749. 455 --|                        : and number of transactions processed by all
  750. 456 --|                        : tellers) in a single or continuous (group
  751. 457 --|                        : of 11 over about one minute) display.
  752. 458 --|                        : The user at the console can also cause new
  753. 459 --|                        : Customer tasks to be created and shut down
  754. 460 --|                        : the system.
  755. 461 --|                        :
  756. 462 --| REQUIREMENTS SUPPORTED : Bank Demonstration Program to show
  757. 463 --|                        : object-oriented design and tasking
  758. 464 --|                        : with Ada
  759. 465 --|                        :
  760. 466 --| LIMITATIONS            : None
  761. 467 --| AUTHOR(S)              : Richard Conn (RLC)
  762. 468 --| CHANGE LOG             : 1/16/89  RLC  Initial Design and Code
  763. 469 --| CHANGE LOG             : 2/25/89  RLC  Final Review Prior to Release
  764. 470 --| REMARKS                : None
  765. 471 --| PORTABILITY ISSUES     : Uses CONSOLE (TEXT_IO), so is very portable;
  766. 472 --|                        : no known portability problems.
  767. 473 --| END PROLOGUE
  768. 474 ------------------------------------------------------------------------
  769. 475  
  770.  
  771. CAPTION Procedure BD3, Part 1 of 4
  772. TAG BD1=figure
  773.  
  774. ======================================================================
  775. END FIGURE
  776.  
  777.     In Figure BD1, lines 442-444 establish the context in which the procedurefunctions.  In particular, the procedure BD3 needs to know about package BANK,package CUSTOMER_WORLD, and package CONSOLE.  These Ada packages must bepreviously compiled into the current program unit library before procedure BD3can be compiled.  The Ada compiler will check all interfaces (such as procedureand object names, parameters passed to the procedures, and the types of allobjects referenced) between these packages and procedure BD3 each time BD3references one of these packages.
  778.     Line 445 introduces the procedure (is the beginning of procedure BD3'sbody), and the rest of the lines in Figure BD1 are comments in the prologue.
  779.  
  780. BEGIN FIGURE
  781. ======================================================================
  782.  
  783. 476     -- Line input from the console
  784. 477     INPUT                               : CONSOLE.OUTSTRING;
  785. 478  
  786. 479     -- Number of continuous status reports displayed by the 'c'
  787. 480     -- command before control is returned to the console
  788. 481     NUMBER_OF_CONTINUOUS_STATUS_REPORTS : constant := 10;
  789. 482  
  790.  
  791. CAPTION Procedure BD3, Part 2 of 4
  792. TAG BD2=figure
  793.  
  794. ======================================================================
  795. END FIGURE
  796.  
  797.     Figure BD2 shows a local variable and a local constant in procedure BD3. The variable INPUT is used to receive the line entered by the user when he isprompted for input.  The READ procedure in package CONSOLE is used to input thisline.  The definition of the constant number of continuous status reports isself-explanatory.
  798.  
  799. BEGIN FIGURE
  800. ======================================================================
  801.  
  802. 483 -- 
  803. 484 -- procedure BD3
  804. 485  
  805. 486 begin                                      -- mainline
  806. 487  
  807. 488     -- This is the beginning of the main loop.  In this loop,
  808. 489     -- a list of commands is printed on the console, the user
  809. 490     -- at the console (as CUSTOMER 0) enters one of these commands
  810. 491     -- followed by striking the RETURN key, and the command is
  811. 492     -- processed.
  812. 493     loop
  813. 494  
  814. 495         -- Command listing and prompt
  815. 496     CONSOLE.WRITE("Enter");
  816. 497     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  817. 498     CONSOLE.WRITE("  b for bank status");
  818. 499     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  819. 500     CONSOLE.WRITE("  c for continuous bank status");
  820. 501     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  821. 502     CONSOLE.WRITE("  s for start next customer");
  822. 503     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  823. 504     CONSOLE.WRITE("  x for exit: ");
  824. 505     CONSOLE.READ(INPUT);
  825. 506  
  826.  
  827. CAPTION Procedure BD3, Part 3 of 4
  828. TAG BD3=figure
  829.  
  830. ======================================================================
  831. END FIGURE
  832.  
  833.     Figure BD3 shows the beginning of the executable code in procedure BD3. This code starts after the "begin" statement on line 486, and the entirety ofthis code is contained within the loop which starts on line 493.
  834.     The first thing done in this loop is the presentation of the prompt to theuser (lines 496-504) and the input of the command from the user (line 505).
  835.  
  836. BEGIN FIGURE
  837. ======================================================================
  838.  
  839. 507         -- Interpretation and execution of input command
  840. 508     case INPUT(1) is
  841. 509         when 'b' | 'c' =>              -- Short or continuous bank status
  842. 510                                        -- report
  843. 511         BANK.PRINT_REPORT;
  844. 512         if INPUT(1) = 'c' then
  845. 513             for I in 1 .. NUMBER_OF_CONTINUOUS_STATUS_REPORTS loop
  846. 514             delay 5.0;
  847. 515             BANK.PRINT_REPORT;
  848. 516                     CONSOLE.WRITE(CONSOLE.NEW_LINE);
  849. 517             end loop;
  850. 518         end if;
  851. 519  
  852. 520         when 's' =>                    -- Start up a new CUSTOMER
  853. 521         CUSTOMER_WORLD.ADD;
  854. 522  
  855. 523         when 'x' =>                      -- Exit program
  856. 524         CUSTOMER_WORLD.TERMINATE_ALL;    -- Kill CUSTOMER tasks
  857. 525         BANK.STOP_WORK;                  -- Kill TELLER tasks
  858. 526         exit;                            -- Exit loop
  859. 527  
  860. 528         when ' ' =>                    -- Non-error on a null input line
  861. 529         null;
  862. 530  
  863. 531         when others =>                 -- Other commands are invalid
  864. 532         CONSOLE.WRITE("Invalid Command: ");
  865. 533         CONSOLE.WRITE(CONSOLE.TRIM(INPUT));
  866. 534         CONSOLE.WRITE(CONSOLE.NEW_LINE);
  867. 535  
  868. 536     end case;
  869. 537  
  870. 538     end loop;
  871. 539  
  872. 540 end BD3;
  873.  
  874. CAPTION Procedure BD3, Part 4 of 4
  875. TAG BD4=figure
  876.  
  877. ======================================================================
  878. END FIGURE
  879.  
  880.     Figure BD4 shows the rest of procedure BD3.  The first letter of thecommand just input (line 505 of Figure BD3) is the target for the caseexpression in line 508.  If this letter is a lower-case 'b' (bank status) or 'c'(continuous bank status), the code in lines 509-519 will be executed.  If thisletter is a lower-case 's' (start next customer), the code in lines 520-522 willbe executed.  If this letter is a lower-case 'x' (exit), the code in lines523-527 will be executed.  If this letter is a space, nothing happens (lines528-530).  If this letter is anything else, an error message is printed (lines531-535).  The loop itself ends on lines 538, and the procedure BD3 ends on line540.
  881. RESERVE 4
  882. SECTION File Distribution
  883.  
  884.     Figure FILELIST shows the files provided in the distribution of Bank Demo3.  This document is in one-column format in the file PRINT.ME.
  885.  
  886. BEGIN FIGURE
  887. ======================================================================
  888.  
  889.      READ.ME          An introduction to the distribution
  890.      PRINT.ME         This document, ready to be printed as ASCII
  891.                         text on 8 1/2" x 11" paper in elite type
  892.      BD3.SPR          Source (in Borland's SPRINT) to the file PRINT.ME
  893.  
  894.      CONSOLE.ADA      Compilable Ada source code to package CONSOLE
  895.      RANDOM.ADA       Compilable Ada source code to package RANDOM
  896.      BD3.ADA          Compilable Ada source code to packages BANK
  897.                         and CUSTOMER_WORLD and procedure BD3
  898.                       Note: the compilation order is CONSOLE.ADA,
  899.                         RANDOM.ADA, and BD3.ADA
  900.  
  901.      CONSOLE.LST      Listing of CONSOLE.ADA with each line numbered
  902.      RANDOM.LST       Listing of RANDOM.ADA with each line numbered
  903.      BD3.LST          Listing of BD3.ADA with each line numbered
  904.  
  905.      BD3.EXE          Executable binary of the BD3 system (run by
  906.                         giving the command "BD3" at the MSDOS prompt)
  907.  
  908. CAPTION Files Distributed as Part of Bank Demo 3
  909. TAG FILELIST=figure
  910.  
  911. ======================================================================
  912. END FIGURE
  913.  
  914.