Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param float $volume volume of backpack
- * @param ItemsGroupInput[] $items array of items groups
- *
- * @return ItemsGroup
- * @throws Exception
- */
- function loadBag ($volume, $items)
- <?php
- class Item{
- /**
- * @var string name of item
- */
- private $name;
- /**
- * @var float volume of item
- */
- private $volume;
- /**
- * @var float price of item
- */
- private $price;
- /**
- * @var int count of items of such kinds
- */
- private $count;
- /**
- * @var ItemsGroup
- */
- private $group;
- /**
- * @return string
- */
- public function getName(){
- return $this->name;
- }
- /**
- * @return float
- */
- public function getVolume(){
- return $this->volume;
- }
- /**
- * @return float
- */
- public function getPrice(){
- return $this->price;
- }
- /**
- * @param ItemsGroup
- */
- public function setGroup(ItemsGroup $itemGroup){
- $this->group = $itemGroup;
- }
- /**
- * Item constructor
- * @param string $name
- * @param float $volume
- * @param float $price
- * @param int $count
- */
- function __construct($name, $volume, $price, $count){
- if(!is_string($name)){throw new exception('Item name should be a string!');} else{$this->name = $name;}
- if(!is_float($volume)){throw new exception('Item volume should be a float!');}else{$this->volume = $volume;}
- if(!is_float($price)){throw new exception('Item price should be a float!');}else{$this->price = $price;}
- if(!is_integer($count) || $count < 0){throw new exception('Item count should be an integer!');}else{$this->count = $count;}
- }
- } // end of item class
- class ItemsGroup{
- private static $idSeque = 1;
- /**
- * @var int
- */
- private $id;
- /**
- * @var Item[]
- */
- protected $items;
- public function __construct($items){
- $this->items = $items;
- $this->id = self::$idSeque;
- self::$idSeque++;
- }
- /**
- * @return Item[]
- */
- public function getItems(){
- return $this->items;
- }
- function addItem($item)
- {
- $this->items[] = $item;
- }
- public function getId(){
- return $this->id;
- }
- public function getTotalPrice(){
- $totalPrice = 0;
- foreach($this->getItems() as $item){
- $totalPrice+=$item->getPrice();
- }
- return $totalPrice;
- }
- }
- // end of itemGroup class
- /**
- * @param float $volume volume of backpack
- * @param ItemsGroupInput[] $items array of items groups
- *
- *@return ItemsGroup
- *@throws Exception
- */
- function loadBag($volume, $listOfItemGroup){
- $output = new itemsGroup(array());
- foreach($listOfItemGroup as $itemGroup){
- $items = $itemGroup->getItems();
- for($i = 0; $i < count($items); $i++){
- $output->addItem($items[$i]);
- }
- } // end of foreach
- return $output;
- } // end of class
- $hats = new ItemsGroup(
- array(
- new Item('hats1', 0.5, 1.99, 5),
- new Item('hats2', 0.3, 2.99, 2),
- new Item('hats3', 0.2, 1.50, 3)
- )
- );
- /* echo $hats->getId();
- echo "<br>";
- echo $hats->getTotalPrice(); */
- $socks = new ItemsGroup(
- array(
- new Item('socks1', 0.01, 2.99, 2),
- new Item('socks2', 0.1, 3.99, 3),
- new Item('socks3', 0.02, 4.99, 4)
- )
- );
- /* echo "<br>";
- echo $socks->getId();
- echo "<br>";
- echo $socks->getTotalPrice(); */
- $tShirts = new ItemsGroup(
- array(
- new Item('t-shirt1', 0.1, 5.99, 4),
- new Item('t-shirt2', 0.1, 6.99, 5),
- new Item('t-shirt3', 0.15, 7.99, 6),
- new Item('t-shirt4', 0.2, 8.99, 7)
- )
- );
- /* echo "<br>";
- echo $tShirts->getId();
- echo "<br>";
- echo $tShirts->getTotalPrice(); */
- $pants = new ItemsGroup(
- array(
- new Item('pant1', 0.2, 5.99, 8),
- new Item('pant2', 0.21, 3.99, 4),
- new Item('pant3', 0.25, 2.99, 2)
- )
- );
- /* echo "<br>";
- echo $pants->getId();
- echo "<br>";
- echo $pants->getTotalPrice(); */
- ///////////////////////////////////////////////////Call function///////////////////////////////////////////////////
- try{
- echo "<pre>";
- print_r(loadBag(1, array($hats, $socks)));
- echo "</pre>";
- } catch(Exception $e){
- echo "Caught exception: " . $e->getMessage();
- }
- ?>
Add Comment
Please, Sign In to add comment