演習 3 : 3 番目のデータ入力クラス
チュートリアル本編の演習の説明に戻るには、ここをクリックします。
-
VS Code - ObjectScript を使用して、ObjectScript.DataEntry2 の先頭行を ObjectScript.DataEntry3 に変更し、ObjectScript.DataEntry3 という名前で保存します。
-
Prompt() メソッドの下に、3 つの検証メソッドを追加します。まず、ValidName() メソッドを記述します。
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 }
-
次に、ValidPhone() メソッドを記述します。電話番号の検証後、必要な場合は先頭に既定のエリア・コードを追加します。
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 }
-
次に、ValidDOB() メソッドを記述します。未来の日付を許可しないようにします。
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 }
-
Do/While ループを使用するように Prompt() を編集し、新規の検証メソッドを 3 つ使用して、データが正しく入力されるまでプロンプトを繰り返します。dob と intdob の前にドットを付けて、これらの引数が参照によって渡されるようにしてください。
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 }
-
answers を渡すように Main() を変更します。Prompt() で、answers の前にドットを付けます。
Class ObjectScript.DataEntry3 { /// Main loop section ClassMethod Main() { while ..Prompt(.answers) { do ..Display(answers) } } // this is where the Prompt() method is }
-
Prompt() のシグニチャを以下のように変更し、Publiclist を削除します。
ClassMethod Prompt(ByRef answers as %String) as %Boolean
Prompt() の末尾で、return 1 の直前に、回答をパックする以下の行を追加します。
set answers = $listbuild(name, phone, intdob)
-
Display() のシグニチャを編集して、Publiclist を削除し、回答をアンパックして intdob を希望の外部形式に変換する行を先頭に追加します。
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 !, "========================================", ! } }
-
[保存] ボタン、[コンパイル] ボタンをクリックします。
-
ターミナルを開始し、do ##class(ObjectScript.DataEntry3).Main() と入力してメソッドを実行します。