Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2017
572
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.65 KB | None | 0 0
  1. <?php
  2. class Shop{
  3.     /**
  4.      * Person
  5.      */
  6.     private $person;
  7.     private $bag=[];
  8.  
  9.     function __construct(Person $person)
  10.     {
  11.         $this->person= $person;
  12.     }
  13.  
  14.     function setBag(Product $product){
  15.         if ($this->person->getMoney() < $product->getCost()) {
  16.             throw new Exception($this->person->getName(). ' can\'t afford ' . $product->getName() . PHP_EOL);
  17.         }
  18.         $this->bag[] = $product;
  19.         $this->person->bay($product->getCost());
  20.         echo $this->person->getName() . ' bought ' . $product->getName().PHP_EOL;
  21.     }
  22.  
  23.     public function getPerson()
  24.     {
  25.         return $this->person;
  26.     }
  27.  
  28.     public function getBag(): array
  29.     {
  30.         return $this->bag;
  31.     }
  32.  
  33.  
  34.  
  35. }
  36.  
  37. class Product
  38. {
  39.     const WORD_LENGHT = 0;
  40.     private $name;
  41.     private $cost;
  42.  
  43.     function __construct($name, $cost)
  44.     {
  45.         $this->setName($name);
  46.         $this->setCost($cost);
  47.     }
  48.  
  49.  
  50.     public function setName($name)
  51.     {
  52.         if($name == '') {
  53.             throw new Exception('Name cannot be empty');
  54.         }
  55.         $this->name = $name;
  56.     }
  57.  
  58.     public function setCost($cost)
  59.     {
  60.         if ($cost < self::WORD_LENGHT){
  61.             throw new Exception("Money cannot be negative");
  62.         }
  63.         $this->cost = $cost;
  64.     }
  65.  
  66.    
  67.     public function getCost()
  68.     {
  69.         return $this->cost;
  70.     }
  71.  
  72.    
  73.     public function getName()
  74.     {
  75.         return $this->name;
  76.     }
  77. }
  78. class Person
  79. {
  80.     const Width = 0;
  81.     private $name;
  82.     private $money;
  83.  
  84.  
  85.     function __construct($name, $money)
  86.     {
  87.         $this->setName($name);
  88.         $this->setMoney($money);
  89.     }
  90.  
  91.     function setName($name){
  92.         if($name == '') {
  93.             throw  new Exception('Name cannot be empty');
  94.         }
  95.         $this->name = $name;
  96.     }
  97.  
  98.     function setMoney($money) {
  99.         if ($money  < self::Width){
  100.             throw new Exception('Money cannot be negative');
  101.         }
  102.         $this->money = $money;
  103.     }
  104.  
  105.     function bay($money) {
  106.         $this->money -= $money;
  107.     }
  108.  
  109.    
  110.     public function getMoney()
  111.     {
  112.         return $this->money;
  113.     }
  114.  
  115.     public function getName()
  116.     {
  117.         return $this->name;
  118.     }
  119.  
  120.  
  121. }
  122.  
  123. $inputPeople = array_map('trim', explode(';', fgets(STDIN)));
  124. $inputProducts = array_map('trim', explode(';', fgets(STDIN)));
  125.  
  126. $people = [];
  127. $shops = [];
  128.  
  129.     for ($i = 0; $i < count($inputPeople) - 1; $i++) {
  130.         if (strstr($inputPeople[$i], '=')) {
  131.             $personInfo = array_map('trim', explode('=', $inputPeople[$i]));
  132.             try {
  133.                 $person = new Person($personInfo[0], $personInfo[1]);
  134.                 $people[$personInfo[0]] = $person;
  135.                 $shops[$personInfo[0]] = new Shop ($person);
  136.             }catch (Exception $e){
  137.                 echo $e->getMessage();
  138.                 exit;
  139.             }
  140.         } else {
  141.             echo 'Name cannot be empty';
  142.             exit;
  143.         }
  144.     }
  145.  
  146.     $products = [];
  147.     for ($i = 0; $i < count($inputProducts) - 1; $i++) {
  148.         if (strstr($inputProducts[$i], '=')) {
  149.             $productInfo = array_map('trim', explode('=', $inputProducts[$i]));
  150.             try {
  151.                 $product = new Product($productInfo[0], $productInfo[1]);
  152.                 $products[$productInfo[0]] = $product;
  153.             } catch (Exception $e){
  154.                 echo $e->getMessage();
  155.                 exit;
  156.             }
  157.         } else {
  158.             echo 'Name cannot be empty';
  159.         }
  160.     }
  161.  
  162.  
  163.  
  164. while(true){
  165.     $input = array_map('trim', explode(' ', fgets(STDIN)));
  166.     if($input[0]=="END") {
  167.         break;
  168.     }
  169.  
  170. try {
  171.     $name = $input[0];
  172.     $product = $input[1];
  173.     if (!array_key_exists($name, $shops)) {
  174.         $shop = new Shop($people[$name], $products[$product]);
  175.         $shops[$name] = $shop;
  176.         $shops[$name]->setBag($products[$product]);
  177.     } else {
  178.         $shops[$name]->setBag($products[$product]);
  179.     }
  180. } catch (Exception $e){
  181.     echo $e->getMessage();
  182. }
  183. }
  184. if (count($shops) == 0) {
  185.     foreach ($people as $person) {
  186.         echo $person->getName() . ' - Nothing bought'. PHP_EOL;
  187.     }
  188.  
  189.  
  190. }
  191. foreach ($shops as $shop) {
  192.     echo $shop->getPerson()->getName() . ' - ';
  193.     $count = count($shop->getBag());
  194.     if ($count==0) {
  195.         echo 'Nothing bought'. PHP_EOL;
  196.     } else {
  197.         for ($i = 0; $i < count($shop->getBag()); $i++) {
  198.             if ($i == count($shop->getBag()) - 1) {
  199.                 echo $shop->getBag()[$i]->getName() . PHP_EOL;
  200.             } else {
  201.                 echo $shop->getBag()[$i]->getName() . ',';
  202.             }
  203.         }
  204.     }
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement