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
Binary file added doc/prices.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
verbose="true"
testdox="true"
colors="true">
<testsuites>
<testsuite name="default">
Expand Down
49 changes: 49 additions & 0 deletions src/Game.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace ClansOfCaledonia;

use ClansOfCaledonia\Good\Grain;
use ClansOfCaledonia\Good\Milk;
use ClansOfCaledonia\Good\Wool;

final class Game
{
const GOODS = [ // TODO: Improve this
'Milk',
'Wool',
'Grain',
];

public static function main(Pound $money): void
{
$priceBuilder = new PriceListBuilder();

$market = (new Market)
->withPriceList(Milk::name(), $priceBuilder->milkPrices())
->withPriceList(Wool::name(), $priceBuilder->woolPrices())
->withPriceList(Grain::name(), $priceBuilder->grainPrices());

$player = new Player($money, $market);
while (true) {
echo sprintf('You have %i pounds', $player->money());
$goodName = readline(sprintf('What do you want to buy now? (%s)', implode(',', self::GOODS)));
$amount = (int)readline('How many pounds?');
$player->buy(new Unit($amount), Good::byName($goodName));
static::printMarket($market);
sleep(1);
}
}

private static function printMarket(Market $market)
{
foreach($market->allPriceList() as $goodName => $priceList){
echo $goodName.PHP_EOL;
foreach($priceList as $price) {
// display the table
}
echo PHP_EOL;
}
}
}
36 changes: 35 additions & 1 deletion src/Good.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,45 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

use ClansOfCaledonia\Good\Grain;
use ClansOfCaledonia\Good\Milk;
use ClansOfCaledonia\Good\Wool;

abstract class Good
{
public static function name(): string
{
return static::class;
}

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

public static function wool(): self
{
return new Wool();
}

public static function grain(): self
{
return new Grain();
}

public static function byName(string $goodName)
{
switch ($goodName){
case Milk::name():
return self::milk();
case Wool::name():
return self::wool();
case Grain::name():
return self::grain();
}

throw new \Exception('Good Unknown');
}

public function isMilk(): bool
Expand Down
9 changes: 9 additions & 0 deletions src/Good/Grain.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia\Good;

use ClansOfCaledonia\Good;

final class Grain extends Good
{
}
18 changes: 18 additions & 0 deletions src/Good/Milk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia\Good;

use ClansOfCaledonia\Good;

final class Milk extends Good
{
public static function name(): string
{
return 'Milk';
}

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

namespace ClansOfCaledonia\Good;

use ClansOfCaledonia\Good;

final class Wool extends Good
{
}
37 changes: 31 additions & 6 deletions src/Market.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,43 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Market
{
public function priceFor(Good $milk): Pound
/** @var array<string,PriceList> */
private $prices;

public function __construct()
{
return new Pound(5);
$this->prices = [];
}

public function withPriceList(string $goodName, PriceList $priceList): self
{
$this->prices[$goodName] = $priceList;

return $this;
}

public function priceFor(Good $good): Pound
{
return $this->prices[$good->name()]->current();
}

public function sellTo(Offer $offer): Pound
{
return new Pound(
$offer->amount()->amount() *
$this->priceFor($offer->good())->amount()
);
$good = $offer->good();
$unit = $offer->amount();
$payment = $this->priceFor($good)->multiply($unit->amount());
$this->adjustPrice($good, $unit);

return $payment;
}

private function adjustPrice(Good $good, Unit $unit)
{
/** @var PriceList $priceList */
$priceList = $this->prices[$good->name()];
$priceList->moveTo($unit);
}
}
10 changes: 0 additions & 10 deletions src/Milk.php

This file was deleted.

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

namespace ClansOfCaledonia;

final class Player
{
/** @var Pound */
private $money;

/** @var Market */
private $market;

public function __construct(Pound $money, Market $market)
{
$this->money = $money;
$this->market = $market;
}

public function buy(Unit $unit, Good $good): void
{
$payment = $this->market->sellTo(new Offer($unit, $good));
$this->money = $this->money->minus($payment->amount());
}

public function money(): Pound
{
return $this->money;
}
}
11 changes: 11 additions & 0 deletions src/Pound.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class Pound
Expand All @@ -17,4 +18,14 @@ public function amount(): int
{
return $this->amount;
}

public function multiply(int $factor): Pound
{
return new Pound($this->amount * $factor);
}

public function minus(int $number): Pound
{
return new Pound($this->amount - $number);
}
}
16 changes: 12 additions & 4 deletions src/PriceList.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class PriceList
Expand All @@ -11,23 +12,30 @@ final class PriceList
/**
* @var int
*/
private $position = 3;
private $position;

public static function fromList(Pound ...$pounds): self
public static function fromList(int $initPosition, Pound ...$pounds): self
{
return new self($pounds);
return new self($initPosition, $pounds);
}

/**
* @param int $initPosition
* @param Pound[] $prices
*/
private function __construct(array $prices)
private function __construct(int $initPosition, array $prices)
{
$this->position = $initPosition;
$this->prices = $prices;
}

public function current(): Pound
{
return $this->prices[$this->position];
}

public function moveTo(Unit $unit): void
{
$this->position += $unit->amount();
}
}
36 changes: 36 additions & 0 deletions src/PriceListBuilder.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php declare(strict_types=1);

namespace ClansOfCaledonia;

final class PriceListBuilder
{
public function milkPrices(): PriceList
{
return PriceList::fromList(
$initPosition = 3,
new Pound(3),
new Pound(4),
new Pound(5),
Expand All @@ -18,4 +20,38 @@ public function milkPrices(): PriceList
new Pound(8)
);
}

public function woolPrices(): PriceList
{
return PriceList::fromList(
$initPosition = 3,
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(
$initPosition = 3,
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)
);
}
}
5 changes: 4 additions & 1 deletion src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ function($class) {
$classes = array(
'clansofcaledonia\\exception' => '/Exception.php',
'clansofcaledonia\\good' => '/Good.php',
'clansofcaledonia\\good\\grain' => '/Good/Grain.php',
'clansofcaledonia\\good\\milk' => '/Good/Milk.php',
'clansofcaledonia\\good\\wool' => '/Good/Wool.php',
'clansofcaledonia\\market' => '/Market.php',
'clansofcaledonia\\milk' => '/Milk.php',
'clansofcaledonia\\offer' => '/Offer.php',
'clansofcaledonia\\outofrangeexception' => '/OutOfRangeException.php',
'clansofcaledonia\\player' => '/Player.php',
'clansofcaledonia\\pound' => '/Pound.php',
'clansofcaledonia\\pricelist' => '/PriceList.php',
'clansofcaledonia\\pricelistbuilder' => '/PriceListBuilder.php',
Expand Down
11 changes: 10 additions & 1 deletion tests/GoodTest.php → tests/Good/GoodTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php declare(strict_types=1);
namespace ClansOfCaledonia;

namespace ClansOfCaledonia\Good;

use ClansOfCaledonia\Good;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -15,4 +17,11 @@ public function testCanBeMilk(): void

$this->assertTrue($milk->isMilk());
}

public function testCanNotBeMilk(): void
{
$grain = Good::grain();

$this->assertFalse($grain->isMilk());
}
}
Loading