It is currently Fri Sep 21, 2018 7:17 am


All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Unit 3 - Lesson 2: Create/Edit Database - Exercise 3
PostPosted: Sun Jul 10, 2011 10:16 am 
User avatar

Joined: Wed Nov 17, 2010 8:37 am
Posts: 136
Real Name: Terry L. Wiechmann
Began Programming in MUMPS: 0- 0-1971
Exercise 2 Solution

Routine: MP1PEDIT
Code:
MP1PEDIT ; Create/Edit Parts routine for the MP1 Course.
PNUM ; Part Number
    Write !,"Part Number: "
    Read X
    If X'?1.6N Quit:X["^"!(X="")  Do  Goto PNUM
    . Write !?5,"Enter 1-6 numeric digits. Required."
    Set PNUM=X
    ;
PDESC ; Description
    Write !,"Description: "
    Read X
    If X'?1A.29NAP Quit:X=""  Goto PNUM:X["^" Do  Goto PDESC
    . Write !?5,"Enter 1-30 characters starting with a letter. Required."
    Set PDESC=X
    ;
PQTY ; Quantity
    Write !,"Quantity: "
    Read X
    If X'?1.N Quit:X=""  Goto PDESC:X["^" Do  Goto PQTY
    . Write !?5,"Enter an integer value. Required."
    Set PQTY=X
    ;
PLVL ; Reorder Level (it's time to reorder the part if PQTY<PLVL)
    Write !,"Order Level: "
    Read X
    If X'?1.N Quit:X=""  Goto PQTY:X["^" Do  Goto PLVL
    . Write !?5,"Enter an integer value. Required."
    Set PLVL=X
    ;
PRC ; Replacement Cost
    Write !,"Replacement Cost: "
    Read X
    If X'?.N.1".".2N Quit:X=""  Goto PLVL:X["^" Do  Goto PRC
    . Write !?5,"Enter replacement cost in dollars[.cents]"
    Set PRC=X
    ;
PSP ; Selling Price
    Write !,"Selling Price: "
    Read X
    If X'?.N.1".".2N Quit:X=""  Goto PRC:X["^" Do  Goto PSP
    . Write !?5,"Enter selling price in dollars[.cents]"
    Set PSP=X
    ;
    Goto PNUM

Review
  1. At this point we are collecting each data element, checking it for the correct syntax and setting it into a local variable. If the syntax is wrong, we check to see if the user wants out of the routine or wants to go to the previous query. If not one of these conditions, we give the user a short help message and re-ask the query.
  2. A note about the "^" character and it's use. It was chosen as the character to enter to return to the previous query because it cannot be used in a field value since it is used as the delimiter in the record. Note that the check for it uses the Contains operator. We want to make sure it does not appear anywhere in the field. A simple equals check will not do.
  3. Notice the use of variables that hold values in terms of their lifetimes. X has a lifetime between queries. Each variable such as PNUM, PDESC, etc. has a lifetime of one query. This lesson will be devoted to creating a record and setting it into the ^xxPARTS global. The record will then be persistent and have a lifetime until it is explicitly killed.
Goals
  1. Add File, Edit or Ignore query
  2. Add variable hiding and initialization to front of routine.
  3. Add code to implement Edit mode.
Description
  1. In place of the "Goto PNUM" line, we will write code to handle a query value of "File, Edit or Ignore". This lets the user make a decision about what to do at this point.
  2. If the user enters an "F" then the record will be filed and control returns to the INIT label for the next record entry. If "E" is entered, control will be returned to the PNUM query to edit the current record. If an "I" is entered, the record is not filed. All variables are initialized to null and control is returned to the INIT label. If none of the values were entered, a message is written and control transferred back to the FEI label to ask again.
  3. Lets implement Ignore and Edit functions and stub out the Put (set to the database) subroutine.
    Code:
    FEI ; File/Edit/Ignore Entry
        Set Default="F"
        Write !,"File (F), Edit (E) or Ignore (I) : "
        Write:$Get(PLVL)]"" Default," // "
        Read X
        If X["^" Goto PSP
        If X="" Set X=Default
        ; This code demands rigid upper/lowercase entry. Will be normalized.
        If $Extract("Edit",1,$Length(X))=X Goto PNUM
        If $Extract("Ignore",1,$Length(X))=X Goto INIT
        If $Extract("File",1,$Length(X))=X Do PUT Goto INIT
        Write " Enter F, E or I."
        Goto FEI
        ;
    PUT ;
        Quit
  4. The Ignore function returns to the label INIT which will be inserted above the PNUM label. This code is always executed when a new query is invoked. The top of a routine is usually used for initialization of variables.
  5. We will execute the routine by entering a Do ^MP1PEDIT. Execution begins at the first executable line in the routine. Note that this routine may be called by another routine such as a menu selection routine. In any case, you should always hide (stack) any existing variables you are going to use. That way, when you exit this routine, those variables that exist at call time will be restored to the callers context. If you don't do this, incorrect values will be passed back not to mention extra variables that simply clutter the local symbol table which can lead to storage errors. Review the New command.
    Code:
        New  X,PNUM,PDESC,PQTY,PLVL,PRC,PSP
        ;
    INIT ; Initialize all variables and drop through.
        Set (PNUM,PDESC,PQTY,PLVL,PRC,PSP)=""
        ;
    PNUM ; Part Number
        Write !,"Part Number: "
        Read X
        If X'?1.6N Quit:X["^"!(X="")  Do  Goto PNUM
        . Write !?5,"Enter 1-6 numeric digits. Required"
        Set PNUM=X
        ;
  6. Before starting the Edit function, reread the Create/Edit Prompts subsection under Application Conventions in the Student Guide.
  7. The Edit function lets you change the values you just entered. It returns to the PNUM, not the INIT label. However, notice that the current code will not display the current values and it will simply overwrite them. We need to make each query aware that the associated variable has a value in it, display it and replace it if the user entered a new value.
  8. The following code illustrates the changes you have to make to each query to implement this functionality.
    Code:
    PNUM ; Part Number
        Write !,"Part Number: "
        Write:$Get(PNUM)]"" PNUM," // "
        Read X
        If X="" Set X=PNUM
        If X'?1.6N Goto EXIT:X["^" Do  Goto PNUM
        . Write !?5,"Enter 1-6 numeric digits. Required."
        Set PNUM=X
        ;
  9. Note that entering null now has a different function. We've replaced a simple Quit with a Goto Exit if X is an "^". The Exit label will contain the Quit. This functionality applies to PNUM only. Entering "^" at other queries jumps to the previous query. Notice that we cannot escape from any of the queries. How would you use the "^" entry to jump out of the query? Hint: Add a character after the "^" to perform specific functions.
  10. At this point, implement Edit mode for all queries. The solution is displayed in the next lesson.
    Note: At this point we will not code for required/not required entries.

_________________
Terry L. Wiechmann


Top
Offline Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net