docs.intersystems.com
Home  /  Application Development: Core Topics  /  ObjectScript Tutorial  /  Exercise 3: Third Data Entry Class


ObjectScript Tutorial
Exercise 3: Third Data Entry Class
[Back]  [Next] 
InterSystems: The power behind what matters   
Search:  


Click here to return to the exercise description in the main part of the tutorial.
  1. Change the top line of ObjectScript.DataEntry2 to ObjectScript.DataEntry3, and save it as ObjectScript.DataEntry3.
  2. Below the Prompt() method, you'll add the three validation methods. First, write the ValidName() method.
    Class ObjectScript.DataEntry3
    {
    // this is where the Prompt() method is
    
    /// use pattern match to validate a name in "Last,First" format.
    /// write error message if invalid
    ClassMethod ValidName(name as %String) as %Boolean     
    {
        if (name?1U.L1","1U.L) {
            return 1
        }
        else {
            write !,"Last,First"
            return 0
        }
    }
    
    // this is where the Display() method is
    }
  3. Next, write the ValidPhone() method. After verifying the phone number, prepend the default area code if necessary.
    Class ObjectScript.DataEntry3
    {
    // this is where the ValidName() method is
    
    /// use RegEx ($match) to validate a phone in "###-####" or "###-###-####" format.
    /// returns the converted phone by reference
    /// write error message if invalid
    ClassMethod ValidPhone(ByRef phone as %String) as %Boolean     
    {
        if $match(phone, "(\d{3}-)?\d{3}-\d{4}") {
            set:($match(phone, "\d{3}-\d{4}")) phone = "617-" _ phone  // add default area code
            return 1
        }
        else {
            write !, "###-###-#### or ###-####"
            return 0
        }
    }
    
    // this is where the Display() method is
    }
  4. Next, write the ValidDOB() method. Make sure you disallow future dates.
    Class ObjectScript.DataEntry3
    {
    // this is where the ValidPhone() method is
    
    /// validate a date of birth using $zdateh and $horolog
    /// returns the internal form of the date of birth by reference
    /// write error message if invalid
    ClassMethod ValidDOB(date as %String, Output convdate as %Date) as %Boolean     
    {
        set convdate = $zdateh(date, 5,,,,,,, -1)
        if (convdate = -1) {
            write !,"Must be a valid past date"
            return 0  // invalid date
        }
        elseif (convdate > $piece($horolog, ",", 1)) {
            write !,"Must be a valid past date"
            return 0  // invalid because it's in the future
        }
        else {
            return 1  // valid date
        }
    }
    
    // this is where the Display() method is
    }
  5. Edit Prompt() to use Do/While loops to cause the prompts to repeat until the data is correct, using the three new validation methods. Remember to precede dob and intdob with a dot so that these arguments are passed by reference.
    Class ObjectScript.DataEntry3
    {
    // this is where the Main() method is
    
    /// prompt
    ClassMethod Prompt() as %Boolean [Publiclist = (name, phone, dob)]
    {   
        do {
            read !, "Name: ", name
            return:(name = "") 0  // user entered nothing so return FALSE and exit method
        }
        while '..ValidName(name)
        
        do {
            read !, "Phone (617): ", phone
        }
        while '..ValidPhone(.phone)
    
        do {
            read !, "DOB: ", dob
        }
        while '..ValidDOB(dob, .intdob)
        return 1  // return true
    }
    
    // this is where the ValidName() method is
    }
  6. Change Main() to pass answers. For Prompt(), precede answers with a dot.
    Class ObjectScript.DataEntry3
    {
    
    /// Main loop section
    ClassMethod Main()     
    {   
        while ..Prompt(.answers) {       
            do ..Display(answers)
        }
    }
    
    // this is where the Prompt() method is
    }
  7. Change the signature of Prompt() to look like this, removing the Publiclist:
    ClassMethod Prompt(ByRef answers as %String) as %Boolean
    Add the following line at the end of Prompt(), just before return 1, that packs up the answers:
    set answers = $listbuild(name, phone, intdob)
  8. Edit the signature of Display(), remove the Publiclist, add a line at the top to unpack the answers, and convert intdob to the external format you like best.
    Class ObjectScript.DataEntry3
    {
    // this is where the ValidDOB() method is
    
    /// display the data
    ClassMethod Display(answers as %String)
    {   
        set $listbuild(name, phone, intdob) = answers
        /* the line above is equivalent to
           set name = $list(answers, 1),
               phone = $list(answers, 2),
               intdob = $list(answers, 3) */
        write !!, "========================================"
        write !, "Name:", ?20, name
        write !, "Phone:", ?20, phone
        write !, "DOB:", ?20, $zdate(intdob, 2)
        write !, "========================================", !
    }
    }
  9. Click the Save and Compile buttons.
  10. Start Terminal, and run your method, by typing do ##class(ObjectScript.DataEntry3).Main().