Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types = 1);
- class Person
- {
- private $name;
- private $money;
- private $bag = [];
- public function __construct(string $name, float $money)
- {
- $this->setName($name);
- $this->setMoney($money);
- }
- /**
- * @return mixed
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * @param $name
- * @throws Exception
- */
- private function setName($name)
- {
- if (empty($name) ) {
- throw new Exception("Name cannot be empty");
- }
- $this->name = $name;
- }
- /**
- * @return mixed
- */
- public function getMoney()
- {
- return $this->money;
- }
- /**
- * @param $money
- * @throws Exception
- */
- public function setMoney($money)
- {
- if ($money < 0) {
- throw new Exception("Money cannot be negative");
- }
- $this->money = $money;
- }
- /**
- * @return array
- */
- public function getBag()
- {
- return $this->bag;
- }
- /**
- * @param array $bag
- */
- public function setBag($bag)
- {
- $this->bag = $bag;
- }
- public function addToBag($bag)
- {
- $this->bag[] = $bag;
- }
- }
- class Product
- {
- private $name;
- private $cost;
- /**
- * Product constructor.
- * @param $name
- * @param $cost
- */
- public function __construct($name, $cost)
- {
- $this->setName($name);
- $this->setCost($cost);
- }
- /**
- * @return $name
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * @return mixed
- */
- public function getCost()
- {
- return $this->cost;
- }
- /**
- * @param mixed $name
- */
- public function setName($name)
- {
- if (empty($name) ) {
- throw new Exception("Name cannot be empty");
- }
- $this->name = $name;
- }
- /**
- * @param mixed $cost
- */
- public function setCost($cost)
- {
- if ($cost <= 0) {
- throw new Exception("Cost cannot be negative");
- }
- $this->cost = $cost;
- }
- }
- $persons = [];
- $products = [];
- $cmd = trim(fgets(STDIN));
- $personsInput = explode(";", $cmd);
- for ($i = 0; $i < count($personsInput) - 1; $i++) {
- $person = $personsInput[$i];
- $personI = explode("=", $person);
- try {
- $name = $personI[0];
- $money = intval($personI[1]);
- $persons[] = new Person($name, $money);
- }
- catch (Exception $e) {
- echo $e->getMessage() . PHP_EOL;
- goto byebye;
- }
- }
- $cmd = trim(fgets(STDIN));
- $productsInput = explode(";", $cmd);
- for ($j = 0; $j < count($personsInput) - 1; $j++) {
- $productBi = $productsInput[$j];
- $productI = explode("=", $productBi);
- try {
- $name = $productI[0];
- $cost = intval($productI[1]);
- $products[] = new Product($name, $cost);
- }
- catch (Exception $e) {
- echo $e->getMessage() . PHP_EOL;
- goto byebye;
- }
- }
- function buy(string $buyer1, string $product1, array $persons, array $products)
- {
- // echo "test person input " . $buyer1 . PHP_EOL;
- foreach ($persons as $key) {
- if ($buyer1 == $key->getName()) {
- // echo "test buyer1 1 " . $buyer1 . PHP_EOL;
- $buyer = $key;
- break;
- }
- }
- foreach ($products as $key) {
- if ($product1 == $key->getName()) {
- // echo "test product " . $key->getName() . PHP_EOL;
- $prodcut = $key;
- break;
- }
- }
- if ($prodcut->getCost() > $buyer->getMoney()) {
- echo $buyer->getName() . " can't afford " . $prodcut->getName() . PHP_EOL;
- } else {
- $buyerMoney = $buyer->getMoney();
- $buyerMoney -= $prodcut->getCost();
- $buyer->setMoney($buyerMoney);
- $buyer->addToBag($prodcut->getName());
- echo $buyer->getName() . " bought " . $prodcut->getName() . PHP_EOL;
- }
- }
- $cmd = trim(fgets(STDIN));
- while ($cmd != 'END') {
- $tokens = explode(" ", $cmd);
- $buyer = $tokens[0];
- $product = $tokens[1];
- buy($buyer, $product, $persons, $products);
- $cmd = trim(fgets(STDIN));
- }
- $personsPurchingList = "";
- //foreach ($persons as $key1) {
- for ($i = 0; $i < count($persons); $i++){
- $personsPurching = $persons[$i]->getBag();
- if (count($personsPurching) == 0) {
- echo $persons[$i]->getName() . ' - ' . 'Nothing bought' . PHP_EOL;
- }
- elseif (count($personsPurching) == 1) {
- echo $persons[$i]->getName() . ' - ' . $personsPurching[0] . PHP_EOL;
- }
- else {
- for ($j = 0; $j<count($personsPurching)-1;$j++){
- $personsPurchingList .= $personsPurching[$j];
- $personsPurchingList .= ',';
- }
- $personsPurchingList .= $personsPurching[$j];
- echo $persons[$i]->getName() . ' - ' . $personsPurchingList . PHP_EOL;
- $personsPurchingList = "";
- }
- }
- byebye:
- while ($cmd != 'END') {
- $cmd = trim(fgets(STDIN));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement