Created
July 13, 2021 17:16
-
-
Save qst-exe/a68f1e4753d3c9f9d98e6301de9572a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
// 消費税10% | |
const TAX = 0.1; | |
// 商品単体の値段を計算する | |
function calculate(int $price, int $count): float | |
{ | |
return $price*$count*(1+TAX); | |
} | |
// レシートの文字列を返す | |
function showReceipt(array $items): string | |
{ | |
$total_price = 0; | |
foreach ($items as $item) { | |
$total_price += calculate($item["price"], $item["count"]); | |
} | |
$item_name_list = array_column($items, "name"); | |
return "レシート\n".implode("\n", $item_name_list)."\n¥$total_price\n\n"; | |
} | |
$items_a = [ | |
[ | |
"name" => "消しゴム", | |
"count" => 2, | |
"price" => 60, | |
] | |
]; | |
$items_b = [ | |
[ | |
"name" => "鉛筆", | |
"count" => 3, | |
"price" => 60, | |
], | |
[ | |
"name" => "ペン", | |
"count" => 2, | |
"price" => 100, | |
], | |
]; | |
echo showReceipt($items_a); | |
echo showReceipt($items_b); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment