Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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 Code:
    <?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->$string;
        }
        public function 
    setY($string) {
            
    $this->$string;
        }
        public function 
    setOperator($string) {
            
    $this->operator $string;
        }
    }
    $obj = new Calculator;
    $obj->calculate("1""+""1");
    ?>
    Thanks!
    Last edited by Mac II; 03-02-2011 at 09:11 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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 Code:
    <?php
    interface ICalculation
    {
        public function 
    calculate($lhs$rhs);
    }

    class 
    Calculator
    {
        public static function 
    calculate($lhs$rhsICalculation $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_EOLCalculator::calculate($a$b, new CalculationAdd()));
    printf('$a - $b = %f' PHP_EOLCalculator::calculate($a$b, new CalculationSubtract()));
    try
    {
        
    printf('"cat" - "dog" = %f' PHP_EOLCalculator::calculate('cat''dog', new CalculationAdd()));
    }
    catch (
    Exception $ex)
    {
        print 
    $ex->getMessage() . PHP_EOL;
    }

    printf('"cat" - "dog" = %s' PHP_EOLCalculator::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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •