Advertisement
Guest User

softuni.bg php 2017 Problem 3. Shopping Spree

a guest
Mar 2nd, 2017
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.37 KB | None | 0 0
  1. <?php
  2. declare(strict_types = 1);
  3.  
  4. class Person
  5. {
  6.     private $name;
  7.     private $money;
  8.     private $bag = [];
  9.  
  10.     public function __construct(string $name, float $money)
  11.     {
  12.         $this->setName($name);
  13.         $this->setMoney($money);
  14.  
  15.     }
  16.  
  17.     /**
  18.      * @return mixed
  19.      */
  20.     public function getName()
  21.     {
  22.         return $this->name;
  23.     }
  24.  
  25.     /**
  26.      * @param $name
  27.      * @throws Exception
  28.      */
  29.     private function setName($name)
  30.     {
  31.         if (empty($name) ) {
  32.             throw new Exception("Name cannot be empty");
  33.         }
  34.         $this->name = $name;
  35.     }
  36.  
  37.     /**
  38.      * @return mixed
  39.      */
  40.     public function getMoney()
  41.     {
  42.         return $this->money;
  43.     }
  44.  
  45.     /**
  46.      * @param $money
  47.      * @throws Exception
  48.      */
  49.     public function setMoney($money)
  50.     {
  51.         if ($money < 0) {
  52.             throw new Exception("Money cannot be negative");
  53.         }
  54.         $this->money = $money;
  55.     }
  56.  
  57.     /**
  58.      * @return array
  59.      */
  60.     public function getBag()
  61.     {
  62.         return $this->bag;
  63.     }
  64.  
  65.     /**
  66.      * @param array $bag
  67.      */
  68.     public function setBag($bag)
  69.     {
  70.         $this->bag = $bag;
  71.     }
  72.  
  73.     public function addToBag($bag)
  74.     {
  75.         $this->bag[] = $bag;
  76.     }
  77.  
  78.  
  79. }
  80.  
  81. class Product
  82. {
  83.     private $name;
  84.     private $cost;
  85.  
  86.     /**
  87.      * Product constructor.
  88.      * @param $name
  89.      * @param $cost
  90.      */
  91.     public function __construct($name, $cost)
  92.     {
  93.         $this->setName($name);
  94.         $this->setCost($cost);
  95.     }
  96.  
  97.     /**
  98.      * @return $name
  99.      */
  100.     public function getName()
  101.     {
  102.         return $this->name;
  103.     }
  104.  
  105.     /**
  106.      * @return mixed
  107.      */
  108.     public function getCost()
  109.     {
  110.         return $this->cost;
  111.     }
  112.  
  113.     /**
  114.      * @param mixed $name
  115.      */
  116.     public function setName($name)
  117.     {
  118.         if (empty($name) ) {
  119.             throw new Exception("Name cannot be empty");
  120.         }
  121.         $this->name = $name;
  122.     }
  123.  
  124.     /**
  125.      * @param mixed $cost
  126.      */
  127.     public function setCost($cost)
  128.     {
  129.         if ($cost <= 0) {
  130.             throw new Exception("Cost cannot be negative");
  131.         }
  132.         $this->cost = $cost;
  133.     }
  134.  
  135.  
  136. }
  137.  
  138. $persons = [];
  139. $products = [];
  140.  
  141.  
  142.  
  143. $cmd = trim(fgets(STDIN));
  144. $personsInput = explode(";", $cmd);
  145.  
  146.  
  147.  
  148.     for ($i = 0; $i < count($personsInput) - 1; $i++) {
  149.         $person = $personsInput[$i];
  150.         $personI = explode("=", $person);
  151.  
  152.         try {
  153.             $name = $personI[0];
  154.             $money = intval($personI[1]);
  155.             $persons[] = new Person($name, $money);
  156.  
  157.         }
  158.  
  159.         catch (Exception $e) {
  160.             echo $e->getMessage() . PHP_EOL;
  161.             goto byebye;
  162.         }
  163.     }
  164.  
  165.  
  166.  
  167. $cmd = trim(fgets(STDIN));
  168. $productsInput = explode(";", $cmd);
  169.  
  170.  
  171.  
  172.     for ($j = 0; $j < count($personsInput) - 1; $j++) {
  173.         $productBi = $productsInput[$j];
  174.         $productI = explode("=", $productBi);
  175.  
  176.         try {
  177.  
  178.             $name = $productI[0];
  179.             $cost = intval($productI[1]);
  180.             $products[] = new Product($name, $cost);
  181.         }
  182.         catch (Exception $e) {
  183.                 echo $e->getMessage() . PHP_EOL;
  184.                 goto byebye;
  185.             }
  186.     }
  187.  
  188.  
  189. function buy(string $buyer1, string $product1, array $persons, array $products)
  190. {
  191.     // echo "test person input " . $buyer1 . PHP_EOL;
  192.     foreach ($persons as $key) {
  193.  
  194.         if ($buyer1 == $key->getName()) {
  195.             // echo "test buyer1 1 " . $buyer1 . PHP_EOL;
  196.             $buyer = $key;
  197.             break;
  198.         }
  199.     }
  200.  
  201.     foreach ($products as $key) {
  202.         if ($product1 == $key->getName()) {
  203.             // echo "test product " . $key->getName() . PHP_EOL;
  204.             $prodcut = $key;
  205.             break;
  206.         }
  207.     }
  208.  
  209.         if ($prodcut->getCost() > $buyer->getMoney()) {
  210.             echo $buyer->getName() . " can't afford " . $prodcut->getName() . PHP_EOL;
  211.         } else {
  212.             $buyerMoney = $buyer->getMoney();
  213.             $buyerMoney -= $prodcut->getCost();
  214.             $buyer->setMoney($buyerMoney);
  215.             $buyer->addToBag($prodcut->getName());
  216.             echo $buyer->getName() . " bought " . $prodcut->getName() . PHP_EOL;
  217.         }
  218.  
  219.  
  220. }
  221.  
  222. $cmd = trim(fgets(STDIN));
  223. while ($cmd != 'END') {
  224.     $tokens = explode(" ", $cmd);
  225.     $buyer = $tokens[0];
  226.     $product = $tokens[1];
  227.  
  228.     buy($buyer, $product, $persons, $products);
  229.  
  230.     $cmd = trim(fgets(STDIN));
  231. }
  232.  
  233. $personsPurchingList = "";
  234.  
  235. //foreach ($persons as $key1) {
  236.  
  237. for ($i = 0; $i < count($persons); $i++){
  238.     $personsPurching = $persons[$i]->getBag();
  239.  
  240.     if (count($personsPurching) == 0) {
  241.         echo $persons[$i]->getName() . ' - ' . 'Nothing bought' . PHP_EOL;
  242.         }
  243.     elseif (count($personsPurching) == 1) {
  244.         echo $persons[$i]->getName() . ' - ' . $personsPurching[0] . PHP_EOL;
  245.     }
  246.  
  247.     else {
  248.             for ($j = 0; $j<count($personsPurching)-1;$j++){
  249.                 $personsPurchingList .= $personsPurching[$j];
  250.                 $personsPurchingList .= ',';
  251.             }
  252.         $personsPurchingList .= $personsPurching[$j];
  253.  
  254.             echo $persons[$i]->getName() . ' - ' . $personsPurchingList . PHP_EOL;
  255.             $personsPurchingList = "";
  256.         }
  257.  
  258.  
  259. }
  260.  
  261. byebye:
  262.  
  263. while ($cmd != 'END') {
  264.  
  265.     $cmd = trim(fgets(STDIN));
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement