0

PHP でスクリプトを書きたいのですが、それでちょっとした計算ができます。

次のタスクを完了する必要があります。

フルーツ ジュースのオンライン ショップを運営しているとします。あなたはしたい

  1. いくつかのジュースをデータベースに保存します (オレンジ ジュース、レモン ジュース、リンゴ ジュースなど)。

  2. また、これらのジュースを分量と価格で節約したいと考えています (例: 100ml / 2,50 USD)。

  3. さまざまなジュースを混合/組み合わせて、ml/USD の合計量を取得したいと考えています。

これを合計すると:

オレンジジュース: 100ml / 2,50 USD | 私は 50ml = 1,25 USD を取ります。

アップルジュース: 100ml / 1,50 USD | 私は 10ml = 0.15 USD を飲みます。

合計ジュース ミックス: 60 ml = 1,40 米ドル。

ここまでは順調ですね。私は次のコードを書きました:

<?php
  function devide() {
  $portion = 100;         //variable for ml    
  $price = 2.50;          //variable for price
  $portionNew = 50;       //value for new portion put into total mix
    if($portionNew > 0) {
      echo ($price / 100 * $portionNew);  //calculates price of new portion
    } 
  }
      echo "Total Juice" . (" = ");  
      echo devide() . (" USD") ;
?>

このコードは、1 つのジュースから取り出した部分の価格のみを計算します。100mlのジュースから30mlをとれば値段に反響します。

これらは私の質問です:

  1. このスクリプトで、新しい価格だけでなく、新しい部分もエコーするようにするにはどうすればよいですか ( $portionNew)?

  2. 最終的なスクリプトで実行したいことに関して、関数は最初から正しいものですか? または、代わりにオブジェクトまたは配列を検討する必要がありますか?

  3. $portionNews/ $price複数の ( ) を追加して、最終的な価格とジュース ミックス全体の割合を教えてくれるようにするには、このスクリプトをどのように進めればよいでしょうか?

4

1 に答える 1

0
  1. echo 'Portion: '.$portionNew関数に次のようなものを追加するだけdevideです。(おそらくあなたはそれを呼びたいと思いますdivideか?)

  2. クラスを作成することもできますがJuice、この場合はやり過ぎだと思います。私はおそらく、ジュースとその価格を格納する配列と、複数のジュースを混合する関数に固執するでしょう。

  3. ジュースを混ぜる関数を作成できます。ミックスの現在の価格と部分、追加するジュースの種類と量を含む変数を指定すると、ミックスのジュースの新しい価格と量を表す値が返されます。

ジュースを配列に保存する場合は、次のようになります。

// In this case, we have an array that maps the juice type to a price (in cents
// per 100 ml).
$juices = array(
    'orange' => 250,
    'apple' => 150
);

// If you decide you need to store more information about a juice, you can change
// this array to map a juice type to another associative array that contains the
// price and your additional information.
// Beware: if you use this, you have to change the mixJuices function to access
// a juice's "price" value:
//     $pricePerLiter = $juices[$juiceType]['price'] * 10;
$juices = array(
    'orange' => array(
        'price' => 250,
        'color' => 'orange'
    ),
    'apple' => array(
        'price' => 150,
        'color' => 'yellow'
    )
);

ドルではなくセントで価格を設定していることに注意してください。こうすることで、価格を計算する際に浮動小数点数とそれに関連するすべての問題を使用しないようにしています ( PHP ドキュメントの浮動小数点精度に関する赤い通知を参照してください) 。

ジュースを混ぜる関数は次のようになります。

// The function needs to know what's currently in the mix, which is why it's
// taking the $currentMix as a parameter. Also, it needs to know about all the
// available juices, which is why it's taking the $juices as another parameter.
// A mix is represented by an associative array that contains the current amount
// of juice in the mix (identified by "portion") and the current price
// (identified by "price"). Note that you need to pass the $juices array I
// defined earlier, so it can access the different types of juices and their 
// prices.
function mixJuices($currentMix, $juices, $juiceType, $juicePortion)
{
    $pricePerLiter = $juices[$juiceType] * 10;
    $price = $pricePerLiter * $juicePortion / 1000;
    $currentMix['portion'] = $currentMix['portion'] + $juicePortion;
    $currentMix['price'] = $currentMix['price'] + $price;
    return $currentMix;
}

この関数は、グローバル$mix変数を使用するように作成することもできます。そうすれば、$currentMix を関数に渡す必要はありません。ただし、同時に複数のミックスを作成する機能も失われます (その変更されたmixJuices関数へのすべての呼び出しが、同じグローバル ミックスを変更するため)。

上記で定義した関数を次のように使用できます。

// At the beginning, there's no juice at all, so it doesn't cost anything (yet)
$mix = array('portion' => 0, 'price' => 0);

// Let's mix some juice in
$mix = mixJuices($mix, $juices, 'apple', 500);
$mix = mixJuices($mix, $juices, 'orange', 250);

// Print the amount of juice and the price
// The price is divided by 100 so we can display it in dollars instead of cents
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mix['portion'], $mix['price'] / 100);

このアプローチにより、あらゆる種類の興味深いことが可能になります。

ジュース ミックスを注文したい 2 人の顧客がいるとします。$mix2 つの異なる配列を作成することで、同じ種類の複数のジュースを簡単に混ぜることができます。

// Process customer 1's order
$mixCustomer1 = array('portion' => 0, 'price' => 0);
$mixCustomer1 = mixJuices($mixCustomer1, $juices, 'apple', 250);
$mixCustomer1 = mixJuices($mixCustomer1, $juices, 'orange', 250);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer1['portion'], $mixCustomer1['price'] / 100);
// 500 milliliters of this juice mix will cost 10.00 USD.

// Process customer 2's order
$mixCustomer2 = array('portion' => 0, 'price' => 0);
$mixCustomer2 = mixJuices($mixCustomer2, $juices, 'apple', 150);
$mixCustomer2 = mixJuices($mixCustomer2, $juices, 'orange', 350);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer2['portion'], $mixCustomer2['price'] / 100);
// 500 milliliters of this juice mix will cost 11.00 USD.

ここで、顧客 2 が、ジュースが 20% オフになる何らかのバウチャーを持っているとします。

// Apply the discount
$discountedJuices = array_map(function($price) { return (1 - 0.2) * $price; }, $juices);

// Process customer 2's order
$mixCustomer2 = array('portion' => 0, 'price' => 0);
$mixCustomer2 = mixJuices($mixCustomer2, $discountedJuices, 'apple', 150);
$mixCustomer2 = mixJuices($mixCustomer2, $discountedJuices, 'orange', 350);
echo sprintf('%d milliliters of this juice mix will cost %.2f USD.',
    $mixCustomer2['portion'], $mixCustomer2['price'] / 100);
// 500 milliliters of this juice mix will cost 8.80 USD.
于 2013-08-05T21:25:25.917 に答える