Primaxm

Untitled

Oct 20th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. <?php
  2. //spl_autoload_register();
  3.  
  4. $personInput = preg_split( "/[=;]/", readline());
  5. $productsInput = preg_split( "/[=;]/", readline());
  6.  
  7. $products = [];
  8. $persons = [];
  9.  
  10. //insert persons obj in array
  11. for ($a = 0; $a <= count($personInput) / 2; $a+=2) {
  12. try {
  13. $person = new Person($personInput[$a], floatval($personInput[$a+1]));
  14. $persons[$personInput[$a]] = $person;
  15. } catch (Exception $e) {
  16. echo $e->getMessage() . PHP_EOL;
  17. }
  18. }
  19.  
  20. //insert products obj in array
  21. for ($a = 0; $a <= count($productsInput) / 2; $a+=2) {
  22. try {
  23. $product = new Product($productsInput[$a], floatval($productsInput[$a+1]));
  24. $products[$productsInput[$a]] = $product;
  25. } catch (Exception $e) {
  26. echo $e->getMessage() . PHP_EOL;
  27. }
  28. }
  29.  
  30. //read command
  31. $command = readline();
  32.  
  33. //print action
  34. while ($command != "END") {
  35. list($name, $prod) = explode(" ", $command);
  36. if (array_key_exists($name, $persons) && array_key_exists($prod, $products)) {
  37. try {
  38. $persons[$name]->setProductsInBag($products[$prod]);
  39. echo $name . " bought " . $prod . PHP_EOL;
  40. } catch (Exception $e) {
  41. echo $e->getMessage() . PHP_EOL;
  42. }
  43. }
  44.  
  45.  
  46. $command = readline();
  47.  
  48. }
  49.  
  50. //print final result
  51. foreach ($persons as $personName => $personObj) {
  52. echo $personName . " - ";
  53. if (count($personObj->getBagOfProducts()) > 0) {
  54. echo implode(",", array_map(function($obj){return $obj->getProductName();}, $personObj->getBagOfProducts())) . PHP_EOL;
  55. } else {
  56. echo "Nothing bought";
  57. }
  58.  
  59. }
  60.  
  61.  
  62. class Product
  63. {
  64. /**
  65. * @var string
  66. */
  67. private $productName;
  68.  
  69. /**
  70. * @var float
  71. */
  72. private $cost;
  73.  
  74. /**
  75. * Product constructor.
  76. * @param string $productName
  77. * @param float $cost
  78. * @throws Exception
  79. */
  80. public function __construct(string $productName, float $cost)
  81. {
  82. $this->setProductName($productName);
  83. $this->setCost($cost);
  84. }
  85.  
  86. /**
  87. * @return string
  88. */
  89. public function getProductName(): string
  90. {
  91. return $this->productName;
  92. }
  93.  
  94. /**
  95. * @param string $productName
  96. * @throws Exception
  97. */
  98. public function setProductName(string $productName): void
  99. {
  100. if (empty($productName)) {
  101. throw new Exception("Product name cannot be empty");
  102. }
  103. $this->productName = $productName;
  104. }
  105.  
  106. /**
  107. * @return float
  108. */
  109. public function getCost(): float
  110. {
  111. return $this->cost;
  112. }
  113.  
  114. /**
  115. * @param float $cost
  116. * @throws Exception
  117. */
  118. public function setCost(float $cost): void
  119. {
  120. if ($cost < 0) {
  121. throw new Exception("Product price cannot be negative");
  122. }
  123. $this->cost = $cost;
  124. }
  125.  
  126. }
  127.  
  128. class Person
  129. {
  130. /**
  131. * @var string
  132. */
  133. private $name;
  134.  
  135. /**
  136. * @var float
  137. */
  138. private $money;
  139.  
  140. /**
  141. * @var array
  142. */
  143. private $bagOfProducts = [];
  144.  
  145. /**
  146. * Person constructor.
  147. * @param string $name
  148. * @param float $money
  149. * @throws Exception
  150. */
  151. public function __construct(string $name, float $money)
  152. {
  153. $this->setName($name);
  154. $this->setMoney($money);
  155. }
  156.  
  157.  
  158. /**
  159. * @return string
  160. */
  161. public function getName(): string
  162. {
  163. return $this->name;
  164. }
  165.  
  166. /**
  167. * @param string $name
  168. * @throws Exception
  169. */
  170. public function setName(string $name): void
  171. {
  172. if (empty($name)) {
  173. throw new Exception("Name cannot be empty");
  174. }
  175. $this->name = $name;
  176. }
  177.  
  178. /**
  179. * @return float
  180. */
  181. public function getMoney(): float
  182. {
  183. return $this->money;
  184. }
  185.  
  186. /**
  187. * @param float $money
  188. * @throws Exception
  189. */
  190. public function setMoney(float $money): void
  191. {
  192. if ($money < 0) {
  193. throw new Exception("Money cannot be negative");
  194. }
  195. $this->money = $money;
  196. }
  197.  
  198. /**
  199. * @return array
  200. */
  201. public function getBagOfProducts(): array
  202. {
  203. return $this->bagOfProducts;
  204. }
  205.  
  206. /**
  207. * @param Product
  208. * @throws Exception
  209. */
  210. public function setProductsInBag(Product $product): void
  211. {
  212. if($this->money < $product->getCost()) {
  213. throw new Exception($this->name . " can't afford " . $product->getProductName());
  214. }
  215. $this->bagOfProducts[] = $product;
  216. $this->money -= $product->getCost();
  217.  
  218. }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment