forked from Kantai235/php-design-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceiver.php
More file actions
71 lines (61 loc) · 1.32 KB
/
Receiver.php
File metadata and controls
71 lines (61 loc) · 1.32 KB
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\Behavioral\CommandPattern;
use InvalidArgumentException;
/**
* Class Receiver.
*/
class Receiver
{
/**
* @var int
*/
protected int $turnips = 0;
/**
* @var int
*/
protected int $bells = 0;
/**
* @param int $price
* @param int $count
*
* @throws InvalidArgumentException
*/
public function buy(int $price, int $count)
{
$total = $price * $count;
if ($this->bells < $total) {
throw new InvalidArgumentException('您的鈴錢不足,無法購買大頭菜。');
}
$this->turnips += $count;
$this->bells -= $total;
}
/**
* @param int $price
* @param int $count
*
* @throws InvalidArgumentException
*/
public function sell(int $price, int $count)
{
if ($this->turnips < $count) {
throw new InvalidArgumentException('您的大頭菜不足,無法販賣大頭菜。');
}
$total = $price * $count;
$this->turnips -= $count;
$this->bells += $total;
}
/**
* @param int $bells
*/
public function setBells(int $bells)
{
$this->bells += $bells;
}
/**
* @return int
*/
public function getBells(): int
{
return $this->bells;
}
}