<?php
abstract class CashSuper{
abstract public function acceptCash($money);
}
class CashNormal extends CashSuper{
public function acceptCash($money){
return $money;
}
}
class CashRebate extends CashSuper{
public $moneyRebate = null;
public function __construct($moneyRebate){
$this->moneyRebate = $moneyRebate/10;
}
public function acceptCash($money){
return $money*$this->moneyRebate;
}
}
class CashReturn extends CashSuper{
private $moneyCondition;
private $moneyReturn;
public function __construct($moneyCondition,$moneyReturn){
$this->moneyCondition = $moneyCondition;
$this->moneyReturn = $moneyReturn;
}
public function acceptCash($money){
$result = $money;
if($money>=$this->moneyCondition){
$result = $money - floor($money/$this->moneyCondition)*$this->moneyReturn;
}
return $result;
}
}
class CashContext{
public $cs = null;
public function __construct($type){
switch($type){
case "正常收费":
$cs0 = new CashNormal();
$this->cs = $cs0;
break;
case "满300-100":
$cr1 = new CashReturn("300","100");
$this->cs = $cr1;
break;
case "打8折":
$cr2 = new CashRebate("8");
$this->cs = $cr2;
break;
}
}
public function getResult($money){
return $this->cs->acceptCash($money);
}
}
$a = new CashContext("正常收费");
echo $a->getResult(700);