Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- final class FlowChart
- {
- public const CAR_A = 1;
- public const CAR_B = 2;
- public const CAR_C = 3;
- public const WALK_RATE_A = 1;
- public const WALK_RATE_B = 2;
- public const WALK_RATE_C = 3;
- public const EXPANSION_RATE_A = 1;
- public const EXPANSION_RATE_B = 2;
- public const SPEED_A = 1;
- public const SPEED_B = 2;
- public const HUNGRY_A = 1;
- public const HUNGRY_B = 2;
- public const SMART_A = 1;
- public const SMART_B = 2;
- /**
- * @param $class
- * @param $expansionRate
- * @param $smart
- * @param $hungry
- * @param $hasCars
- * @param $speed
- * @param $walkTime
- * @return int
- */
- public function flowChart($class, $expansionRate, $smart, $hungry, $hasCars, $speed, $walkTime): ?int
- {
- $expansionRate = $this->expansionRateRule($expansionRate);
- $smart = $this->smartRule($smart);
- $hungry = $this->hungryRule($hungry);
- $hasCars = $this->hasCarsRule($hasCars);
- $walkTime = $this->walkRule($walkTime);
- switch ($class) {
- case 'A':
- return 1;
- case 'B':
- return 2;
- case 'C':
- return $this->checkRules(
- 3,
- $this->smartRule($smart),
- $this->hungryRule($hungry)
- );
- case 'D':
- return $this->checkRules(
- 6,
- $this->smartRule($smart),
- $this->hungryRule($hungry)
- );
- case 'E':
- return $this->checkRules(
- 9,
- $this->smartRule($smart),
- $this->hungryRule($hungry),
- $this->hasCarsRule($hasCars)
- );
- case 'F':
- return $this->checkRules(
- 13,
- $this->smartRule($smart),
- $this->hungryRule($hungry),
- $this->hasCarsRule($hasCars),
- $this->speedRule($speed),
- $this->walkRule($walkTime),
- true,
- $this->expansionRateRule($expansionRate)
- );
- case 'G':
- return $this->checkRules(
- 25,
- $this->smartRule($smart),
- $this->hungryRule($hungry),
- $this->hasCarsRule($hasCars),
- $this->speedRule($speed),
- $this->walkRule($walkTime)
- );
- }
- return null;
- }
- /**
- * @param $smart
- * @return int
- */
- private function smartRule($smart): int
- {
- if ($smart === 'Yes') {
- return self::SMART_A;
- }
- return self::SMART_B;
- }
- /**
- * @param $hungry
- * @return int
- */
- private function hungryRule($hungry): int
- {
- if ($hungry === 'Yes') {
- return self::HUNGRY_A;
- }
- return self::HUNGRY_B;
- }
- /**
- * @param $hasCars
- * @return int
- */
- private function hasCarsRule($hasCars): int
- {
- if ($hasCars > 1) {
- return self::CAR_A;
- }
- if ($hasCars === 1) {
- return self::CAR_B;
- }
- return self::CAR_C;
- }
- /**
- * @param $expansion
- * @return int
- */
- private function expansionRateRule($expansion): int
- {
- if ($expansion < 200) {
- return self::EXPANSION_RATE_A;
- }
- return self::EXPANSION_RATE_B;
- }
- /**
- * @param $speed
- * @return int
- */
- private function speedRule($speed): int
- {
- if ($speed <= 105) {
- return self::SPEED_A;
- }
- return self::SPEED_B;
- }
- /**
- * @param $walk
- * @return int
- */
- private function walkRule($walk): int
- {
- if ($walk < 100) {
- return self::WALK_RATE_A;
- }
- if ($walk > 100 && $walk < 106) {
- return self::WALK_RATE_B;
- }
- return self::WALK_RATE_C;
- }
- /**
- * @param $input
- * @param bool $smart
- * @param bool $hungry
- * @param bool $hasCars
- * @param bool $speed
- * @param bool $walk
- * @param bool $modifiedWalk
- * @param bool $expansionRate
- * @return int
- */
- public function checkRules(
- $input,
- $smart = false,
- $hungry = false,
- $hasCars = false,
- $speed = false,
- $walk = false,
- $modifiedWalk = false,
- $expansionRate = false
- ): int
- {
- if ($expansionRate) {
- switch ($expansionRate) {
- case self::EXPANSION_RATE_A:
- break;
- case self::EXPANSION_RATE_B:
- $input += 5;
- }
- }
- if ($smart) {
- switch ($smart) {
- case self::SMART_A:
- break;
- case self::SMART_B:
- ++$input;
- }
- }
- if ($hungry) {
- switch ($hungry) {
- case self::HUNGRY_A:
- break;
- case self::HUNGRY_B:
- ++$input;
- }
- }
- if ($hasCars && $speed && $modifiedWalk) {
- switch ($hasCars) {
- case self::CAR_A:
- break;
- case self::CAR_B:
- ++$input;
- break;
- case self::CAR_C:
- ++$input;
- }
- switch ($walk) {
- case self::WALK_RATE_A:
- break;
- case self::WALK_RATE_B:
- ++$input;
- break;
- case self::WALK_RATE_C:
- $input += 2;
- }
- } elseif ($hasCars && $speed) {
- switch ($hasCars) {
- case self::CAR_A:
- break;
- case self::CAR_B:
- ++$input;
- break;
- case self::CAR_C:
- $input += 2;
- }
- switch ($speed) {
- case self::SPEED_A:
- break;
- case self::SPEED_B:
- ++$input;
- }
- switch ($walk) {
- case self::WALK_RATE_A:
- break;
- case self::WALK_RATE_C:
- ++$input;
- }
- } elseif ($hasCars) {
- switch ($hasCars) {
- case self::CAR_B:
- break;
- case self::CAR_C:
- ++$input;
- }
- }
- return $input;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement