Guest User

Untitled

a guest
Nov 20th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. /**
  2. * @param float $volume volume of backpack
  3. * @param ItemsGroupInput[] $items array of items groups
  4. *
  5. * @return ItemsGroup
  6. * @throws Exception
  7. */
  8. function loadBag ($volume, $items)
  9.  
  10. <?php
  11.  
  12. class Item{
  13.  
  14. /**
  15. * @var string name of item
  16. */
  17.  
  18. private $name;
  19.  
  20. /**
  21. * @var float volume of item
  22. */
  23.  
  24. private $volume;
  25.  
  26. /**
  27. * @var float price of item
  28. */
  29.  
  30. private $price;
  31.  
  32. /**
  33. * @var int count of items of such kinds
  34. */
  35.  
  36. private $count;
  37.  
  38. /**
  39. * @var ItemsGroup
  40. */
  41.  
  42. private $group;
  43.  
  44. /**
  45. * @return string
  46. */
  47.  
  48. public function getName(){
  49.  
  50. return $this->name;
  51. }
  52.  
  53. /**
  54. * @return float
  55. */
  56.  
  57. public function getVolume(){
  58.  
  59. return $this->volume;
  60. }
  61.  
  62. /**
  63. * @return float
  64. */
  65.  
  66. public function getPrice(){
  67.  
  68. return $this->price;
  69. }
  70.  
  71. /**
  72. * @param ItemsGroup
  73. */
  74.  
  75. public function setGroup(ItemsGroup $itemGroup){
  76.  
  77. $this->group = $itemGroup;
  78. }
  79.  
  80. /**
  81. * Item constructor
  82. * @param string $name
  83. * @param float $volume
  84. * @param float $price
  85. * @param int $count
  86. */
  87.  
  88. function __construct($name, $volume, $price, $count){
  89.  
  90. if(!is_string($name)){throw new exception('Item name should be a string!');} else{$this->name = $name;}
  91. if(!is_float($volume)){throw new exception('Item volume should be a float!');}else{$this->volume = $volume;}
  92. if(!is_float($price)){throw new exception('Item price should be a float!');}else{$this->price = $price;}
  93. if(!is_integer($count) || $count < 0){throw new exception('Item count should be an integer!');}else{$this->count = $count;}
  94.  
  95. }
  96.  
  97. } // end of item class
  98.  
  99.  
  100. class ItemsGroup{
  101.  
  102. private static $idSeque = 1;
  103.  
  104. /**
  105. * @var int
  106. */
  107.  
  108. private $id;
  109.  
  110. /**
  111. * @var Item[]
  112. */
  113.  
  114. protected $items;
  115.  
  116. public function __construct($items){
  117.  
  118. $this->items = $items;
  119. $this->id = self::$idSeque;
  120. self::$idSeque++;
  121. }
  122.  
  123. /**
  124. * @return Item[]
  125. */
  126.  
  127. public function getItems(){
  128.  
  129. return $this->items;
  130. }
  131.  
  132. function addItem($item)
  133. {
  134. $this->items[] = $item;
  135. }
  136.  
  137. public function getId(){
  138.  
  139. return $this->id;
  140. }
  141.  
  142. public function getTotalPrice(){
  143.  
  144. $totalPrice = 0;
  145.  
  146. foreach($this->getItems() as $item){
  147.  
  148. $totalPrice+=$item->getPrice();
  149. }
  150.  
  151. return $totalPrice;
  152. }
  153. }
  154.  
  155. // end of itemGroup class
  156.  
  157. /**
  158. * @param float $volume volume of backpack
  159. * @param ItemsGroupInput[] $items array of items groups
  160. *
  161. *@return ItemsGroup
  162. *@throws Exception
  163. */
  164.  
  165. function loadBag($volume, $listOfItemGroup){
  166.  
  167. $output = new itemsGroup(array());
  168.  
  169. foreach($listOfItemGroup as $itemGroup){
  170.  
  171. $items = $itemGroup->getItems();
  172.  
  173. for($i = 0; $i < count($items); $i++){
  174.  
  175. $output->addItem($items[$i]);
  176.  
  177. }
  178. } // end of foreach
  179.  
  180. return $output;
  181.  
  182. } // end of class
  183.  
  184.  
  185. $hats = new ItemsGroup(
  186.  
  187. array(
  188.  
  189. new Item('hats1', 0.5, 1.99, 5),
  190. new Item('hats2', 0.3, 2.99, 2),
  191. new Item('hats3', 0.2, 1.50, 3)
  192. )
  193. );
  194.  
  195.  
  196. /* echo $hats->getId();
  197.  
  198. echo "<br>";
  199.  
  200. echo $hats->getTotalPrice(); */
  201.  
  202.  
  203. $socks = new ItemsGroup(
  204.  
  205. array(
  206.  
  207. new Item('socks1', 0.01, 2.99, 2),
  208. new Item('socks2', 0.1, 3.99, 3),
  209. new Item('socks3', 0.02, 4.99, 4)
  210. )
  211. );
  212.  
  213.  
  214. /* echo "<br>";
  215.  
  216. echo $socks->getId();
  217.  
  218. echo "<br>";
  219.  
  220. echo $socks->getTotalPrice(); */
  221.  
  222.  
  223. $tShirts = new ItemsGroup(
  224. array(
  225. new Item('t-shirt1', 0.1, 5.99, 4),
  226. new Item('t-shirt2', 0.1, 6.99, 5),
  227. new Item('t-shirt3', 0.15, 7.99, 6),
  228. new Item('t-shirt4', 0.2, 8.99, 7)
  229. )
  230. );
  231.  
  232. /* echo "<br>";
  233.  
  234. echo $tShirts->getId();
  235.  
  236. echo "<br>";
  237.  
  238. echo $tShirts->getTotalPrice(); */
  239.  
  240. $pants = new ItemsGroup(
  241. array(
  242. new Item('pant1', 0.2, 5.99, 8),
  243. new Item('pant2', 0.21, 3.99, 4),
  244. new Item('pant3', 0.25, 2.99, 2)
  245. )
  246. );
  247.  
  248. /* echo "<br>";
  249.  
  250. echo $pants->getId();
  251.  
  252. echo "<br>";
  253.  
  254. echo $pants->getTotalPrice(); */
  255.  
  256. ///////////////////////////////////////////////////Call function///////////////////////////////////////////////////
  257.  
  258. try{
  259.  
  260. echo "<pre>";
  261. print_r(loadBag(1, array($hats, $socks)));
  262. echo "</pre>";
  263.  
  264. } catch(Exception $e){
  265.  
  266. echo "Caught exception: " . $e->getMessage();
  267. }
  268.  
  269.  
  270. ?>
Add Comment
Please, Sign In to add comment