<?php
   require_once("../../Smarty-3.1.11/Smarty-3.1.11/libs/Smarty.class.php");
   class Arvutaja{
      private $arv1=0, $arv2=0, $tehe="+", $vastus=0;
	  private $lubatudtehted=array("+", "-", "x", "/");
	  private $smarty=null;
	  function loeAndmed($m){
	     if(isSet($m["arv1"])){$this->arv1=floatval($m["arv1"]);}
		 else{throw new Exception("Arv1 puudub");}
	     if(isSet($m["arv2"])){$this->arv2=floatval($m["arv2"]);}
		 else{throw new Exception("Arv2 puudub");}
		 if(isSet($m["tehe"])){
		   if(in_array($m["tehe"], $this->lubatudtehted)){
		     $this->tehe=$m["tehe"];
		   } else {throw new Exception("Lubamatu tehe ".$m["tehe"]);}
		 }
		 else{throw new Exception("Tehe puudub");}
	  }
	  function t88tleVeebiP2ring(){ 
	     if(isSet($_REQUEST["arv1"])){
	       $this->loeAndmed($_REQUEST);
		 }
	  }
	  function arvutaVastus(){
	    if($this->tehe=="+"){$this->vastus=$this->arv1+$this->arv2;}
	    if($this->tehe=="-"){$this->vastus=$this->arv1-$this->arv2;}
	    if($this->tehe=="x"){$this->vastus=$this->arv1*$this->arv2;}
	    if($this->tehe=="/"){
		 if($this->arv2==0){
		  $this->vastus="Jagamine nulliga";
		 } else {
		  $this->vastus=$this->arv1/$this->arv2;
		 }
		}
	  }
	  function leiaVastus(){
	     $this->arvutaVastus();
	  }
	  private function kontrolliSmarty(){
	     if($this->smarty==null){
		    $this->smarty=new Smarty();
		 }
	  }
	  function kuvaVeebis(){
	     $this->kontrolliSmarty();
         $this->smarty->assign("arv1", $this->arv1);		 
         $this->smarty->assign("arv2", $this->arv2);		 
         $this->smarty->assign("tehted", $this->lubatudtehted);		 
         $this->smarty->assign("valitudtehe", $this->tehe);	 
         $this->smarty->assign("vastusteade", $this->vastus);			 
		 $this->smarty->display("arvutusmall.tpl");
	  }
	  function andmedTekstina(){
	     return "$this->arv1$this->tehe$this->arv2=$this->vastus";
	  }
   }
  
   
?>




