搜尋

首頁  >  問答  >  主體

這題怎麼做,求解?!!~~~

<?php

/*

一.依照給定選單(menu)和訂單(order),計算訂單價格總和

*/


$menu = '[

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

{"food_id":1,"name":"魚香肉絲","price":"10"},

                                            {"food_id":2,"name":"紅燒肉"," price":"11"},

                                                                             ]},

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

                 _ood        4,"name":"小炒肉","price" :"13"},

                                                                             ]},

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

                 name":"雪蛋糕","price":"15"} ,

                                                                             ]}      

   

/*

*/


#//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方法被呼叫的次數

*/



#
██████2303 天前1611

全部回覆(3)我來回復

  • biubiubiu

    biubiubiu2018-09-18 14:53:18

    class Menu{
    private $menu;//選單
    private $discountNum=10;//折扣
    static $calculateCount=0;//呼叫次數


    公共函數__construct($menu){
    $this->menu=json_decode($menu,true);
    }


    公共函數計算($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.'
    ';
    echo '折扣價為:'.$totalPrice*($this->discountNum/10).'< ;br/>' ;
    }


    #公用函數discount($discountNum){
    return $this->discountNum=$discountNum;
    }


    public static function counter(){
    #echo 'calculate方法的呼叫次數為:'.self::$calculateCount.'
    ';
    #}


    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);
    Menu::counter();


    #

    回覆
    1
  • 秋香姐家的小书童

    秋香姐家的小书童2018-09-10 15:01:28

    把JSON轉成陣列 然後 遍歷循環 陣列 相加就行了

    回覆
    0
  • jesse

    jesse2018-09-08 12:11:50

    json和陣列轉化,並OOP基礎知識的一併考察,如果覺得無從下手去中文網看看相關影片。

    回覆
    0
  • 取消回覆