Monday, March 26, 2012

Day Twenty Seven: More Work with RMH Homebase


Exercise 6.1:
This is a very simple exercise that involves adding setters and getters for employer, contact person, and contact phone variables in the Person class.
Note: This exercise does not mention implementing a "status" variable, but it is necessary for the next few exercises. I went ahead and included the variable in this exercise.

Initialize the variables:

private $employer;           // name of current employer
private $contact_person;    // name of a contact Person
private $contact_phone;    // phone of the contact Person
private $status;          // a Person may be "active" or "inactive"

Make the setters:

function set_employer ($name) {
    $this->employer = $name;
}
function set_contact_person ($name) {
    $this->contact_person = $name;
}
function set_contact_phone ($phone) {
    $this->contact_phone = $phone;
}
function set_status ($status) {
    $this->status = $status;
}

Make the getters:

function get_employer () {
    return $this->employer;
}
function get_contact_person () {
    return $this->contact_person;
}
function get_contact_phone () {
    return $this->contact_phone;
}
function get_status () {
    return $this->status;
}

Exercise 6.2:
This exercise involves updating Person's constructor to implement status, employer, contact, and contact phone. The unit test, testPerson.php, also needs to be updated.

Modify the constructor with the new variables:


/**
 * constructor for all persons
 */
function __construct ($f, $l, $a, $c, $s, $z, $p1, $p2, $e, $t,
    $bg, $in, $sh, $con, $whe, $exp, $mot, $spe,
    $av, $sch, $hist, $bd, $sd, $pubn, $myn, $privn, $pass
    $status, $employer, $contact, $contact_phone) {
        $this->status = $status;
        $this->employer = $employer;
        $this->contact_person = $person;
        $this->contact_phone = $contact_phone;
        ...
}


Include the variables in the unit test:



//I need to make an object to test. This is just dummy data.
 $myPerson = new Person("Taylor","Talmage","928 SU","Brunswick","ME",04011,
 2074415902,2072654046,"ttalmage@bowdoin.edu","applicant,volunteer,sub","no","no","no","", "", "", "", "", "Mon9-12, Tue9-12, Wed12-3", "", "", "02-19-89", "03-14-08","this is one of my notes","this is a cool note","this is another note","Taylor2074415902", "active", "McDonalds", "Ronald McDonald", 8034563452);



Test getters and setters:


$this->assertTrue($myPerson->get_employer() == "McDonalds");
$myPerson->set_employer("Burger King");
$this->assertTrue($myPerson->get_employer() == "Burger King");


$this->assertTrue($myPerson->get_contact_person() == "Ronald McDonald");
$myPerson ->set_contact_person("The King");
$this->assertTrue($myPerson->get_contact_person() == "The King");

$this->assertTrue($myPerson->get_contact_phone() == 8034563452);
$myPerson->set_contact_phone(8035467654);
$this->assertTrue($myPerson->get_contact_phone() ==  8035467654); 


$this->assertTrue($myPerson->get_status() == "active");
$myPerson->set_status("inactive");
$this->assertTrue($myPerson->get_status() == "inactive");



Exercise 6.3:
This exercise asks how set_status could be implemented in order to error check the values provided to the method. Since a valid value for status can only be "active" or "inactive", it would be easy to set up a Boolean expression for error checking.

function set_status ($value) {
    if ($value == "inactive" or $value == "active") {
        $this->status = $value;
    }
    else {
        echo (""active" or "inactive" are the only valid inputs for status");
    }
}

Exercise 6.4:
This exercise involves removing mutators that are not called in any part of the code. As is stated in the book, none of the setters for the Person class are ever called because a new Person object is created every time information is changed; however, the book suggests that we leave these methods intact for reasons that will be discussed in future chapters. All of the getter methods are called at some point in the code base.

No comments:

Post a Comment