Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class InputException extends Exception
- {
- private $_invalidWarpFactor;
- public function __construct( $message , $code, $factor )
- {
- parent::__construct( $message , $code );
- $this->_invalidWarpFactor = $factor;
- }
- public function getInvalidWarpFactor()
- {
- return $this->_invalidWarpFactor;
- }
- } // End of class Exception
- class FuelException extends Exception
- {
- private $_remainingFuel;
- public function __construct( $message , $code , $remainingFuel )
- {
- parent::__construct( $message , $code );
- $this->_remainingFuel = $remainingFuel;
- }
- public function getRemainingFuel()
- {
- return $this->_remainingFuel;
- }
- } // End of class FuelException
- class WarpDrive
- {
- static private $_dilithiumLevel = 10;
- public function setWarpFactor( $factor )
- {
- if( $factor < 1 )
- {
- throw new InputException( "Warp need to be atleast 1", 1 , $factor );
- }
- else if( $factor > 9 )
- {
- throw new InputException( "Warp Factor exceeds drive specification" , 2 , $factor );
- }
- else if( WarpDrive::$_dilithiumLevel < $factor )
- {
- throw new FuelException( "Insufficient fuel" , 3 , WarpDrive::$_dilithiumLevel );
- }
- else
- {
- WarpDrive::$_dilithiumLevel -= $factor;
- echo "<p>Now traveling at a warp factor $factor</p>";
- }
- }
- } // End of class WarpDrive
- class ChiefEngineer
- {
- public function doWarp( $factor )
- {
- $wd = new WarpDrive();
- $wd->setWarpFactor($factor);
- }
- }
- class Captain
- {
- public function newWarpOrder( $factor )
- {
- $ce = new ChiefEngineer();
- try
- {
- $ce->doWarp( $factor );
- }
- catch( InputException $e )
- {
- echo "<p>Captain's log: Warp factor " . $e->getInvalidWarpFactor() . "? I must be losing my mind ...</p>";
- }
- catch( FuelException $e )
- {
- echo "<p>Captain log: I'm getting fuel problem from the warp engine. It says '" . $e->getMessage();
- echo "' . we have " . $e->getRemainingFuel() . "dilithium left. I guess w're not going anywhere</p>";
- }
- catch( Exception $e )
- {
- echo "<p>Captain's log: something else happend , I don't know what. The message is '" . $e->getMessage() ."'<p>";
- }
- }
- } // End of class Captain
- $c = new Captain();
- $c->newWarpOrder(5);
- $c->newWarpOrder(-1);
- $c->newWarpOrder(12);
- $c->newWarpOrder(4);
- $c->newWarpOrder(9);
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement