这题怎么做,求解?!!~~~

<?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
回复biubiubiu赞同13天前3楼

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=new Menu($menu);
$menu1->discount(8.7);
$menu1->calculate($order1);
$menu1->calculate($order2);
$menu1->calculate($order3);
Menu::counter();


    秋香姐家的小书童
    回复秋香姐家的小书童赞同01星期前2楼

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

      jesse
      回复jesse赞同01星期前1楼

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

        首页上一页1下一页尾页

        PHP中文网

        未登录