$firstChar = substr($id, 0, 1); if ($alphanumeric && $length > 0 && $length <= MAX_LENGTH_ID && ($firstChar == 'B' || $firstChar == 'S' || $firstChar == 'G')) { switch ($firstChar) { case 'B': $cust = new Bronze_Customer($id); break; case 'S': $cust = new Silver_Customer($id); break; case 'G': $cust = new Gold_Customer($id); break; } } else { throw new \InvalidArgumentException('Incorrect id passed in, please ensure id is 1 to 30 alphanumeric characters in length and starts with either a \'B\' or \'S\' or \'G\'.'); } return $cust; } } /** * Q3 pass in id */ $cust = CustomerFactory::get_instance('S234433'); $cust->deposit(45.3); echo 'id is ' . $cust->get_id() . ' customer balance is ' . $cust->get_balance() . '<br>'; /** * Q4 generate id */ $cust = CustomerFactory::get_instance('G'); $cust->deposit(45.3); echo 'id is ' . $cust->get_id() . ' customer balance is ' . $cust->get_balance();
* @package SoftwareEngineerTest * based on give ID, then use factory method to initiate correct */ class CustomerFactory { /** * @param $id * @return Bronze_Customer|Gold_Customer|Silver_Customer * @throws Exception */ public static function get_instance($id) { $firstIdString = substr($id, 0, 1); switch ($firstIdString) { case 'B': return new Bronze_Customer($id); break; case 'S': return new Silver_Customer($id); break; case 'G': return new Gold_Customer($id); break; default: throw new Exception('InvalidArgument .'); break; } } } CustomerFactory::get_instance('G123456');