Skip to content
This repository was archived by the owner on Jun 3, 2019. It is now read-only.
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
14 changes: 14 additions & 0 deletions .idea/php-test-framework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/Bread.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Bread extends Good
{

}
8 changes: 8 additions & 0 deletions src/Cheese.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Cheese extends Good
{

}
9 changes: 0 additions & 9 deletions src/Good.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,4 @@

abstract class Good
{
public static function milk(): self
{
return new Milk;
}

public function isMilk(): bool
{
return false;
}
}
8 changes: 8 additions & 0 deletions src/Grain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Grain extends Good
{

}
34 changes: 28 additions & 6 deletions src/Market.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,38 @@

final class Market
{
public function priceFor(Good $milk): Pound
/** @var PriceListCollection */
private $priceListCollection;

public function __construct(PriceListCollection $priceListCollection)
{
$this->priceListCollection = $priceListCollection;
}

public function priceFor(Good $good): Pound
{
return new Pound(5);
return $this->priceListCollection->currentFor($good);
}

public function sellTo(Offer $offer): Pound
{
return new Pound(
$offer->amount()->amount() *
$this->priceFor($offer->good())->amount()
);
$cost = $this
->priceFor($offer->good())
->mult($offer->amount()->amount());

$this->priceListCollection->moveFor($offer->good(), -$offer->amount()->amount());

return $cost;
}

public function buyFrom(Offer $offer): Pound
{
$cost = $this
->priceFor($offer->good())
->mult($offer->amount()->amount());

$this->priceListCollection->moveFor($offer->good(), $offer->amount()->amount());

return $cost;
}
}
4 changes: 0 additions & 4 deletions src/Milk.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@

final class Milk extends Good
{
public function isMilk(): bool
{
return true;
}
}
5 changes: 5 additions & 0 deletions src/Pound.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@ public function amount(): int
{
return $this->amount;
}

public function mult(int $multiplier): Pound
{
return new Pound($this->amount * $multiplier);
}
}
21 changes: 21 additions & 0 deletions src/PriceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,25 @@ public function current(): Pound
{
return $this->prices[$this->position];
}

public function move(int $positions): Pound
{
$nextPosition = $this->position + $positions;

if ($nextPosition >= count($this->prices)) {
$this->position = count($this->prices) - 1;

return $this->current();
}

if ($nextPosition < 0) {
$this->position = 0;

return $this->current();
}

$this->position = $nextPosition;

return $this->current();
}
}
82 changes: 81 additions & 1 deletion src/PriceListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,87 @@ public function milkPrices(): PriceList
new Pound(7),
new Pound(7),
new Pound(8),
new Pound(8)
new Pound(8),
);
}

public function woolPrices(): PriceList
{
return PriceList::fromList(
new Pound(3),
new Pound(3),
new Pound(4),
new Pound(4),
new Pound(5),
new Pound(5),
new Pound(6),
new Pound(6),
new Pound(7),
new Pound(8),
);
}

public function grainPrices(): PriceList
{
return PriceList::fromList(
new Pound(3),
new Pound(3),
new Pound(4),
new Pound(5),
new Pound(6),
new Pound(6),
new Pound(7),
new Pound(7),
new Pound(8),
new Pound(8),
);
}

public function breadPrices(): PriceList
{
return PriceList::fromList(
new Pound(7),
new Pound(8),
new Pound(9),
new Pound(10),
new Pound(11),
new Pound(11),
new Pound(12),
new Pound(13),
new Pound(14),
new Pound(15),
);
}

public function cheesePrices(): PriceList
{
return PriceList::fromList(
new Pound(7),
new Pound(8),
new Pound(9),
new Pound(10),
new Pound(11),
new Pound(12),
new Pound(13),
new Pound(14),
new Pound(14),
new Pound(15),
);
}

public function whiskyPrices(): PriceList
{
return PriceList::fromList(
new Pound(7),
new Pound(8),
new Pound(9),
new Pound(10),
new Pound(11),
new Pound(12),
new Pound(13),
new Pound(14),
new Pound(15),
new Pound(16),
);
}
}
44 changes: 44 additions & 0 deletions src/PriceListCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

use function get_class;
use InvalidArgumentException;
use function sprintf;

final class PriceListCollection
{
/** @var PriceList[] */
private $priceLists;

public function __construct()
{
$this->priceLists = [];
}

public function registerPriceListForGood(Good $good, PriceList $priceList): void
{
$this->priceLists[get_class($good)] = $priceList;
}

public function currentFor(Good $good): Pound
{
$this->ensureGoodHasPriceList($good);

return $this->priceLists[get_class($good)]->current();
}

public function moveFor(Good $good, int $positions): Pound
{
$this->ensureGoodHasPriceList($good);

return $this->priceLists[get_class($good)]->move($positions);
}

private function ensureGoodHasPriceList(Good $good): void
{
if (!isset($this->priceLists[get_class($good)])) {
throw new InvalidArgumentException(sprintf("Good: %s is unknown", get_class($good)));
}
}
}
8 changes: 8 additions & 0 deletions src/Whisky.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Whisky extends Good
{

}
8 changes: 8 additions & 0 deletions src/Wool.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Wool extends Good
{

}
4 changes: 3 additions & 1 deletion src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ function($class) {
'clansofcaledonia\\pound' => '/Pound.php',
'clansofcaledonia\\pricelist' => '/PriceList.php',
'clansofcaledonia\\pricelistbuilder' => '/PriceListBuilder.php',
'clansofcaledonia\\unit' => '/Unit.php'
'clansofcaledonia\\unit' => '/Unit.php',
'clansofcaledonia\\pricelistcollection' => '/PriceListCollection.php',
'clansofcaledonia\\wool' => '/Wool.php'
);
}
$cn = strtolower($class);
Expand Down
18 changes: 0 additions & 18 deletions tests/GoodTest.php

This file was deleted.

Loading