Guest User

Untitled

a guest
Jul 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.09 KB | None | 0 0
  1. //статически заданные параметры:
  2.  
  3. $products=array('discount'=>0,'A'=>3,'B'=>2,'C'=>1,'D'=>1); //'discount'=>0 - текущая скидка (накапливаемый параметр, изначально равный нулю),
  4. // 'A'=>3 и т.п. - статически заданные группы товара в заказе с указанным количеством
  5. $discounts=array('d1','d2','d3','d4','d5','d6','d7');  //список действующих скидок
  6. $price_products=array('A'=>1,'B'=>10,'C'=>5,'D'=>7,'E'=>5,'F'=>4,'G'=>4,'H'=>3,'I'=>5,'J'=>10,'K'=>6,'L'=>10,'M'=>4);//прайс-лист
  7. //все вышеперечисленные данные целесообразно получать из БД ($discounts, $price_products) или POSTом/GETом($products) и для этого использовать типичные классы connectDB или getData
  8. class DiscountManager {  //класс управляющий пакетом действующих скидок
  9.     private $discounts=array();
  10.     private $products=array();
  11.     private $price_products=array();
  12.     public $obj_discount=array();
  13.     public $res_discount=array();
  14.     public $total_discounts;
  15.  
  16.     function __construct($discounts,$products,$price_products){
  17.         $this->discounts=$discounts;
  18.         $this->products=$products;
  19.         $this->price_products=$price_products;
  20.     }
  21.     function create_obj_discount(){ //функция, управляющая созданием и накопдением скидок
  22.  
  23.         foreach($this->discounts as $value){
  24.             $this->obj_discount=new $value($this->products,$this->price_products); //вызываем классы скидок
  25.             $this->products=$this->obj_discount->calculate_discount();  //получаем результаты выполнения функций классов скидок
  26.         }
  27.         $this->total_discount=$this->products['discount'];
  28.     }
  29.  
  30. }
  31. class order_Manager{  //класс, управляющий суммой заказа
  32.     protected $price_products=array();
  33.     protected $total_discount;
  34.     protected $products;
  35.     protected $sum;
  36.     protected $sum_without_discount;
  37.  
  38.     function __construct($price_products,$total_discount,$products){
  39.         $this->price_products=$price_products;
  40.         $this->total_discount=$total_discount;
  41.         $this->products=$products;
  42.     }
  43.     function sum_order(){ //находит общую сумму заказа
  44.         $this->sum=0;
  45.         foreach($this->products as $key=>$value){
  46.  
  47.             foreach($this->price_products as $key1=>$value1){
  48.                 if($key==$key1){
  49.                      $this->sum+=$value*$value1;
  50.  
  51.                 }
  52.  
  53.             }
  54.         }
  55.          $this->sum_without_discount=$this->sum-$this->total_discount;
  56.          $this->show_order_price();
  57.     }
  58.     function show_order_price(){    //выводим представление
  59.         $view='<h2>Заказ</h2> Товары: <ul>';
  60.         foreach($this->products as $key => $value){
  61.             if($key!='discount'){
  62.                 $view.='<li>'.$key.'</li>';
  63.             }
  64.         }
  65.        echo $view.='</ul>Общая сумма: '.$this->sum.'<br />Сумма скидки: '.$this->total_discount.'<br />Окончательная цена; '.$this->sum_without_discount;
  66.     }
  67. }
  68.  // далее - классы скидок
  69.  
  70. class d1  {
  71.     private $products;
  72.     private $price_products;
  73.     function __construct($products,$price_products){
  74.         $this->products=$products;
  75.         $this->price_products=$price_products;
  76.     }
  77.     function calculate_discount(){
  78.  
  79.  
  80.         if(array_key_exists('A',$this->products)&& array_key_exists('B',$this->products)
  81.         && $this->products['A']!=0 && $this->products['B']!=0){
  82.  
  83.             $pair=min(array($this->products['A'],$this->products['B']));
  84.             $this->products['discount']=$this->products['discount']+((($pair*($this->price_products['A']+$this->price_products['B']))/100)*10);
  85.             $this->products['A']=$this->products['A']-$pair;
  86.             $this->products['B']=$this->products['B']-$pair;
  87.             return $this->products;
  88.         }else{
  89.             return $this->products;
  90.         }
  91.  
  92.     }
  93. }
  94. class d2 {
  95.     private $products;
  96.     private $price_products;
  97.     function __construct($products,$price_products){
  98.         $this->products=$products;
  99.         $this->price_products=$price_products;
  100.     }
  101.     function calculate_discount(){
  102.         if(array_key_exists('D',$this->products)&& array_key_exists('E',$this->products)
  103.         && $this->products['D']!=0 && $this->products['E']!=0){
  104.             $pair=min(array($this->products['D'],$this->products['E']));
  105.             $this->products['discount']=$this->products['discount']+((($pair*($this->price_products['D']+$this->price_products['E']))/100)*10);
  106.             $this->products['D']=$this->products['D']-$pair;
  107.             $this->products['E']=$this->products['E']-$pair;
  108.             return $this->products;
  109.         }else{
  110.             return $this->products;
  111.         }
  112.     }
  113. }
  114. class d3 {
  115.     private $products;
  116.     private $price_products;
  117.     function __construct($products,$price_products){
  118.         $this->products=$products;
  119.         $this->price_products=$price_products;
  120.     }
  121.     function calculate_discount(){
  122.         if(array_key_exists('E',$this->products)&& array_key_exists('F',$this->products)&& array_key_exists('G',$this->products)
  123.         && $this->products['E']!=0 && $this->products['F']!=0 && $this->products['G']!=0){
  124.             $pair=min(array($this->products['E'],$this->products['F'],$this->products['G']));
  125.             $this->products['discount']=$this->products['discount']+((($pairEFG*($this->price_products['E']+$this->price_products['F']+$this->price_products['G']))/100)*5);
  126.             $this->products['E']=$this->products['E']-$pair;
  127.             $this->products['F']=$this->products['F']-$pair;
  128.             $this->products['G']=$this->products['G']-$pair;
  129.             return $this->products;
  130.         }else{
  131.             return $this->products;
  132.         }
  133.     }
  134. }
  135. class d4 {
  136.     private $products;
  137.     private $price_products;
  138.     function __construct($products,$price_products){
  139.         $this->products=$products;
  140.         $this->price_products=$price_products;
  141.     }
  142.     function calculate_discount(){
  143.         if(array_key_exists('A',$this->products)&& $this->products['A']!=0
  144.         && ((array_key_exists('K',$this->products)&& $this->products !=0)
  145.         xor (array_key_exists('L',$this->products)&& $this->products['L']!=0)
  146.         xor (array_key_exists('M',$this->products) && $this->products['M']!=0))){
  147.             if($this->products['K']){
  148.                 $key='K';
  149.             }elseif($this->products['L']){
  150.                 $key='L';
  151.             }else{
  152.                 $key='M';
  153.             }
  154.             $this->products['A']=$this->products['A']-$pair;
  155.             $this->products[$key]=$this->products[$key]-$pair;
  156.             $this->products['discount']=$this->products['discount']+($this->price_products[$key]/100*5);
  157.             return $this->products;
  158.         }else{
  159.             return $this->products;
  160.         }
  161.     }
  162. }
  163.  
  164. class d5 {
  165.     private $products;
  166.     private $price_products;
  167.     function __construct($products,$price_products){
  168.         $this->products=$products;
  169.         $this->price_products=$price_products;
  170.     }
  171.     function calculate_discount(){
  172.         if(!array_key_exists('A',$this->products)&& !array_key_exists('C',$this->products)&& count($this->products)==4 && $this->products['discount']!=0 ){
  173.             $price=0;
  174.             foreach($this->products as $key=>$value){
  175.                 if($key!='discount'){
  176.  
  177.                     foreach($this->price_products as $key1=>$value1){
  178.  
  179.                         if($key==$key1){
  180.                             $price_part=$value*$value1;
  181.                         }
  182.                         $prise+=$price_part;
  183.                     }
  184.                 }
  185.             }
  186.             $this->products['discount']=$this->products['discount']+((array_sum($price))/100*5);
  187.             return $this->discount5;
  188.         }else{
  189.             return $this->products;
  190.         }
  191.     }
  192. }
  193. class d6 {
  194.     private $products;
  195.     private $price_products;
  196.     function __construct($products,$price_products){
  197.         $this->products=$products;
  198.         $this->price_products=$price_products;
  199.     }
  200.     function calculate_discount(){
  201.         if(!array_key_exists('A',$this->products)&& !array_key_exists('C',$this->products)&& count($this->products)==5 && $this->products['discount']!=0 ){
  202.             $price=0;
  203.             foreach($this->products as $key=>$value){
  204.                 if($key!='discount'){
  205.  
  206.                     foreach($this->price_products as $key1=>$value1){
  207.  
  208.                         if($key==$key1){
  209.                             $price_part=$value*$value1;
  210.                         }
  211.                         $prise+=$price_part;
  212.                     }
  213.                 }
  214.             }
  215.             $this->products['discount']=$this->products['discount']+((array_sum($this->products))/100*10);
  216.             return $this->products;
  217.         }else{
  218.             return $this->products;
  219.         }
  220.     }
  221. }
  222. class d7 {
  223.     private $products;
  224.     private $price_products;
  225.     function __construct($products,$price_products){
  226.         $this->products=$products;
  227.         $this->price_products=$price_products;
  228.     }
  229.     function calculate_discount(){
  230.         if(!array_key_exists('A',$this->products)&& !array_key_exists('C',$this->products)&& count($this->products)==6 && $this->products['discount']!=0){
  231.             $price=0;
  232.             foreach($this->products as $key=>$value){
  233.                 if($key!='discount'){
  234.  
  235.                     foreach($this->price_products as $key1=>$value1){
  236.  
  237.                         if($key==$key1){
  238.                             $price_part=$value*$value1;
  239.                         }
  240.                         $prise+=$price_part;
  241.                     }
  242.                 }
  243.             }
  244.             $this->products['discount']=$this->products['discount']+((array_sum($this->products))/100*20);
  245.             return $this->products;;
  246.         }else{
  247.             return $this->products;
  248.         }
  249.     }
  250. }
  251. $disc=new DiscountManager($discounts,$products,$price_products);
  252. $disc->create_obj_discount();
  253. $order=new order_Manager($price_products,$disc->total_discount,$products);
  254. echo $order->sum_order();
  255.  
  256.  
  257. ?>
Add Comment
Please, Sign In to add comment