Rumah > Soal Jawab > teks badan
& lt ;? php
/*
1. ,"nama":"大菜","makanan":[
{"food_id ":2,"name":"Daging babi rebus","harga":"11"},
$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
*/
biubiubiu2018-09-18 14:53:18
Menu kelas{
$menu;//Menu
pribadi $discountNum=10;//Diskaun
statik $calculateCount=0;//Bilangan panggilan
fungsi awam __konstruk($menu){
$this-> ; 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-> ;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-> ;calculate($order3);
Menu::counter();
秋香姐家的小书童2018-09-10 15:01:28
Tukar JSON kepada tatasusunan dan kemudian gelung melalui tatasusunan dan tambahkannya bersama
jesse2018-09-08 12:11:50
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.