...

OOP Calculator [Mac] (What you think about that code ?)

Mac II
03-02-2011, 09:06 PM
Please , just tell me if it is functionality , if not give new source and i will study it , but if it is don't give new source because it is still functionality .

<?php
class Calculator {
public $x;
public $y;
public $operator;
public function calculate($x, $operator, $y) {
$this->setX($x);
$this->setY($y);
$this->setOperator($operator);
if(!isset($this->x) or !isset($this->operator) or !isset($this->y) or !is_numeric($this->x) or !is_numeric($this->y)):
throw new exception('X, operator or Y is not set or X or Y is not numeric');
else:
if($this->operator !== "+" or $this->operator !== "-" or $this->operator !== "*" or $this->operator !== "/"):
throw new invalidargumentexception('Invalid Operator');
else:
switch($this->operator):
case "+":
echo $x + $y;
break;
case "-":
echo $x - $y;
break;
case "*":
echo $x * $y;
break;
case "/":
echo $x / $y;
break;
endswitch;
endif;
endif;
}
public function setX($string) {
$this->x = $string;
}
public function setY($string) {
$this->y = $string;
}
public function setOperator($string) {
$this->operator = $string;
}
}
$obj = new Calculator;
$obj->calculate("1", "+", "1");
?>

Thanks!

Fou-Lu
03-03-2011, 12:33 AM
You don't need any members for this at all since thing will need to persist. The 'x', 'y', and 'operator' setters are redundant since they are overwritten with any calculate call.
When doing OO code though, you may be better off using a composite model to write things like a calculator. It allows for extremely easy updating of functionality.

<?php
interface ICalculation
{
public function calculate($lhs, $rhs);
}

class Calculator
{
public static function calculate($lhs, $rhs, ICalculation $fp)
{
return $fp->calculate($lhs, $rhs);
}
}

class CalculationAdd implements ICalculation
{
public function calculate($lhs, $rhs)
{
if (!is_numeric($lhs) || !is_numeric($rhs))
{
throw new Exception(NAN);
}
return $lhs + $rhs;
}
}

class CalculationSubtract implements ICalculation
{
public function calculate($lhs, $rhs)
{
if (!is_numeric($lhs) || !is_numeric($rhs))
{
throw new Exception(NAN);
}
return $lhs - $rhs;
}
}

class CalculationStringConcat implements ICalculation
{
public function calculate($lhs, $rhs)
{
return $lhs . $rhs;
}
}

// Usage
$a = 10;
$b = 5;

printf('$a + $b = %f' . PHP_EOL, Calculator::calculate($a, $b, new CalculationAdd()));
printf('$a - $b = %f' . PHP_EOL, Calculator::calculate($a, $b, new CalculationSubtract()));
try
{
printf('"cat" - "dog" = %f' . PHP_EOL, Calculator::calculate('cat', 'dog', new CalculationAdd()));
}
catch (Exception $ex)
{
print $ex->getMessage() . PHP_EOL;
}

printf('"cat" - "dog" = %s' . PHP_EOL, Calculator::calculate('cat', 'dog', new CalculationStringConcat()));


Validation is much much easier this way since its done at a per calculation basis. 'cat' + 'dog' using CalculateAdd will fail with a NAN, but 'cat' + 'dog' using a CalculateStringConcat will concat together. There is no end to the calculations you can do this way, but it is bound by a $lhs and $rhs only rule (which is fine anyway). All that needs to happen is a new class written to perform the calculation.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum