Skip to content

Instantly share code, notes, and snippets.

@qst-exe
Created July 13, 2021 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qst-exe/a68f1e4753d3c9f9d98e6301de9572a4 to your computer and use it in GitHub Desktop.
Save qst-exe/a68f1e4753d3c9f9d98e6301de9572a4 to your computer and use it in GitHub Desktop.
<?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