这题怎么做,求解?!!~~~
███
███ 2018-09-07 23:41:15
0
3
1579

<?php

/*

一.按照给定菜单(menu)和订单(order),计算订单价格总和

*/


$menu = '[

            {"type_id":1,"name":"大菜","food":[

                                            {"food_id":1,"name":"鱼香肉丝","price":"10"},

                                            {"food_id":2,"name":"红烧肉","price":"11"},

                                            {"food_id":3,"name":"香辣粉","price":"12"}

                                            ]},

            {"type_id":2,"name":"中菜","food":[

                                            {"food_id":4,"name":"小炒肉","price":"13"},

                                            {"food_id":5,"name":"云吞","price":"14"}

                                            ]},

            {"type_id":3,"name":"小菜","food":[

                                            {"food_id":6,"name":"雪糕","price":"15"},

                                            {"food_id":7,"name":"黄瓜","price":"16"}

                                            ]}      

        ]';


/*

*/


//num系数量

$order = '[{"food_id":1,"num":2},{"food_id":3,"num":1},{"food_id":6,"num":2},{"food_id":7,"num":1}]';


/*

二.设计一个类Menu,实现以下功能:

1. 设定菜单,每个实例必须有且只有一个菜单(json字符串,结构如上题)

2. 方法calculate, 输入订单后(json字符串,结构如上题), 输出格价

3. 方法discount, 可设定折扣,输出格价时自动计算折扣

4. 静态方法counter。输出calculate方法被调用的次数

*/



███
███

全部回复(3)
biubiubiu

class Menu{
private $menu;//菜单
private $discountNum=10;//折扣
static $calculateCount=0;//调用次数


public function __construct($menu){
$this->menu=json_decode($menu,true);
}


public function calculate($order){
self::$calculateCount++;
$totalPrice=0;
$order=json_decode($order,true);
foreach ($order as $key => $value) {
$foodPrice=$this->getFoodPrice($value['food_id']);
$num=$value['num'];
$totalPrice+=($foodPrice*$num);
}
echo '原总价为:'.$totalPrice.'<br/>';
echo '折扣价为:'.$totalPrice*($this->discountNum/10).'<br/>';
}


public function discount($discountNum){
return $this->discountNum=$discountNum;
}


public static function counter(){
echo 'calculate方法的调用次数为:'.self::$calculateCount.'<br/>';
}


public function getFoodPrice($foodId){
foreach ($this->menu as $key => $value) {
foreach ($value['food'] as $key => $value) {
if($foodId==$value['food_id']){
return $value['price'];
}
}
}
}


}


$menu='[
{"type_id":1,"name":"大菜","food":[
{"food_id":1,"name":"鱼香肉丝","price":"10"},
{"food_id":2,"name":"红烧肉","price":"11"},
{"food_id":3,"name":"香辣粉","price":"12"}
]},
{"type_id":2,"name":"中菜","food":[
{"food_id":4,"name":"小炒肉","price":"13"},
{"food_id":5,"name":"云吞","price":"14"}
]},
{"type_id":3,"name":"小菜","food":[
{"food_id":6,"name":"雪糕","price":"15"},
{"food_id":7,"name":"黄瓜","price":"16"}
]}
]';


$order1='[
{"food_id":1,"num":2},
{"food_id":3,"num":1},
{"food_id":6,"num":2},
{"food_id":7,"num":1}
]';
$order2='[
{"food_id":1,"num":2},
{"food_id":4,"num":1},
{"food_id":6,"num":7},
{"food_id":5,"num":6}
]';
$order3='[
{"food_id":1,"num":2},
{"food_id":2,"num":17},
{"food_id":4,"num":24},
{"food_id":7,"num":11}
]';


$menu1=新菜单($menu);
$menu1->折扣(8.7);
$menu1->计算($order1);
$menu1->计算($order2);
$menu1-> ;计算($order3);
菜单::计数器();


秋香姐家的小书童

把JSON转成数组 然后 遍历循环 数组 相加就行了

jesse

json和数组转化,并OOP基础知识的一并考察,如果觉得无从下手去中文网看看相关视频。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!