Created
April 1, 2018 06:17
-
-
Save ounziw/4ae24d7b424e0263475621ec682b075f to your computer and use it in GitHub Desktop.
nagoya php 12 #nagoyaphp
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 | |
/** | |
* Class Bus | |
* | |
* バス代と、人数から、料金を計算する | |
* | |
* メインの処理は、 | |
* 乗客種別ごとの料金 | |
* 無料になる乗客の処理 | |
* 計算 | |
* | |
*/ | |
class Bus { | |
protected $base_fee = 0 ; | |
protected $passenger_type = array('An','Ap','Aw','Cn','Cp','Cw','In','Ip','Iw') ; | |
protected $passengers; | |
protected $paid_passengers; | |
protected $passengers_fee; | |
protected $total_fee = 0; | |
public function __construct() { | |
foreach($this->passenger_type as $key) { | |
$this->passengers[$key] = 0; | |
$this->paid_passengers[$key] = 0; | |
$this->passengers[$fee] = 0; | |
} | |
} | |
public function setFee($fee) { | |
// 10円単位かどうかのチェックは省略 | |
$this->base_fee = intval($fee); | |
} | |
public function setPassengers(array $passengers) { | |
foreach($this->passenger_type as $key) { | |
if (array_key_exists($key, $passengers)) { | |
$this->passengers[$key] = $passengers[$key]; | |
} | |
} | |
} | |
// メインの計算ロジック | |
protected function cal() { | |
$this->setPassengersFee(); | |
$this->setPaidPassengers(); | |
$this->calTotalFee(); | |
} | |
protected function setPaidPassengers() { | |
$this->paid_passengers = $this->passengers; | |
// 大人1人につき2人まで幼児を無料にする | |
$free_infant = 2 * ($this->passengers['An'] + $this->passengers['Ap'] + $this->passengers['Aw']); | |
$this->paid_passengers['In'] = max(0, $this->passengers['In'] - $free_infant); | |
// 幼児を無料にした後、まだ無料枠があれば、幼児福祉を無料にする | |
$free_infant_available = max(0, $free_infant - $this->passengers['In']); | |
$this->paid_passengers['Iw'] = max(0, $this->passengers['Iw'] - $free_infant_available); | |
} | |
protected function setPassengersFee() { | |
$this->passengers_fee['An'] = $this->base_fee; | |
$this->passengers_fee['Ap'] = 0; | |
$this->passengers_fee['Aw'] = $this->getHalfFee($this->base_fee); | |
$this->passengers_fee['Cn'] = $this->getHalfFee($this->base_fee); | |
$this->passengers_fee['Cp'] = 0; | |
$this->passengers_fee['Cw'] = $this->getHalfFee($this->passengers_fee['Cn']); | |
$this->passengers_fee['In'] = $this->getHalfFee($this->base_fee); | |
$this->passengers_fee['Ip'] = 0; | |
$this->passengers_fee['Iw'] = $this->getHalfFee($this->passengers_fee['In']); | |
} | |
protected function getHalfFee($val) { | |
$half = $val / 2 ; | |
$half_ceil = ceil($half / 10) * 10; | |
return $half_ceil; | |
} | |
protected function calTotalFee() { | |
// 乗客の種類ごとに、料金 x 人数 | |
foreach($this->passenger_type as $key) { | |
$this->total_fee += $this->passengers_fee[$key] * $this->paid_passengers[$key]; | |
} | |
} | |
public function getTotalFee() { | |
$this->cal(); | |
return $this->total_fee; | |
} | |
} |
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 | |
include('bus.php'); | |
/** | |
* nagoya php 12 のプログラミング課題 | |
* | |
* 問題は | |
* http://nabetani.sakura.ne.jp/hena/ord9busfare/ | |
* | |
* 方針 | |
* 料金はバスが決めることなので、Bus クラスで処理 | |
* 乗車人数を管理する Passengers クラスで、まず人数を集計する | |
* | |
* | |
*/ | |
class Passengers { | |
protected $members = array(); | |
public function addMember($member) { | |
$this->members[$member] += 1; | |
} | |
public function getMembers() { | |
return $this->members; | |
} | |
} | |
// 入出力管理用 | |
function cal($input) { | |
$data = explode(':', $input); | |
$memberlist = explode(',',$data[1]); | |
$passengers = new Passengers(); | |
foreach ($memberlist as $member) { | |
$passengers->addMember($member); | |
} | |
$bus = new Bus; | |
$bus->setFee($data[0]); | |
$bus->setPassengers($passengers->getMembers()); | |
return $bus->getTotalFee(); | |
} | |
function test($input, $expected){ | |
$result = cal($input); | |
if($result == $expected) | |
{ | |
echo "OK"; | |
} else { | |
echo "NG"; | |
} | |
} | |
// テスト実行 | |
/*0*/ test( "210:Cn,In,Iw,Ap,Iw", "170" ); | |
/*1*/ test( "220:Cp,In", "110" ); | |
/*2*/ test( "230:Cw,In,Iw", "240" ); | |
/*3*/ test( "240:In,An,In", "240" ); | |
/*4*/ test( "250:In,In,Aw,In", "260" ); | |
/*5*/ test( "260:In,In,In,In,Ap", "260" ); | |
/*6*/ test( "270:In,An,In,In,Ip", "410" ); | |
/*7*/ test( "280:Aw,In,Iw,In", "210" ); | |
/*8*/ test( "200:An", "200" ); | |
/*9*/ test( "210:Iw", "60" ); | |
/*10*/ test( "220:Ap", "0" ); | |
/*11*/ test( "230:Cp", "0" ); | |
/*12*/ test( "240:Cw", "60" ); | |
/*13*/ test( "250:In", "130" ); | |
/*14*/ test( "260:Cn", "130" ); | |
/*15*/ test( "270:Ip", "0" ); | |
/*16*/ test( "280:Aw", "140" ); | |
/*17*/ test( "1480:In,An,In,In,In,Iw,Cp,Cw,In,Aw,In,In,Iw,Cn,Aw,Iw", "5920" ); | |
/*18*/ test( "630:Aw,Cw,Iw,An,An", "1740" ); | |
/*19*/ test( "340:Cn,Cn,Ip,Ap", "340" ); | |
/*20*/ test( "240:Iw,Ap,In,Iw,Aw", "120" ); | |
/*21*/ test( "800:Cw,An,Cn,Aw,Ap", "1800" ); | |
/*22*/ test( "1210:An,Ip,In,Iw,An,Iw,Iw,An,Iw,Iw", "3630" ); | |
/*23*/ test( "530:An,Cw,Cw", "810" ); | |
/*24*/ test( "170:Aw,Iw,Ip", "90" ); | |
/*25*/ test( "150:In,Ip,Ip,Iw,In,Iw,Iw,In,An,Iw,Aw,Cw,Iw,Cw,An,Cp,Iw", "580" ); | |
/*26*/ test( "420:Cn,Cw,Cp", "320" ); | |
/*27*/ test( "690:Cw,In,An,Cp,Cn,In", "1220" ); | |
/*28*/ test( "590:Iw,Iw,Cn,Iw,Aw,In,In,Ip,Iw,Ip,Aw", "1200" ); | |
/*29*/ test( "790:Cw,Cn,Cn", "1000" ); | |
/*30*/ test( "1220:In,In,An,An,In,Iw,Iw,In,In,Ip,In,An,Iw", "4590" ); | |
/*31*/ test( "570:Cw,Cn,Cp", "440" ); | |
/*32*/ test( "310:Cn,Cw,An,An,Iw,Cp,Cw,Cn,Iw", "1100" ); | |
/*33*/ test( "910:Aw,In,Iw,Iw,Iw,Iw,Iw,An,Cw,In", "2290" ); | |
/*34*/ test( "460:Iw,Cw,Cw,Cn", "590" ); | |
/*35*/ test( "240:Iw,Iw,In,Iw,In,In,Cn,In,An", "780" ); | |
/*36*/ test( "1240:In,In,In,Ap,In,Cw,Iw,Iw,Iw,Aw,Cw", "2170" ); | |
/*37*/ test( "1000:Iw,Ip,In,An,In,In,In,An,In,Iw,In,In,Iw,In,Iw,Iw,Iw,An", "5500" ); | |
/*38*/ test( "180:In,Aw,Ip,Iw,In,Aw,In,Iw,Iw,In", "330" ); | |
/*39*/ test( "440:In,Ip,Cp,Aw,Iw,In,An", "660" ); | |
/*40*/ test( "1270:Ap,In,An,Ip,In,Ip,Ip", "1270" ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment