tristamoff

Untitled

Feb 3rd, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.23 KB | None | 0 0
  1. <?php
  2. //исходные данные
  3. $fruits = array();
  4. $fruits[] = array(
  5.   'name' => 'Бананы',
  6.   'price' => 65,
  7. );
  8. $fruits[] = array(
  9.   'name' => 'Ананасы',
  10.   'price' => 90,
  11. );
  12. $fruits[] = array(
  13.   'name' => 'Яблоки',
  14.   'price' => 50,
  15. );
  16. $fruits[] = array(
  17.   'name' => 'Груши',
  18.   'price' => 80,
  19. );
  20. $fruits[] = array(
  21.   'name' => 'Кокосы',
  22.   'price' => 130,
  23. );
  24.  
  25. class Fruiting {
  26.   public $fruits;
  27.   public $weights;
  28.  
  29.   public function setData($fruits, $weights) {
  30.     $this->fruits = $fruits;
  31.     $this->weights = $weights;
  32.   }
  33.  
  34.   private function getPrice($price, $weight) {
  35.     $result_price = $price * $weight;
  36.     return $result_price;
  37.   }
  38.  
  39.   public function getResult() {
  40.     //перебираем все фрукты
  41.     foreach ($this->fruits as $fruit) {
  42.       //перебираем все веса для каждого фрукта
  43.       foreach ($this->weights as $weight) {
  44.         echo $fruit['name'] . ' ' . $weight . 'кг стоят ' . self::getPrice($fruit['price'], $weight) . 'руб.<br />';
  45.       }
  46.     }
  47.   }
  48. }
  49.  
  50. $fruiting = new Fruiting();
  51. $fruiting->setData($fruits, array(5, 12, 14, 16, 22, 135, 150, 200, 254, 300, 400));
  52. $fruiting->getResult();
  53. ?>
Advertisement
Add Comment
Please, Sign In to add comment