【静的メンバ】STEP: 5 デフォルト引数 (paizaランク B 相当) 解答例 – PHP編【クラス・構造体メニュー】
【クラス・構造体メニュー】 > 【静的メンバ】STEP: 5 デフォルト引数 (paizaランク B 相当)
※リンク先へ移動する為には「paiza」へのログインが必要です。
解答例
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<?php class minors{ public $old; public $order; public $price; public $flag; public function __construct($old){ $this->old = $old; $this->price = 0; $this->flag = "false"; } public function pricesum($order , $price){ if($order == "softdrink" OR $order == "food"){ $this->price += $price; } } } class adult extends minors{ public function pricesum($order , $price){ if($order == "alcohol" AND $this->flag == "false"){ $this->price += $price; $this->flag = "true"; } elseif ($order == "food" AND $this->flag == "true"){ $this->price += ($price - 200); } else { $this->price += $price; } } } $input = explode(" ",trim(fgets(STDIN))); $n = $input[0]; $ordernum = $input[1]; $array = array(); for($i = 1;$i <= $n;$i++){ $age = trim(fgets(STDIN)); if($age < 20){ $array[$i] = new minors($age); } else { $array[$i] = new adult($age); } } for($i = 0;$i < $ordernum;$i++){ $test = explode(" ",trim(fgets(STDIN))); $num = $test[0]; if($test[1] == 0){ $order = "alcohol"; } else { $order = $test[1]; } if($test[1] == 0){ $price = 500; } else { $price = $test[2]; } $array[$num]->pricesum($order , $price); } //print_r($array); foreach($array as $value){ echo $value->price."\n"; } ?> |
