-
Notifications
You must be signed in to change notification settings - Fork 17
/
Store.php
63 lines (53 loc) · 1.8 KB
/
Store.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace DesignPatterns\Behavioral\MediatorPattern;
use InvalidArgumentException;
/**
* Class Store.
*/
class Store extends Colleague
{
/**
* @param int $price
* @param int $count
*
* @throws InvalidArgumentException
* @return Turnips
*/
public function buyTurnips(int $price, int $count)
{
$total = $price * $count;
if ($this->mediator->getBells() >= $total) {
/**
* 跟商店(Store)買大頭菜(Turnips),並且將大頭菜放入背包(Bag)當中。
*/
$this->mediator->setBells($this->mediator->getBells() - $total);
echo "[商店] 收到了 $total 鈴錢。";
echo "[商店] 賣出了 $count 顆大頭菜。";
$this->mediator->setTurnips($this->mediator->getTurnips() + $count);
return;
}
throw new InvalidArgumentException('[錯誤] 您背包裡的鈴錢不足,無法購買大頭菜。');
}
/**
* @param int $price
* @param int $count
*
* @throws InvalidArgumentException
* @return int
*/
public function sellTurnips(int $price, int $count)
{
if ($this->mediator->getTurnips() >= $count) {
/**
* 跟商店(Store)賣大頭菜(Turnips),並且將鈴錢放入背包(Bag)當中。
*/
$this->mediator->setTurnips($this->mediator->getTurnips() - $count);
echo "[商店] 收購了 $count 顆大頭菜。";
$total = $price * $count;
echo "[商店] 支出了 $total 鈴錢。";
$this->mediator->setBells($this->mediator->getBells() + $total);
return;
}
throw new InvalidArgumentException('[錯誤] 您背包裡的大頭菜不足,無法販賣大頭菜。');
}
}