Monday, April 8, 2013

Line 19: Chapter 6

Tonight we have been given the assignment to blog some exercises from our book, Software Development.

6.1 - This question basically asks you to create some new functions to retrieve and set values for the variables $employer, $contact_person, and $contact_phone in the Person class in RMH Homebase. So all you had to do was to declare the variables, then write a getter and a setter for each. It basically looks like this. 

private $foo;

//getter for foo
function getFoo(){
      return $this->foo;
}
//setter for foo
function setFoo($f){
      $this->foo = $f;

You can extrapolate the rest from this example.

6.2 - The next question was basically to add these new fields to the constructor, so that they are properly initialized when an object is made. They give you the constructor call to follow, and you fill it in. It looks like this.

function __construct($f, $l, $a, $c, $s, $z, $p1, $p2, $e, $t, $status, $employer, $contact, $contact_phone){
    $this->status = status;
    setEmployer(employer);
    setContactPerson(contact);
    setContactPhone(contact_phone);
}

6.3 - Next question is dealing with the fact that the set_status method will accept any status, when it should only accept the strings "active", "inactive", or any case permutation therein. It asks you to implement a fix for set_status. So, this is basically a string comparing problem. It can be solved like so.

function set_status($value){
if(is_string($value){
    if (strcasecmp("active",$value) == 0 || strcasecmp("active", $value) == 0){
        $this->status = $value;
    }
    else{
         $this->status = NULL;
    }
}
else{
    echo "Status value must be of type string";
}
}

This code should handle any incorrect strings, and cases where value is not passed a string. To test to make sure it works, possibly unit tests cases are setting $value equal to: "active", "inactive", "ACTIVE", "INACTIVE", "iNaCtiVE", "0", "NULL", "you will fail", "", or any other type of value you can throw at it.

6.4 - This next problem has us refactor the Person class by removing all of the mutators that weren't called from anywhere in the codebase.

Turns out none of the setters were called from anywhere in the codebase. All variables were just set manually. So, I removed the setters then tested it. Seemed to work fine.

EDIT: As we discussed this problem in class today, Dr. Bowring pointed out that there is no reason to set the status to NULL in problem 6.3. NULL grants you nothing, and just gives you a new exception you will have to handle later. He suggested instead that we use a default value that is set when incorrect values are given. The else statement now looks like this, using active as the default state.

else{
         $this->status = "active";
    }

No comments:

Post a Comment