Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //spl_autoload_register();
- $personInput = preg_split( "/[=;]/", readline());
- $productsInput = preg_split( "/[=;]/", readline());
- $products = [];
- $persons = [];
- //insert persons obj in array
- for ($a = 0; $a <= count($personInput) / 2; $a+=2) {
- try {
- $person = new Person($personInput[$a], floatval($personInput[$a+1]));
- $persons[$personInput[$a]] = $person;
- } catch (Exception $e) {
- echo $e->getMessage() . PHP_EOL;
- }
- }
- //insert products obj in array
- for ($a = 0; $a <= count($productsInput) / 2; $a+=2) {
- try {
- $product = new Product($productsInput[$a], floatval($productsInput[$a+1]));
- $products[$productsInput[$a]] = $product;
- } catch (Exception $e) {
- echo $e->getMessage() . PHP_EOL;
- }
- }
- //read command
- $command = readline();
- //print action
- while ($command != "END") {
- list($name, $prod) = explode(" ", $command);
- if (array_key_exists($name, $persons) && array_key_exists($prod, $products)) {
- try {
- $persons[$name]->setProductsInBag($products[$prod]);
- echo $name . " bought " . $prod . PHP_EOL;
- } catch (Exception $e) {
- echo $e->getMessage() . PHP_EOL;
- }
- }
- $command = readline();
- }
- //print final result
- foreach ($persons as $personName => $personObj) {
- echo $personName . " - ";
- if (count($personObj->getBagOfProducts()) > 0) {
- echo implode(",", array_map(function($obj){return $obj->getProductName();}, $personObj->getBagOfProducts())) . PHP_EOL;
- } else {
- echo "Nothing bought";
- }
- }
- class Product
- {
- /**
- * @var string
- */
- private $productName;
- /**
- * @var float
- */
- private $cost;
- /**
- * Product constructor.
- * @param string $productName
- * @param float $cost
- * @throws Exception
- */
- public function __construct(string $productName, float $cost)
- {
- $this->setProductName($productName);
- $this->setCost($cost);
- }
- /**
- * @return string
- */
- public function getProductName(): string
- {
- return $this->productName;
- }
- /**
- * @param string $productName
- * @throws Exception
- */
- public function setProductName(string $productName): void
- {
- if (empty($productName)) {
- throw new Exception("Product name cannot be empty");
- }
- $this->productName = $productName;
- }
- /**
- * @return float
- */
- public function getCost(): float
- {
- return $this->cost;
- }
- /**
- * @param float $cost
- * @throws Exception
- */
- public function setCost(float $cost): void
- {
- if ($cost < 0) {
- throw new Exception("Product price cannot be negative");
- }
- $this->cost = $cost;
- }
- }
- class Person
- {
- /**
- * @var string
- */
- private $name;
- /**
- * @var float
- */
- private $money;
- /**
- * @var array
- */
- private $bagOfProducts = [];
- /**
- * Person constructor.
- * @param string $name
- * @param float $money
- * @throws Exception
- */
- public function __construct(string $name, float $money)
- {
- $this->setName($name);
- $this->setMoney($money);
- }
- /**
- * @return string
- */
- public function getName(): string
- {
- return $this->name;
- }
- /**
- * @param string $name
- * @throws Exception
- */
- public function setName(string $name): void
- {
- if (empty($name)) {
- throw new Exception("Name cannot be empty");
- }
- $this->name = $name;
- }
- /**
- * @return float
- */
- public function getMoney(): float
- {
- return $this->money;
- }
- /**
- * @param float $money
- * @throws Exception
- */
- public function setMoney(float $money): void
- {
- if ($money < 0) {
- throw new Exception("Money cannot be negative");
- }
- $this->money = $money;
- }
- /**
- * @return array
- */
- public function getBagOfProducts(): array
- {
- return $this->bagOfProducts;
- }
- /**
- * @param Product
- * @throws Exception
- */
- public function setProductsInBag(Product $product): void
- {
- if($this->money < $product->getCost()) {
- throw new Exception($this->name . " can't afford " . $product->getProductName());
- }
- $this->bagOfProducts[] = $product;
- $this->money -= $product->getCost();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment