How to solve this problem?!!~~~
███
███ 2018-09-07 23:41:15
0
3
1607

<?php

/*

1. According to the given menu (menu) and order (order), calculate the total order price

*/


$menu = '[

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

{"food_id":1,"name":"Shredded Pork with Fish Flavor","price":"10"},

                                                                                                                      price":"11"},

                                                                               ]},

                                                                                                                                                       ; ]},

                                                                                                                                                                                  . ]}

]';

/*

*/

//num coefficient number

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


/*

2. Design a class Menu to implement the following functions:

1. Set menu, each instance must have and only one menu (json string, structure as above)

2. Method calculate, after entering the order (json string, structure As in the above question), output the price

3. Discount method, you can set the discount, and automatically calculate the discount when outputting the price

4. Static method counter. Output the number of times the calculate method is called

*/



███
███

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


秋香姐家的小书童

Convert JSON to an array and then loop through the arrays and add them up

jesse

json and array conversion, and examine the basic knowledge of OOP together. If you feel that you have no idea where to start, go to the Chinese website and watch the relevant videos.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template