Heim  >  Fragen und Antworten  >  Hauptteil

Wie kann dieses Problem gelöst werden?!!~~~

<?php

/*

1. Berechnen Sie anhand des angegebenen Menüs (Menü) und der Bestellung (Bestellung) den Gesamtbestellpreis

*/


$menu = '[

                                                                                                                      : 1, "Name": "大 菜", "Food": [

{"food_id": 2, "name": "geschmortes Schweinefleisch", "Preis": "11"},

]}, " {" type_id ": 2," name ": "Chinesische Küche", "Food": [

{" food_id ": 4," name ": "kleines gebratenes Fleisch", "price": "13"},

"," Food ": [

{" food_id ": 7," name ":" gurcumber "," preis ":" 16 "}

// Num -Koeffizienten Menge

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


/*

2. Entwerfen Sie ein Klassenmenü, um die folgenden Funktionen zu implementieren:

1. Menü festlegen, jede Instanz darf nur eine haben Menü (JSON-String, Struktur wie oben)

2. Methode berechnen, nach Eingabe der Bestellung (JSON-String, Struktur wie oben) den Preis ausgeben

3. Sie können den Rabatt festlegen und den Preis automatisch ausgeben Rabatt berechnen

4. Statischer Methodenzähler. Geben Sie die Häufigkeit aus, mit der die Berechnungsmethode aufgerufen wird

*/



██████2207 Tage vor1543

Antworte allen(3)Ich werde antworten

  • biubiubiu

    biubiubiu2018-09-18 14:53:18

    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();


    Antwort
    1
  • 秋香姐家的小书童

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

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

    Antwort
    0
  • jesse

    jesse2018-09-08 12:11:50

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

    Antwort
    0
  • StornierenAntwort