Macam mana nak selesaikan masalah ni?!!~~~
███
███ 2018-09-07 23:41:15
0
3
1600

& lt ;? php

/*

1. ,"nama":"大菜","makanan":[

                                                                                                                                                                                        {"food_id ":2,"name":"Daging babi rebus","harga":"11"},

                                                                   

]}, "{ " type_id ": 2," name ":" Masakan Cina "," Makanan ": [

{" food_id ": 4," name ":" daging goreng kecil "," harga ":" 13"},

"," Makanan ": [

{" food_id ": 7," name ":" cucumber "," price ":" 16 "}

// kuantiti pekali num

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


/*

2. Reka bentuk Menu kelas untuk melaksanakan fungsi berikut:

1. Set menu, setiap contoh mesti mempunyai satu dan hanya satu Menu (rentetan json, struktur seperti di atas)

2 Kaedah mengira, selepas memasukkan pesanan (rentetan json, struktur seperti di atas), keluarkan harga

3, anda boleh menetapkan diskaun, dan mengeluarkan harga secara automatik Kira diskaun

4. Kaunter kaedah statik. Keluarkan bilangan kali kaedah pengiraan dipanggil

*/



███
███

membalas semua(3)
biubiubiu

Menu kelas{
$menu;//Menu
pribadi $discountNum=10;//Diskaun
statik $calculateCount=0;//Bilangan panggilan


fungsi awam __konstruk($menu){
$this-&gt ; menu=json_decode($menu,true);
}


public function count($order){
self::$calculateCount++;
$totalPrice=0;
$order=json_decode($order,true);
foreach ($pesanan sebagai $kunci => $value) {
$foodPrice=$this->getFoodPrice($value['food_id']);
$num=$value['num'];
$totalPrice+= ( $foodPrice*$num);
}
echo 'Jumlah harga asal ialah:'.$totalPrice.'<br/>';
echo 'Harga diskaun ialah:'.$totalPrice*($this- >discountNum /10).'<br/>';
}


diskaun fungsi awam($discountNum){
return $this->discountNum=$discountNum;
}


fungsi statik kaunter (){
echo 'bilangan kaedah pengiraan panggilan ialah:'.self::$calculateCount.'<br/>';
}


fungsi awam getFoodPrice($foodId){
foreach ($this-&gt ;menu sebagai $key => $value) {
foreach ($value['food'] as $key => $value) {
if($foodId==$value['food_id']){
return $value [ 'harga'];
}
}
}
}


}


$menu='[
{"type_id":1,"name":"大菜","food":[
{" food_id ":1,"name":"Babi Dicincang dengan Perisa Ikan","price":"10"},
{"food_id":2,"name":"Babi Rebus","price":"11" },
{"food_id":3,"name":"Mee Pedas","price":"12"}
]},
{"type_id":2,"name":"Masakan Cina","makanan " :[
{"food_id":4,"name":"Babi Goreng","price":"13"},
{"food_id":5,"name":"Wonton","price":" 14"}
]},
{"type_id":3,"name":"lauk-lauk","makanan":[
{"food_id":6,"name":"aiskrim","harga": "15 "},
{"food_id":7,"name":"timun","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 baharu($menu);
$menu1->diskaun(8.7);
$menu1->kira($order1);
$menu1->kira($order2);
$menu1-&gt ;calculate($order3);
Menu::counter();


秋香姐家的小书童

Tukar JSON kepada tatasusunan dan kemudian gelung melalui tatasusunan dan tambahkannya bersama

jesse

Penukaran Json dan tatasusunan, dan pengetahuan asas OOP akan diperiksa bersama-sama Jika anda rasa anda tidak tahu di mana untuk bermula, pergi ke tapak web Cina dan tonton video yang berkaitan.

Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan