SHOW:
|
|
- or go back to the newest paste.
| 1 | <?php | |
| 2 | ||
| 3 | class drink | |
| 4 | {
| |
| 5 | private $alcool = ''; | |
| 6 | private $pourcent = 0; | |
| 7 | ||
| 8 | public function __construct($alcool, $pourcent) | |
| 9 | {
| |
| 10 | $this->alcool = $alcool; | |
| 11 | $this->pourcent = $pourcent; | |
| 12 | } | |
| 13 | ||
| 14 | public function __toString() | |
| 15 | {
| |
| 16 | return $this->alcool . ': ' . $this->pourcent . '%'; | |
| 17 | } | |
| 18 | ||
| 19 | public function getPourcent() | |
| 20 | {
| |
| 21 | return $this->pourcent; | |
| 22 | } | |
| 23 | ||
| 24 | public function getAlcool() | |
| 25 | {
| |
| 26 | return $this->alcool; | |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | class drinks implements iteratorAggregate | |
| 31 | {
| |
| 32 | private $drinks = array(); | |
| 33 | ||
| 34 | public function destroy() | |
| 35 | {
| |
| 36 | $this->drinks = array(); | |
| 37 | } | |
| 38 | ||
| 39 | public function add(drink $drink) | |
| 40 | {
| |
| 41 | $this->drinks[] = $drink; | |
| 42 | ||
| 43 | return $this; | |
| 44 | } | |
| 45 | ||
| 46 | public function getIterator() | |
| 47 | {
| |
| 48 | return new arrayIterator($this->drinks); | |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | class order | |
| 53 | {
| |
| 54 | private $numberOfDrinks = 0; | |
| 55 | private $alcool = ''; | |
| 56 | private $isPaid = false; | |
| 57 | ||
| 58 | public function __construct($numberOfDrinks, $alcool) | |
| 59 | {
| |
| 60 | $this->numberOfDrinks = $numberOfDrinks; | |
| 61 | $this->alcool = $alcool; | |
| 62 | } | |
| 63 | ||
| 64 | public function increaseNumberOfDrinks() | |
| 65 | {
| |
| 66 | $this->numberOfDrinks++; | |
| 67 | ||
| 68 | return $this; | |
| 69 | } | |
| 70 | ||
| 71 | public function getNumberOfDrinks() | |
| 72 | {
| |
| 73 | return $this->numberOfDrinks; | |
| 74 | } | |
| 75 | ||
| 76 | public function getAlcool() | |
| 77 | {
| |
| 78 | return $this->alcool; | |
| 79 | } | |
| 80 | ||
| 81 | public function getPrice() | |
| 82 | {
| |
| 83 | return $this->numberOfDrinks * 6; | |
| 84 | } | |
| 85 | ||
| 86 | public function isPaid() | |
| 87 | {
| |
| 88 | return $this->isPaid; | |
| 89 | } | |
| 90 | ||
| 91 | public function setPaid($boolean) | |
| 92 | {
| |
| 93 | $this->isPaid = $boolean; | |
| 94 | return $this; | |
| 95 | } | |
| 96 | } | |
| 97 | ||
| 98 | class client | |
| 99 | {
| |
| 100 | private $drinks = array(); | |
| 101 | private $creditCardNumber = ''; | |
| 102 | ||
| 103 | public function __construct() | |
| 104 | {
| |
| 105 | $this->creditCardNumber = uniqid(); | |
| 106 | } | |
| 107 | ||
| 108 | public function order($numberOfDrinks, $alcool) | |
| 109 | {
| |
| 110 | return new order($numberOfDrinks, $alcool); | |
| 111 | } | |
| 112 | ||
| 113 | public function receiveDrinks(drinks $drinks) | |
| 114 | {
| |
| 115 | foreach($drinks as $drink ) | |
| 116 | {
| |
| 117 | $this->drinks[] = $drink; | |
| 118 | } | |
| 119 | ||
| 120 | return $this; | |
| 121 | } | |
| 122 | ||
| 123 | public function getCreditCardNumber() | |
| 124 | {
| |
| 125 | return $this->creditCardNumber; | |
| 126 | } | |
| 127 | ||
| 128 | public function showDrinks() | |
| 129 | {
| |
| 130 | foreach ($this->drinks as $drink) | |
| 131 | {
| |
| 132 | echo $drink . PHP_EOL; | |
| 133 | } | |
| 134 | ||
| 135 | return $this; | |
| 136 | } | |
| 137 | } | |
| 138 | ||
| 139 | class tpe | |
| 140 | {
| |
| 141 | private $creditCardNumber = ''; | |
| 142 | private $price = 0; | |
| 143 | ||
| 144 | public function setCreditCardNumber($creditCardNumber) | |
| 145 | {
| |
| 146 | $this->creditCardNumber = $creditCardNumber; | |
| 147 | ||
| 148 | return $this; | |
| 149 | } | |
| 150 | ||
| 151 | public function setPrice($price) | |
| 152 | {
| |
| 153 | $this->price = $price; | |
| 154 | ||
| 155 | return $this; | |
| 156 | } | |
| 157 | ||
| 158 | public function executeTransaction(order $order) | |
| 159 | {
| |
| 160 | echo 'Just pay ' . $order->getPrice() . ' with card ' . $this->creditCardNumber . '!' . PHP_EOL; | |
| 161 | $order->setPaid(true); | |
| 162 | ||
| 163 | return $this; | |
| 164 | } | |
| 165 | } | |
| 166 | ||
| 167 | class barman | |
| 168 | {
| |
| 169 | private $order = null; | |
| 170 | private $drinks = null; | |
| 171 | ||
| 172 | public function receiveOrder(order $order) | |
| 173 | {
| |
| 174 | if( $this->order && !$this->order->isPaid()) | |
| 175 | {
| |
| 176 | throw new \RuntimeException('Je dois en premier me faire payer la commande en court');
| |
| 177 | } | |
| 178 | ||
| 179 | $this->order = $order; | |
| 180 | ||
| 181 | return $this; | |
| 182 | } | |
| 183 | ||
| 184 | public function prepareOrder() | |
| 185 | {
| |
| 186 | $this->drinks = new drinks(); | |
| 187 | ||
| 188 | for ($numberOfDrinks = $this->order->getNumberOfDrinks(); $numberOfDrinks > 0; $numberOfDrinks--) | |
| 189 | {
| |
| 190 | $this->drinks->add(new drink($this->order->getAlcool(), 75)); | |
| 191 | } | |
| 192 | ||
| 193 | return $this; | |
| 194 | } | |
| 195 | ||
| 196 | public function giveDrinks() | |
| 197 | {
| |
| 198 | // le barman ne les a plus donc on les lui retire | |
| 199 | $drinks = $this->drinks; | |
| 200 | $this->drinks = null; | |
| 201 | return $drinks; | |
| 202 | } | |
| 203 | ||
| 204 | public function billDrinks($client) | |
| 205 | {
| |
| 206 | $tpe = new tpe(); | |
| 207 | ||
| 208 | $tpe | |
| 209 | ->setCreditCardNumber($client->getCreditCardNumber()) | |
| 210 | ->executeTransaction($this->order) | |
| 211 | ; | |
| 212 | ||
| 213 | return $this; | |
| 214 | } | |
| 215 | } | |
| 216 | ||
| 217 | $me = new client(); | |
| 218 | $barman = new barman(); | |
| 219 | ||
| 220 | $barman | |
| 221 | ->receiveOrder($me->order(5, 'Chimay bleu')) | |
| 222 | ->prepareOrder() | |
| 223 | ; | |
| 224 | $me->receiveDrinks($barman->giveDrinks()); | |
| 225 | ||
| 226 | $barman->billDrinks($me); | |
| 227 | $me->showDrinks(); |