Jump to content
#FlattenTheCurve ×
weirdtr

Calculate percentage discount in php?

Recommended Posts

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?

 

 

 

 

 

 

 

 

 

 

Share this post


Link to post
Share on other sites

Is this homework?

 

Did it work when you tried it that way?

 

Where's the rest of your code?

 

What does "share" mean (given the description above)?

 

Will there only ever be the three different types of discounting?

Share this post


Link to post
Share on other sites

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.  

Share this post


Link to post
Share on other sites

Is this homework?

 

Did it work when you tried it that way?

 

Where's the rest of your code?

 

What does "share" mean (given the description above)?

 

Will there only ever be the three different types of discounting?

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 ;

Share this post


Link to post
Share on other sites

You dump 100+ lines of code at us and then say you want a "simle and quick method'? Uh.... no thanks.

 

How about you tell us in plain English without using "coding" what you want to do? Something like "user provides a price value and I need to show him what his discount will be based upon....". When one has an issue that has rapidly grown ugly it is usually better to ask for help as if you were starting from scratch.

Share this post


Link to post
Share on other sites

Here's one way


// products price list
$products =    [ 'A' => 100,
                 'B' => 23,
                 'C' => 2,
                 'D' => 16,
                 'E' => 10,
                 'F' => 35,
                 'G' => 63,
                 'H' => 50,
                 'I' => 45,
                 'J' => 75,
                 'K' => 33,
                 'L' => 78  ];
                 
$promotions  = [ 'A', 'C' ];

$baskets     = [
                  [ 'B' => 2 ]  ,
                  [ 'A' => 1, 'D' => 4 ] ,
                  [ 'A' => 1, 'D' => 1 ] ,
                  [ 'D' => 4, 'L' => 1 ] ,
                  [ 'C' => 3 ] ,
                  [ 'D' => 1, 'E' => 1, 'F' => 1] ,
                  [ 'A' => 1, 'L' => 1, 'K' => 2]
               ];

function total_price ($basket, $products)
{
    $total = 0;
    foreach ($basket as $prod => $qty) {
        $total += $qty * $products[$prod];
    }
    return $total;
}

function discount1 ($basket) 
{
    return count($basket) >= 3 ? 0.05 : 0;
}

function discount2 ($basket, $promotions)
{
    if (array_sum($basket) >= 3 && count(array_intersect($promotions, array_keys($basket))) > 0) {
        return 0.10;
    }
    return 0;
}

function discount3 ($totalprice)
{
    if ($totalprice > 100)
        return ($totalprice-100) * 0.15;
    else 
        return 0;
} 

//
// evaluate the test baskets
//
foreach ($baskets as $bskt) {
    $totalprice = total_price ($bskt, $products);
    $disc1 = $totalprice * discount1($bskt);
    $disc2 = $totalprice * discount2($bskt, $promotions);
    $disc3 = discount3($totalprice);
    $nett = $totalprice  - ($disc1 + $disc2 + $disc3);
    echo '<br><hr>';
    printf("<pre>Total price :\t%8.2f
Discount 1:\t%8.2f
Discount 2:\t%8.2f
Discount 3:\t%8.2f\t%8.2f
Nett price:\t\t\t%8.2f</pre>", $totalprice, $disc1, $disc2, $disc3, $disc1+$disc2+$disc3, $nett);
}

  • Like 2

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • 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.