php价格结算,PHP根据用户折扣计算商品价格

步骤01

创建User.php界面,在该界面中首先申明一个User接口,用户都是对该接口实现的

interface User{

function getName();

function setName($name);

function getDiscount();

}

abstract class AbstractUser implements User{

private $name="";

protected $discount=0;

protected $grade="";

public function __construct($name){

$this->setName($name);

}

public function getName(){

return $this->name;

}

public function getDiscount(){

return $this->discount;

}

public function setName($name){

$this->name=$name;

}

public function getGrade(){

return $this->grade;

}

}

/**

* 普通用户

*/

class NormalUser extends AbstractUser

{

protected $discount=1.0;

protected $grade="NormalUser";

}

/**

* VIP用户

*/

class VIPlUser extends AbstractUser

{

protected $discount=0.8;

protected $grade="VIPUser";

}

/**

* 内部用户

*/

class InnerlUser extends AbstractUser

{

protected $discount=0.7;

protected $grade="InnerlUser";

}

?>

步骤02

对商品进行设计,首先申明一个Product接口;然后Book接口继承该接口;最后创建BookOnline类实现Book接口

/*

*定义产品接口

*/

Interface Product{

public function getProductName();

public function getProductPrice();

}

interface Book extends Product{

public function getAuthor();

}

/**

*

*/

class BookOnline implements Book

{

private $productName;

private $producePrice;

private $author;

function __construct($bookName)

{

$this->productName=$bookName;

}

public function getProductName(){

return $this->productName;

}

public function getProductPrice(){

$this->productPrice=100;

return $this->productPrice;

}

public function getAuthor(){

return $this->author;

}

}

?>

步骤03

创建ProductSettle类,他是一个独立的计算类,使用静态方法进行商品计算

include_once 'User.php';

include_once 'Product.php';

class ProductSettle{

public static function finalPrice(User $_user, Product $_product, $number=1){

$price = $_user->getDiscount()*$_product->getProductPrice()*$number;

return $price;

}

}

?>

步骤04

创建用户测试界面Test.php页面

include_once 'User.php';

include_once 'Product.php';

include_once 'ProductSettle.php';

$number=10;

$book = new BookOnline("PHP+MySQL");

$user = new NormalUser("Tom");

$price = ProductSettle::finalPrice($user,$book,$number);

$str="您好!尊敬的用户 ".$user->getName()."";

$str.="您的级别是".$user->getGrade()."";

$str.="您的折扣是".$user->getDiscount()."";

$str.="购买$number 本《".$book->getProductName()."》总价格是 ".$price."";

echo $str;

?>结果图

bdf6da618c89a3cfd39f74d06669b770.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值