Exercises in developing domain classes: Part I

Today’s post is a continuation of exercises in RMH Homebase. For these exercises we will be taking a class and further refining it. In this case we will be looking at the person class.

6.1 Define a new pair of functions to set and retrieve the value of variables $employer, $contact_person, and $contact_phone.

Added in variables:

class Person {

private $employer;

private $contact_person;

private $contact_phone;

Added in getters and setters:

function get_employer(){

return $this->employer;

}

function get_contact_person(){

return $this->contact_person;

}

function get_contact_phone(){

return $this->contact_phone;

}

function set_employer($e){

$this->employer=$e;

}

function set_contact_person($cpe){

$this->contact_person=$cpe;

}

function set_contact_phone($cph){

$this->contact_phone=$cph;

}

6.2 Add four new parameters and corresponding assignments to the constructor for the Person class, so that the status, employer, contact person, and contact person’s phone are properly intitialized. Use the following prototype for your new constructor:

function __construct($f, $l, $a, $c, $s, $z, $p1, $p2, $e, $t, $status, $employer, $contact, $contact_phone, …)

Here is the new initializer:

function __construct($f, $l, $a, $c, $s, $z, $p1, $p2, $e, $t, $status, $employer, $contact, $contact_phone, …){

$this->status = $status;

$this->employer = $employer;

$this->contact_person = $contact;

$this->contact_phone = $contact_phone;

6.3 Modify set_status function to check the the $value passed is valid. Describe unit testing implications of your design decision.

function set_status($st){

if (strcmp($st, “active”)==0 || strcmp($st, “inactive”)==0)

$this->status = $st;

else

$this->status = “error: bad input”;

}

This basic design allows for an simple set of test cases. {“active”, “inactive”, “test”}

The first two should change the status and the last should change the status to error. The other test cases needed are longer strings, blank strings, and numeral strings.

6.4 Refactor the person class by removing all the mutators that are not called from anywhere in the code base.

After removing a few mutators I rebuilt the RMH Homebase and tested to make sure the program was working. There were no visible signs of bugs after playing around with it for a hour.