       $set p(prexml) endp
       identification division.
       program-id. customerdbxml.

       select customer-file assign to "customers"
                            organization is indexed
                            record key is customer-id
                            alternate key is customer-name
                            access mode is dynamic.

      ******************************************************************
      *File definition for the customer database
      ******************************************************************
       fd customer-file.
       copy "customer.cpy".


       working-storage section.
      ******************************************************************
      *Display definition for the program's main menu
      ******************************************************************
       01 disp-main-menu.
           05 filler       pic x(30)
               value "Customer Information Database".
           05 filler       pic x(130).
           05 filler       pic x(15) value "1. Add Customer".
           05 filler       pic x(65) value spaces.
           05 filler       pic x(16) value "2. Find Customer".
           05 filler       pic x(64) value spaces.
           05 filler       pic x(18) value "7. Delete Customer".
           05 filler       pic x(62) value spaces.
           05 filler       pic x(15) value "9. Exit Program".
           05 filler       pic x(145) value spaces.
           05 filler       pic x(9) value "Choice : ".

      ******************************************************************
      *Display definition for the program's new customer menu
      ******************************************************************
       01 disp-add-customer-menu.
           05 filler       pic x(16) value "Add New Customer".
           05 filler       pic x(144).

      ******************************************************************
      *Display definition for the program's find customer record menu
      ******************************************************************
       01 disp-find-customer-menu.
           05 filler       pic x(20) value "Find Customer Record".
           05 filler       pic x(140).
           05 filler       pic x(22) value "Enter Customer ID   : ".
           05 filler       pic x(58) value spaces.
           05 filler       pic x(22) value "Enter Customer Name : ".

      ******************************************************************
      *Display definition for the program's delete customer record menu
      ******************************************************************
       01 disp-delete-customer-menu.
           05 filler       pic x(23) value "Delete Customer Record".
           05 filler       pic x(137).
           05 filler       pic x(22) value "Enter Customer ID   : ".

      ******************************************************************
      *Display definition for a customer record
      ******************************************************************
       01 disp-customer-record.
           05 filler       pic x(17) value "Customer ID    : ".
           05 filler       pic x(63) value spaces.
           05 filler       pic x(17) value "Customer Name  : ".
           05 filler       pic x(63) value spaces.
           05 filler       pic x(17) value "Address Line 1 : ".
           05 filler       pic x(63) value spaces.
           05 filler       pic x(17) value "Address Line 2 : ".
           05 filler       pic x(63) value spaces.
           05 filler       pic x(17) value "Town/City      : ".
           05 filler       pic x(63) value spaces.
           05 filler       pic x(17) value "State/County   : ".
           05 filler       pic x(63) value spaces.
           05 filler       pic x(17) value "ZIP/postcode   : ".
           05 filler       pic x(63) value spaces.
           05 filler       pic x(17) value "Phone Number   : ".

      *Program flags
       01  prog-flags.
	   05 main-menu-choice     pic x.
	   05 prompt-yes-no        pic x.
	   05 record-key-is-name   pic x.
	   05 record-ok            pic x.
       01  xml-status              pic x value "0". *> 0 - No Action.
                                                    *> 2 - safe to write file

      *XML File status
       01 file-status-spec pic S9(9).


      *Record Keys
       01 key-customer-id   pic x(5).
       01 key-customer-name pic x(50).

       procedure division.
      *Open the customer file
           open i-o customer-file

      *Do an initial read from the file (even if the file is empty)
           if xml-status = "0"
               read customer-file
               move "1" to xml-status
           end-if

      ******************************************************************
      *Controlling loop for the main menu
      ******************************************************************
           perform until exit
      *Initialize the program variables and the record members
               move spaces to customer-record
               move spaces to prog-flags

      *Display the main menu
               display spaces with blank screen
               display disp-main-menu
               accept main-menu-choice at line 8 column 10

               evaluate true
               when main-menu-choice = "1"
                   perform add-customer-record
               when main-menu-choice = "2"
                   perform find-customer-record
               when main-menu-choice = "7"
                   perform delete-customer-record
               when main-menu-choice = "9"
                   perform program-end
                   exit perform
               when other
                   display "Invalid Menu Option"
                       at line 10 column 1
                   display "Press ENTER key to continue : "
                       at line 11 column 1
                   accept prompt-yes-no at line 11 column 31
               end-evaluate
           end-perform

      *Write the Internal XML DOM to file
       if xml-status = "2"
           write customerdb
       end-if

      *Close the customer file
           close customer-file

      *Exit the program
           exit program
           stop run.

      ******************************************************************
      *Add a record to the customer file
      ******************************************************************
       add-customer-record section.
      *Display the add customer menu
           display spaces with blank screen
           display disp-add-customer-menu
           display disp-customer-record

      *Get the new customer id
           accept customer-id at line 3 column 18

      *Get the new customer name
           accept customer-name at line 4 column 18

      *Get the first line of the new customer's address
           accept address-line-1 at line 5 column 18

      *Get the second line of the new customer's address
           accept address-line-2 at line 6 column 18

      *Get the Town/City of the new customer's address
           accept town-city at line 7 column 18

      *Get the State/County of the new customer's address
           accept state-county at line 8 column 18

      *Get the ZIP/Postcode of the new customer's address
           accept zip-postcode at line 9 column 18

      *Get the phone number of the new customer
           accept phone-number at line 10 column 18

      *Prompt user if they wish to add record to the customer file.  If
      *the user returns "y" or "Y", write the record to the file
           display "Do you wish to add this record (y/n) ? "
               at line 12 column 1
           move spaces to prompt-yes-no
           accept prompt-yes-no at line 12 column 40

           move "y" to record-ok
           if prompt-yes-no = "y" or prompt-yes-no = "Y"
               perform file-write-customer-record
               if record-ok = "n"
                   display "Error : Unable to write record. "
                       at line 14 column 1
               end-if
           end-if
           exit section.

      ******************************************************************
      *Find a record from the file.  File index is customer-id
      ******************************************************************
       find-customer-record section.
      *Get the customer-id and attempt to read the record from the
      *customer file
           display spaces with blank screen
           display disp-find-customer-menu
           accept key-customer-id at line 3 column 23
           accept key-customer-name at line 4 column 23

      *Determine the key to use for finding the record.  By default, the
      *customer id is used
           if key-customer-id not = spaces
               move "n" to record-key-is-name
           else
               move "y" to record-key-is-name
           end-if

      *Read the record from the file
           move "y" to record-ok
           perform file-read-customer-record

      *Display the customer record
           display " "
           display " "

           if record-ok = "y"     
               display disp-customer-record
               display customer-id at line 6 column 18
               display customer-name at line 7 column 18
               display address-line-1 at line 8 column 18
               display address-line-2 at line 9 column 18
               display town-city at line 10 column 18
               display state-county at line 11 column 18
               display zip-postcode at line 12 column 18
               display phone-number at line 13 column 18
           else if record-ok = "n"
               display "Unable to find record with customer ID "
               display key-customer-id at line 6 column 40
           end-if end-if

      *Prompt the user to continue
           move spaces to prompt-yes-no
           if record-ok ="y"
               display "Press ENTER key to continue : "
                   at line 15 column 1
               accept prompt-yes-no at line 15 column 31
           else
               display "Press ENTER key to continue : "
                   at line 8 column 1
               accept prompt-yes-no at line 8 column 31
           end-if
           exit section.

      ******************************************************************
      *Delete a record from the customer file.  File index is
      *customer-id
      ******************************************************************
       delete-customer-record section.
      *Get the customer-id
           display spaces with blank screen
           display disp-delete-customer-menu
           accept key-customer-id at line 3 column 23

      *Prompt the user to continue with record deletion
           move spaces to prompt-yes-no
           display "Do you wish to delete this record (y/n) ? "
               at line 5 column 1
           accept prompt-yes-no at line 5 column 43

      *Delete the customer record
           move "y" to record-ok
           if prompt-yes-no = "y" or prompt-yes-no = "Y"
               perform file-read-customer-record
               if record-ok = "y"
                   perform file-delete-customer-record
               end-if
               if record-ok = "n"
                   display "Error : Unable to delete record ID "
                       at line 7 column 1
                   display key-customer-id at line 7 column 36
               else
                   display "Customer record deleted" at line 7 column 1
	       end-if
           else
               display "Record delete cancelled" at line 7 column 1
	   end-if

      *Prompt the user to contine
           display "Press ENTER key to continue : " at line 9 column 1
           move space to prompt-yes-no
           accept prompt-yes-no at line 9 column 31
	   exit section.


      ******************************************************************
      *File and record I/O operation.  Write a customer record to the
      *file
      ******************************************************************
       file-write-customer-record section.
           move "2" to xml-status
           write customerdb key is all customer-record
	   exit section.

      ******************************************************************
      *File and record I/O operation.  Read a customer record from the
      *file
      ******************************************************************
       file-read-customer-record section.
           start customer-file key is customer-record
           perform until exit
               read customer-file next key is customer-record
               if file-status-spec not = 0
                   move "n" to record-ok
                   exit perform
               else
                   if record-key-is-name = "y"
                       if customer-name = key-customer-name
                           exit perform 
                       end-if
                   else
                       if customer-id = key-customer-id
                           exit perform
                       end-if
                   end-if
               end-if
           end-perform
               *>record key is customer-name
      *            invalid key move "n" to record-ok
      *        end-read
      *     else
      *         read customer-file *>record key is customer-id
      *            invalid key move "n" to record-ok
      *        end-read
      *	   end-if

           move space to record-key-is-name
	   exit section.

      ******************************************************************
      *File and record I/O operation.  Delete a customer record from the
      *file
      ******************************************************************
       file-delete-customer-record section.
           move "2" to xml-status
           delete customer-file record key is customer-record
      *        invalid key move "n" to record-ok
           exit section.

      ******************************************************************
      *End of program
      ******************************************************************
       program-end section.
           display spaces with blank screen
	   exit section.
