      PROGRAM INVEN
**MAIN******************************************************************
* This program accepts an order from the terminal, searches an inven-  *
* tory file to see if the item ordered is in stock, updates the file,  *
* and displays an out-of-stock message and reorder message when        *
* necessary.  The search of the inventory file uses an index of stock  *
* numbers.  This index is read into main memory from an unformatted    *
* file.  Identifiers used are:                                         *
*     LIMIT   :  upper limit on the number of items in inventory file  *
*     LREC    :  length of records in inventory file                   *
*     INUNIT  :  unit number of INFILE                                 *
*     INDEX   :  array of item numbers (INDEX(0) = # of items)         *
*     RESPON  :  response from user (Y or N) re more orders to process *
*                                                                      *
* Input:  RESPON                                                       *
* Output: User prompts                                                 *
************************************************************************

      INTEGER LREC, LIMIT
      PARAMETER (LIMIT = 1000, LREC = 43)
      CHARACTER*1 RESPON
      INTEGER INUNIT, INDEX(0:LIMIT)

      CALL INIT(INUNIT, INDEX, LIMIT, LREC)

* Repeat the following until there are no more transactions

10    CONTINUE
          CALL TRANS(INUNIT, INDEX, LIMIT)
          PRINT *, 'MORE TRANSACTIONS (Y OR N)?'
          READ '(A)', RESPON
      IF (RESPON .EQ. 'Y') GO TO 10

      END


**INIT******************************************************************
* This subroutine opens the inventory file and the index file and con- *
* structs the array INDEX of item numbers.  Identifiers used:          *
*     INUNIT, INDEX, LIMIT, LREC as in MAIN                            *
*     INFILE          :  name of inventory file                        *
*     IXFILE, IXUNIT  :  name and unit number of index file            *
*                                                                      *
* Accepts:  INDEX, LIMIT, LREC                                         *
* Input:    INFILE, IXFILE                                             *
* Returns:  INUNIT, INDEX                                              *
* Output:   User prompts                                               *
************************************************************************

      SUBROUTINE INIT(INUNIT, INDEX, LIMIT, LREC)

      CHARACTER*20 INFILE, IXFILE
      INTEGER INUNIT, INDEX(0:LIMIT), IXUNIT, LREC, LIMIT

      PRINT *, 'ENTER NAME OF INVENTORY FILE:'
      READ '(A)', INFILE
      CALL OPENER(INFILE, INUNIT, 'FORMATTED', 'OLD', 'DA', LREC)
      PRINT *, 'ENTER NAME OF INDEX FILE:'
      READ '(A)', IXFILE
      CALL OPENER(IXFILE, IXUNIT, 'UNFORMATTED', 'OLD', 'SE', 0)
      CALL CONST(IXUNIT, INDEX, LIMIT)
      END

**OPENER****************************************************************
* Subroutine to open a file and assign it a unit number.  Successive   *
* calls to this subroutine assign unit numbers 10, 11, 12, ...  A call *
* to OPENER with TYPE = 'SE' open a sequential file, and a call with   *
* TYPE = 'DA' opens a direct-access file.  Variables used are:         *
*     FNAME  :  name of file to be opened                              *
*     NUNIT  :  unit number to be connected to file (integer)          *
*     FORMSP :  'FORMATTED' or 'UNFORMATTED' depending on whether file *
*            :  is formatted or unformatted                            *
*     STAT   :  status of file ('OLD', 'NEW', 'SCRATCH', etc.)         *
*     TYPE   :  indicates whether file is sequential or direct access  *
*     LREC   :  length of records for direct access files              *
*                                                                      *
* Accepts:  FNAME, FORMSP, STAT, TYPE, LREC                            *
* Returns:  NUNIT                                                      *
* Output:   Message re opening of files                                *
************************************************************************

      SUBROUTINE OPENER(FNAME, NUNIT, FORMSP, STAT, TYPE, LREC)

      CHARACTER*(*) FNAME, FORMSP, STAT, TYPE
      INTEGER LREC, NUNIT

      IF (TYPE .EQ. 'SE') THEN
          PRINT *, FNAME, ' IS OPENED FOR SEQUENTIAL ACCESS'
      ELSE
          PRINT *, FNAME, ' IS OPENED FOR DIRECT ACCESS'
      END IF
      END


**CONST*****************************************************************
* This subroutine constructs the array INDEX of item numbers from the  *
* (unformatted) file IXFILE.  IXFILE, IXUNIT, INDEX, and               *
* LIMIT are as in MAIN.                                                *
*                                                                      *
* Accepts:  IXUNIT, INDEX, LIMIT                                       *
* Returns:  INDEX                                                      *
************************************************************************

      SUBROUTINE CONST(IXUNIT, INDEX, LIMIT)

      INTEGER LIMIT, INDEX(0:LIMIT), IXUNIT

      PRINT *, 'INDEX CONSTRUCTED'
      END

**TRANS*****************************************************************
* This subroutine processes a transaction by accepting an order for    *
* a certain item from the terminal, searching the array INDEX to find  *
* the number of the record in the inventory file describing this item, *
* and then updating this record (displaying out-of-stock and/or reorder*
* messages at the terminal when necessary).  Variables used:           *
*     INUNIT, INDEX, LIMIT as in MAIN and INIT                         *
*     NUMORD  :  order number                                          *
*     INDEX   :  item number                                           *
*     QUANT   :  number of items ordered                               *
*     NUMREC  :  number of record containing information re INDEX      *
*             :  (VALUE 0 indicates item not found)                    *
*                                                                      *
* Accepts:  INUNIT, INDEX, LIMIT                                       *
************************************************************************

      SUBROUTINE TRANS(INUNIT, INDEX, LIMIT)

      INTEGER LIMIT, INDEX (0:LIMIT), INUNIT, NUMORD, ITNUM, QUANT,
     +        NUMREC

      CALL SEARCH(INDEX, ITNUM, LIMIT, NUMREC)
      CALL PROCES(NUMREC, QUANT, NUMORD, INUNIT)
      END

**SEARCH****************************************************************
* This subroutine searches the array INDEX of item numbers to locate   *
* the number NUMREC of the record in the inventory file containing     *
* information re the item with item number INDEX.  LIMIT is as in MAIN.*
*                                                                      *
* Accepts:  INDEX, ITNUM, LIMIT                                        *
* Returns:  NUMREC                                                     *
************************************************************************

      SUBROUTINE SEARCH(INDEX, ITNUM, LIMIT, NUMREC)

      INTEGER LIMIT, INDEX(0:LIMIT), ITNUM, NUMREC

      PRINT *, 'SEARCHING INDEX'
      END


**PROCES****************************************************************
* This subroutine processes an order (order # NUMORD) for QUANT items. *
* The NUMREC-th record of the inventory file (unit number INUNIT) is   *
* examined to determine whether the number in stock is sufficient to   *
* fill the order.  If not, an out-of-stock message will be displayed   *
* at the terminal.  In either case, this record will be updated.       *
* Also, if the new number in stock is below the reorder point, a       *
* reorder message will be displayed at the terminal.                   *
*                                                                      *
* Accepts:  NUMREC, QUANT, NUMORD, INUNIT                              *
************************************************************************

      SUBROUTINE PROCES(NUMREC, QUANT, NUMORD, INUNIT)

      INTEGER NUMREC, QUANT, NUMORD, INUNIT

      PRINT *, 'PROCESSING ORDER'
      END
