Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions app/Http/Controllers/Exercise02Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Carbon;

class Exercise02Controller extends Controller
{
const NORMAL_FREE = 110;
const FREE = 0;
const TIME_PERIOD = ['08:45', '17:59'];
//example default holiday
const HOLIDAYS = ['01-01', '10-03', '02-09'];

/**
* charge ATM
*
* @param bool $isVip
* @param string $date
* @param string $time
* @return int
*/
public function chargeATM($isVip = true, $date = '2021-05-01', $time = '14:25'): int
{
//rule1
if ($isVip) {
return self::FREE;
}
//rule2
if($this->isWeekend($date) || $this->isHoliday($date)) {
return self::NORMAL_FREE;
}
//rule3
if ($this->isTimeDiscount($date, $time)) {
return self::FREE;
}
//rule4
return self::NORMAL_FREE;
}

/**
* Check is weekend
*
* @param $date
* @return bool
*/
public function isWeekend($date): bool
{
if (Carbon::createFromFormat('Y-m-d', $date)->isWeekend()) {
return true;
}

return false;
}

/**
* Check is holiday
*
* @param $date
* @return bool
*/
public function isHoliday($date): bool
{
$date = Carbon::createFromFormat('Y-m-d', $date);
if (in_array($date->format('d-m'), self::HOLIDAYS)) {
return true;
}

return false;
}

/**
* Check is time discount
*
* @param $date
* @param $time
* @return bool
*/
public function isTimeDiscount($date, $time): bool
{
$date = Carbon::createFromFormat('Y-m-d H:i', $date . " " . $time);
$time = $date->format('H:i');
list($minTime, $maxTime) = self::TIME_PERIOD;

if ($time >= $minTime && $time <= $maxTime) {
return true;
}

return false;
}
}
46 changes: 46 additions & 0 deletions app/Http/Controllers/Exercise03Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Controllers;

class Exercise03Controller extends Controller
{
const CRAVAT_TYPE = 1;
const WHITE_SHIRT_TYPE = 2;
const DISCOUNT_TYPE_ONE = 12;
const DISCOUNT_TYPE_TWO = 7;
const DISCOUNT_TYPE_THREE = 5;
const NO_DISCOUNT = 0;
const TOTAL_PRODUCT_TO_DISCOUNT = 7;

/**
* Calculate discount by products
*
* @param $products
* @return int
*/
public function calculateDiscount($products)
{
$products = collect($products);
$totalProduct = $products->count();

if ($totalProduct) {
$hasCravat = $products->filter(function ($product) {
return $product['type'] == self::CRAVAT_TYPE;
})->isNotEmpty();

$hasWhiteShirt = $products->filter(function ($product) {
return $product['type'] === self::WHITE_SHIRT_TYPE;
})->isNotEmpty();

if ($totalProduct >= self::TOTAL_PRODUCT_TO_DISCOUNT) {
if ($hasCravat && $hasWhiteShirt) return self::DISCOUNT_TYPE_ONE;
if (!$hasCravat || !$hasWhiteShirt) return self::DISCOUNT_TYPE_TWO;
} else {
if ($hasCravat && $hasWhiteShirt) return self::DISCOUNT_TYPE_THREE;
if (!$hasCravat && !$hasWhiteShirt) return self::NO_DISCOUNT;
}
}

return self::NO_DISCOUNT;
}
}
46 changes: 46 additions & 0 deletions app/Http/Controllers/Exercise08Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Support\Carbon;

class Exercise08Controller extends Controller
{
const FEMALE = 'female';

/**
* Calculate price
*
* @param $age
* @param string $date
* @param $gender
* @return int|string
*/
public function calculatePrice($age, $date = '2021-05-01', $gender)
{
$date = Carbon::createFromFormat('Y-m-d', $date);
$dayOfWeek = $date->dayOfWeek;

if ($age < 0 || $age > 120) {
return "Error";
}

if ($age < 13) {
return 900;
}

if ($dayOfWeek === Carbon::TUESDAY) {
return 1200;
}

if ($dayOfWeek === Carbon::FRIDAY && $gender === self::FEMALE) {
return 1400;
}

if ($age > 65) {
return 1600;
}

return 1800;
}
}
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
<testsuite name="Exercise10">
<directory suffix="Test.php">./Modules/Exercise10/Tests</directory>
</testsuite>
<testsuite name="CommonUnit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
Expand Down
53 changes: 53 additions & 0 deletions tests/Unit/Controllers/Exercise02ControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Tests\Unit\Controllers;

use Tests\TestCase;
use \App\Http\Controllers;

class Exercise02ControllerTest extends TestCase
{
protected $exercise02Controller;

/**
* @throws \Exception
*/
public function setUp():void
{
parent::setUp();

$this->exercise02Controller = new Controllers\Exercise02Controller();
}

// functions test with rule in decision table

public function test_charge_ATM_with_rule_1()
{
$result = $this->exercise02Controller->chargeATM(true);
$this->assertEquals(0, $result);
}

public function test_charge_ATM_with_rule_2()
{
$result = $this->exercise02Controller->chargeATM(false, '2021-05-01');
$this->assertEquals(110, $result);
}

public function test_charge_ATM_with_rule_3()
{
$result = $this->exercise02Controller->chargeATM(false, '2021-04-28', '14:25');
$this->assertEquals(0, $result);
}

public function test_charge_ATM_with_rule_4()
{
$result = $this->exercise02Controller->chargeATM(false, '2021-04-28', '07:00');
$this->assertEquals(110, $result);
}

public function test_is_holiday()
{
$result = $this->exercise02Controller->isHoliday('2021-05-01');
$this->assertEquals(true, $result);
}
}
Loading