只需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);
这种方法允许你做各种有趣的事情。
假设您有两个客户想要订购果汁混合物。$mix
您可以通过创建两个不同的数组轻松地混合同一类型的多个果汁:
// 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.