-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBag.php
71 lines (58 loc) · 1.39 KB
/
Bag.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
64
65
66
67
68
69
70
71
<?php
namespace DesignPatterns\Structural\FacadePattern;
use InvalidArgumentException;
/**
* Class Bag.
*/
class Bag implements BagInterface
{
/**
* @var int
*/
protected $bells = 0;
/**
* @var Turnips
*/
protected $turnips;
/**
* @param int $bells
*/
public function setBells(int $bells): int
{
$this->bells += $bells;
return $this->bells;
}
/**
* @throws InvalidArgumentException
* @return int
*/
public function getBells(int $bells): int
{
if ($this->bells >= $bells) {
$this->bells -= $bells;
return $this->bells;
}
throw new InvalidArgumentException('背包裡頭沒有那麼多的鈴錢。');
}
/**
* @param Turnips
*/
public function setTurnips(Turnips $turnips): Turnips
{
$this->turnips = $turnips;
return $this->turnips;
}
/**
* @throws InvalidArgumentException
* @return Turnips
*/
public function getTurnips(int $count): Turnips
{
if ($this->turnips->getCount() >= $count) {
$newCount = $this->turnips->getCount() - $count;
$this->turnips->setCount($newCount);
return new Turnips($this->turnips->getPrice(), $count);
}
throw new InvalidArgumentException('背包裡頭沒有那麼多的大頭菜。');
}
}