echo 'Portion: '.$portionNew
関数に次のようなものを追加するだけdevide
です。(おそらくあなたはそれを呼びたいと思いますdivide
か?)
クラスを作成することもできますがJuice
、この場合はやり過ぎだと思います。私はおそらく、ジュースとその価格を格納する配列と、複数のジュースを混合する関数に固執するでしょう。
ジュースを混ぜる関数を作成できます。ミックスの現在の価格と部分、追加するジュースの種類と量を含む変数を指定すると、ミックスのジュースの新しい価格と量を表す値が返されます。
ジュースを配列に保存する場合は、次のようになります。
// 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 人の顧客がいるとします。$mix
2 つの異なる配列を作成することで、同じ種類の複数のジュースを簡単に混ぜることができます。
// 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.