Last active
August 29, 2015 14:05
-
-
Save ounziw/2c7f16e9927b869292ba 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 | |
/** | |
* Copyright (c) 2014 Fumito MIZUNO | |
* License: MIT | |
* http://opensource.org/licenses/mit-license.php | |
*/ | |
require_once('yoyakulist.php'); | |
class Main | |
{ | |
protected $data; | |
protected $queuelist = array(); | |
protected $yoyakulist; | |
public function __construct() | |
{ | |
$this->yoyakulist = new YoyakuList(); | |
} | |
/** | |
* メイン処理 | |
* @param $data ex. 1_12345|2_12345 | |
* @return string | |
*/ | |
public function main($data) | |
{ | |
$this->splitdata($data); | |
$this->setupUser(); | |
$this->tryReserve(); | |
return $this->output(); | |
} | |
/** | |
* @param $data | |
*/ | |
public function splitdata($data) | |
{ | |
$this->data = explode('|', $data); | |
} | |
/** | |
* ユーザー情報を登録 | |
* @param $userid ex. 1 | |
* @param $days ex. 12345 | |
*/ | |
protected function addUser($userid, $days) | |
{ | |
if (is_numeric($userid) && is_numeric($days)) | |
{ | |
$this->queuelist[$userid] = $days; | |
} | |
else | |
{ | |
echo 'Input Error.'; | |
} | |
} | |
/** | |
* 入力を分割し、addUserする | |
*/ | |
public function setupUser() | |
{ | |
foreach ($this->data as $val) { | |
$user_days = explode('_', $val); | |
$this->addUser($user_days[0], $user_days[1]); | |
} | |
} | |
/** | |
* 予約を試みる | |
*/ | |
public function tryReserve() | |
{ | |
for ($i=0;$i<5;$i++) // 曜日が5つなので、最大5回 | |
{ | |
foreach ($this->queuelist as $key => $val) | |
{ | |
$day = substr($val,0,1); | |
// 希望が通れば、そのユーザーを削除 | |
// 通らなければ、第一希望を削除 | |
if ($this->yoyakulist->execute($key, $day)) | |
{ | |
unset($this->queuelist[$key]); | |
} | |
else | |
{ | |
$this->queuelist[$key] = substr($val,1); | |
} | |
} | |
} | |
} | |
/** | |
* 出力する | |
* @return string | |
*/ | |
public function output() | |
{ | |
$output = array(); | |
$return = $this->yoyakulist->getYoyakulist(); | |
foreach ($return as $key => $val) | |
{ | |
// 予約のあるもののみ表示 | |
if ($val) { | |
// 並べ替えて表示 | |
asort($val); | |
$output[] = $key . '_' . implode(':', $val); | |
} | |
} | |
return implode('|', $output); | |
} | |
} |
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 | |
/** | |
* Copyright (c) 2014 Fumito MIZUNO | |
* License: MIT | |
* http://opensource.org/licenses/mit-license.php | |
*/ | |
require_once 'main.php'; | |
class mainTest extends PHPUnit_Framework_TestCase | |
{ | |
/** | |
* @dataProvider provider | |
*/ | |
public function test1($input, $expected) | |
{ | |
$class = new Main(); | |
$output = $class->main($input); | |
$this->assertEquals($expected, $output); | |
} | |
public function provider() | |
{ | |
return array( | |
array('1_12345|2_12345','1_1:2'), | |
array('55_24153|91_24531|9_12543|41_34215|72_15423|44_42531|6_42351|79_15243|21_35412|31_52413|74_24135|83_31254|33_35421|84_53421|89_53241|16_32415|36_15234|92_34521|62_12345|14_23415|40_23415|88_43251|52_45213|77_32154|59_53241','1_9:36:72:79|2_14:55:74:91|3_21:33:41:83|4_6:44:52:88|5_31:59:84:89'), | |
array('90_21453|58_43521|69_42351|12_51432|34_14352|23_45321|77_35421|71_24531|95_24153|49_13254|9_45231|78_21534|15_43512|54_41532|52_54321|59_24513|57_41532|75_35214|24_12435|97_52341|39_12453|53_12435|48_51234|32_51234', '1_24:34:39:49|2_71:78:90:95|3_15:54:75:77|4_9:23:58:69|5_12:48:52:97'), | |
array('55_13524|64_41523|40_23451|99_25314|65_21453|69_31524|77_45231|62_51432|21_13542|81_13452|66_21534|33_41352|5_52314|90_31452|6_42135|93_42351|84_43125|54_45213|76_21453|19_52134|50_21435|61_13542|30_35421|43_25431|24_25341|31_15234|39_15243|20_51423|67_13524', '1_21:55:61:81|2_40:65:66:99|3_30:69:84:90|4_6:33:64:77|5_5:19:20:62'), | |
); | |
} | |
} |
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 | |
/** | |
* Copyright (c) 2014 Fumito MIZUNO | |
* License: MIT | |
* http://opensource.org/licenses/mit-license.php | |
*/ | |
class YoyakuList | |
{ | |
protected $yoyakulist; | |
protected $max = 4; | |
/** | |
* 曜日(5個)を用意しておく | |
*/ | |
public function __construct() | |
{ | |
$this->yoyakulist = array( | |
1 => array(), | |
2 => array(), | |
3 => array(), | |
4 => array(), | |
5 => array(), | |
); | |
} | |
/** | |
* 予約を実行 | |
* @param $userid ex. 1 | |
* @param $day ex. 2 | |
* @return bool | |
*/ | |
public function execute($userid, $day) | |
{ | |
$day = (int) $day; | |
$userid = (int) $userid; | |
// 空きがあれば登録する | |
if ($this->check($day)) | |
{ | |
$this->accept($userid, $day); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* 空きがあるかをチェックする | |
* @param $day ex. 2 | |
* @return bool | |
*/ | |
protected function check($day) | |
{ | |
// 4未満なら空きあり | |
if (count($this->yoyakulist[$day]) < $this->max) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
/** | |
* 予約を受け入れる | |
* @param $day ex. 2 | |
* @param $userid ex. 1 | |
*/ | |
protected function accept($userid, $day) | |
{ | |
$this->yoyakulist[$day][] = $userid; | |
} | |
/** | |
* 予約データを返す | |
* @return array | |
*/ | |
public function getYoyakulist() | |
{ | |
return $this->yoyakulist; | |
} | |
} |
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 | |
/** | |
* Copyright (c) 2014 Fumito MIZUNO | |
* License: MIT | |
* http://opensource.org/licenses/mit-license.php | |
*/ | |
require_once 'yoyakulist.php'; | |
class YoyakuTest extends PHPUnit_Framework_TestCase | |
{ | |
// もう少し細かい粒度でテストする? | |
public function testExecute() | |
{ | |
$class = new YoyakuList(); | |
$class->execute(1,1); | |
$class->execute(2,1); | |
$class->execute(3,1); | |
$class->execute(4,1); | |
$class->execute(5,1); | |
$output = $class->getYoyakulist(); | |
$expected = array( | |
1 => array(1,2,3,4), | |
2 => array(), | |
3 => array(), | |
4 => array(), | |
5 => array(), | |
); | |
$this->assertEquals($expected, $output); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment