Jump to content
#FlattenTheCurve ×

weirdtr

New Members
  • Content Count

    4
  • Joined

  • Last visited

Community Reputation

0 Neutral

About weirdtr

  • Rank
    Newbie
  1. This is not homework, I found this code snippet on the forum. And it seemed very interesting to me,but I'm stuck and now this task doesn't give me rest. I want to see simple solution. share (this object array , basket object) Yes,only three different types of discounting . I tried ,but i didn't see simple solution . I hope that someone can help me. I tried in different ways : interface Product{ public function setProduct($product); } interface Discount{ public function setProducts($products); public function setPrice($price); public function getDiscount(); public function applyDiscount(); } class TotalDiscount implements Discount{ protected $products; protected $price; protected $discount; protected function setTotalDiscount(){//discount counting foreach ($this->products as $key=>$product) { if($this->isNotOneOf($product, 'A', 'C')){ if(count($this->products) >= 5 ){ $this->discount = 20; } else if (count($this->products) >= 4 ) { $this->discount = 10; } else if (count($this->products) >= 3 ) { $this->discount = 5; } } } } public function applyDiscount(){//goods discount $this->setTotalDiscount(); return $this->price * $this->discount/100; } protected function isNotOneOf($product, $p1, $p2){ if ($product != $p1 and $product != $p2) { return true;} } public function setProducts($products){//initialize product $this->products = $products; } public function setPrice($price){//initialize prize for product $this->price = $price; } public function getDiscount(){// %discount for product return $this->discount; } } class ProductDiscount implements Product, Discount{ protected $price;// price for product protected $discount;//%discount for product protected $product;//product protected $products;//array of all products public static $prod;//counter of products available for discount protected function setProductDiscount(){//discount counting foreach ($this->products as $key=>$product) { $this->discount = 0; while($this->products[$key]['q']>0) {//paired discounts if ($this->discount() and $this->product == 'A' and $product['type'] == 'B') { $this->discount = 10; ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'B' and $product['type'] == 'A') { $this->discount = 10; ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'D' and $product['type'] == 'E') { $this->discount = 5; ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'E' and $product['type'] == 'D') { $this->discount = 5; ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'K' and $product['type'] == 'A') { $this->discount = 5; ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'L' and $product['type'] == 'A') { $this->discount = 5; ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'M' and $product['type'] == 'A') { $this->discount = 5; ProductDiscount::$prod[$this->product]--; } $this->products[$key]['q']--; if ($this->discount) { break; } } if ($this->discount) { break; } } if (!$this->discount) {//These conditions must be specified separately, so that discounts are performed in the correct sequence if ($this->discount() and $this->product == 'E' and $this->isBothWith('F') and $this->isBothWith('G')) {// $this->discount = 5; //after checking the previous pairwise conditions ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'F' and $this->isBothWith('E') and $this->isBothWith('G')) { $this->discount = 5; ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'G' and $this->isBothWith('F') and $this->isBothWith('E')) { $this->discount = 5; ProductDiscount::$prod[$this->product]--; } else if ($this->discount() and $this->product == 'A' and $this->isWithAnyOf('K','L','M')) { $this->discount = 5; ProductDiscount::$prod[$this->product]--; } } } protected function discount(){ if(ProductDiscount::$prod[$this->product]>0){ return true; } else return false; } protected function isBothWith($p1){//Check for one product foreach ($this->products as $key=>$product) { if ($product['type'] == $p1) {return true;} } return false; } protected function isWithAnyOf($p1, $p2, $p3){//Check for few product foreach ($this->products as $key=>$product) { if ($product['type'] == $p1) { return true;} else if ($product['type'] == $p2) { return true;} else if ($product['type'] == $p3) { return true;} } return false; } public function setProducts($products){//initializing all products $this->products = $products; } public function setPrice($price){//initializing all products $this->price = $price; } public function setProduct($product){//initializing product $this->product = $product; } public function getDiscount(){//%discount for product return $this->discount; } public function applyDiscount(){ //price with dicount $this->setProductDiscount(); return $this->price * ((100 - $this->discount)/100); } public function getSaving(){ // product discount return $this->price * ($this->discount/100); } } $products = array( array('type'=>'A', 'price'=>100, 'q'=>1), array('type'=>'B', 'price'=>23, 'q'=>1), array('type'=>'C', 'price'=>2, 'q'=>1), array('type'=>'D', 'price'=>16, 'q'=>1), array('type'=>'E', 'price'=>10, 'q'=>1), array('type'=>'F', 'price'=>35, 'q'=>1), array('type'=>'G', 'price'=>63, 'q'=>1), array('type'=>'L', 'price'=>78, 'q'=>1), array('type'=>'K', 'price'=>33, 'q'=>1) ); $prod=array(); foreach($products as $key=>$product){ $prod[$product['type']]=$product['q']; } $total_price=0; $total_save=0; ProductDiscount::$prod=$prod; foreach ($products as $key=>$product) { $p = new ProductDiscount; $p->setProducts($products); $i=$products[$key]['q']; while($i>0) { $type = $product['type']; $price = $product['price']; $q = $product['q']; $p->setProduct($type); $p->setPrice($price); $cost = $p->applyDiscount(); $discount = $p->getDiscount(); $discount ? $discount = " with discount ".$discount."%" : $discount = ""; echo "Price of product ".$type." by price ".$price.$discount.": ".$cost."\n<br />"; $total_price += $price; $total_save += $p->getSaving(); $i--; } } echo "<br /><br />Price of all products: ".$total_price."\n<br />"."Save: ".$total_save."\n<br />"; echo "Price with discount: ".($total_price - $total_save)."\n<br /><br />"; $t = new TotalDiscount; $t->setProducts($products); $t->setPrice($total_price); $total_discount = $t->applyDiscount(); echo 'Saving with discount '.$t->getDiscount().'% for all order: '.$total_discount."\n<br />"; echo 'Total: '.($total_price-$total_discount) ; it's looks horrible; i need simple and quick method ;
  2. This is not homework, I found this code snippet on the forum. And it seemed very interesting to me,but I'm stuck and now this task doesn't give me rest. I want to see simple solution. share (this object array , basket object) Yes,only three different types of discounting . I tried ,but i didn't see simple solution . I hope that someone can help me.
  3. Hi! I need help! Stuck with this task Have a set of products for example: A, B, C, D, E, F, G. And also the conditions for the discount: - 5% for any 3 products, - 10% for any two goods + one of the goods "A" or "C" - 15% of the total cost of the composition of products above 100 $ Prices for goods are random, but not more than $ 100 Need a method that calculates the discount based on the entered product composition. I see it like : class test{ public $a = 10; public $b = 15; public $c = 20; public $d = 30; public $e = 40; public $f = 50; public $g = 60; // do i need it in array ↑ public function share($A, $B, $C, $D, $E, $F, $G) { $this->$a = $A; ......... if(){} //condition } } $value = new test(); $value->share // Maybe someone knows how to solve it in different way?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.