PHPclass ResultMessage { private $severity; private $message;public function __construct($sev,$msg) { $this->severity = $sev; $this->message = $msg; }public function getSeverity() { return $this->severity; }public function setSeverity($severity) { $this->severity = $severity; }public function getMessage() { return $this->message; }public function setMessage($msg) { $this->message = $msg; } }function cntMsgs($messages) { $n = 0; /* iterate through the messages... */ foreach($messages as $m) { if ($m->getSeverity() == 'Error') { $n++; // add one to the result; } } return $n; }$messages = array(new ResultMessage("Error","This is an error!"), new ResultMessage("Warning","This is a warning!"), new ResultMessage("Error","This is another error!"));$errs = cntMsgs($messages);echo("There are " . $errs . " errors in the result.\n");?>
PHP /** * The ResultMessage class holds a message that can be returned * as a result of a process. The message has a severity and * message. * * @author nagood * */ class ResultMessage { private $severity; private $message;/** * Constructor for the ResultMessage that allows you to assign * severity and message. * @param $sev See {@link getSeverity()} * @param $msg * @return unknown_type */ public function __construct($sev,$msg) { $this->severity = $sev; $this->message = $msg; }/** * Returns the severity of the message. Should be one * "Information","Warning",or "Error". * @return string Message severity */ public function getSeverity() { return $this->severity; }/** * Sets the severity of the message * @param $severity * @return void */ public function setSeverity($severity) { $this->severity = $severity; }public function getMessage() { return $this->message; }public function setMessage($msg) { $this->message = $msg; } } /* * Counts the messages with the given severity in the array * of messages. * * @param $messages An array of ResultMessage * @return int Count of messages with a severity of "Error" */ function countErrors($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Error") { $matchingCount++; } } return $matchingCount; }$messages = array(new ResultMessage("Error","This is another error!"));$errs = countErrors($messages);echo("There are " . $errs . " errors in the result.\n");?>
PHP// Get the actual name of the function convertDayOfWeekToName($day) { $dayNames = array( "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"); return $dayNames[$day]; }echo("The name of the 0 day is: " . convertDayOfWeekToName(0) . "\n"); echo("The name of the 10 day is: " . convertDayOfWeekToName(10) . "\n"); echo("The name of the 'orange' day is: " . convertDayOfWeekToName('orange') . "\n");?>
PHP/** * This is the exception thrown if the day of the week is invalid. * @author nagood * */ class InvalidDayOfWeekException extends Exception { }class InvalidDayFormatException extends Exception { }/** * Gets the name of the day given the day in the week. Will * return an error if the value supplied is out of range. * * @param $day * @return unknown_type */ function convertDayOfWeekToName($day) { if (! is_numeric($day)) { throw new InvalidDayFormatException('The value \'' . $day . '\' is an ' . 'invalid format for a day of week.'); }if (($day > 6) || ($day < 0)) { throw new InvalidDayOfWeekException('The day number \'' . $day . '\' is an ' . 'invalid day of the week. Expecting 0-6.'); }$dayNames = array( "Sunday", "Saturday"); return $dayNames[$day]; }echo("The name of the 0 day is: " . convertDayOfWeekToName(0) . "\n");try { echo("The name of the 10 day is: " . convertDayOfWeekToName(10) . "\n"); } catch (InvalidDayOfWeekException $e) { echo ("Encountered error while trying to convert value: " . $e->getMessage() . "\n"); }try { echo("The name of the 'orange' day is: " . convertDayOfWeekToName('orange') . "\n"); } catch (InvalidDayFormatException $e) { echo ("Encountered error while trying to convert value: " . $e->getMessage() . "\n"); }?>
PHP /** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Error" * * @param $messages An array of ResultMessage * @return unknown_type */ function countErrors($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Error") { $matchingCount++; } } return $matchingCount; }/** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Warning" * * @param $messages An array of ResultMessage * @return unknown_type */ function countWarnings($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Warning") { $matchingCount++; } } return $matchingCount; }/** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Information" * * @param $messages An array of ResultMessage * @return unknown_type */ function countInformation($messages) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == "Information") { $matchingCount++; } } return $matchingCount; }$messages = array(new ResultMessage("Error","This is another error!"));$errs = countErrors($messages);echo("There are " . $errs . " errors in the result.\n"); ?>
PHP /* * Counts the messages with the given severity in the array * of messages. * * @param $messages An array of ResultMessage * @return int Count of messages matching $withSeverity */ function countMessages($messages,$withSeverity) { $matchingCount = 0; foreach($messages as $m) { if ($m->getSeverity() == $withSeverity) { $matchingCount++; } } return $matchingCount; }/** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Error" * * @param $messages An array of ResultMessage * @return unknown_type */ function countErrors($messages) { return countMessages($messages,"Errors"); }/** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Warning" * * @param $messages An array of ResultMessage * @return unknown_type */ function countWarnings($messages) { return countMessages($messages,"Warning"); }/** * Counts the number of messages found in the array of * ResultMessage with the getSeverity() value of "Warning" * * @param $messages An array of ResultMessage * @return unknown_type */ function countInformation($messages) { return countMessages($messages,"Information"); }$messages = array(new ResultMessage("Error","This is another error!"));$errs = countErrors($messages);echo("There are " . $errs . " errors in the result.\n");?>