Exercise 1: Modify Data Entry Routine
-
Write the validName function.
public function validName(name As %String) As %String ' validate a name - just checks for 2 pieces using "," ' returns 0 for an invalid name and writes error message ' else returns formatted name if len(name, ",") = 2 then return formatName(name) else print "Enter Last,First" : println return 0 end if end function
-
Write the formatName() function.
public function formatName(name As %String) As %String ' change user's entry into proper name format ' SMITH,JOHN and smith,john -> Smith,John dim ln, fn ln = piece(name, ",", 1) : fn = piece(name, ",", 2) ln = ucase(left(ln, 1)) & lcase(mid(ln, 2, len(ln))) fn = ucase(left(fn, 1)) & lcase(mid(fn, 2, len(fn))) return ln & "," & fn end function
-
Write the validPhone function. After verifying the phone number, add the default area code if necessary.
public function validPhone(phone As %String) As %String ' validate a phone - just checks for 3 pieces using "-" and length ' returns 0 for an invalid phone and writes error message ' else returns unchanged phone with default area code added if (len(phone) = 8 and len(phone, "-") = 2) then phone = "617-" & phone ' add default area code end if if (len(phone) = 12 and len(phone, "-") = 3) then return phone else print "Enter ###-###-#### or ###-####" : println return 0 end if end function
-
Write the validDOB function. You'll need to use On Error Goto for invalid dates. Make sure you disallow future dates.
public function validDOB(dob As %String) As %Integer ' validate a date of birth - a valid date before or equal to today ' returns 0 for invalid dates and writes error message ' else returns internal format for valid dates On Error Goto BadDate if DateDiff("d", dob, Date) < 0 then print "Enter a date in the past" : println return 0 else return DateConvert(dob, vbToInternal) end if BadDate: print "Invalid date" : println return 0 end function
-
Edit the prompt subroutine. Use Do/Loop While to cause prompts to repeat until the data is correct, using the three new “valid” functions.
private sub prompt() ' subroutine for prompting do input "Name: ", name : println if (name = "") then exit sub name = validName(name) loop while name = 0 do input "Phone (617): ", phone : println phone = validPhone(phone) loop while phone = 0 do input "DOB: ", dob : println intdob = validDOB(dob) loop while intdob = 0 println end sub
-
Edit the display subroutine. Add code to convert the date to external format.
public sub display() ' subroutine for displaying data println "Name:", space(15), name println "Phone:", space(14), phone println "DOB:", space(16), DateConvert(intdob, vbToExternal) println end sub