2017-08-14 2 views
-1

Ich versuche, ein ziemlich einfach PHP-Checkout-System zu erstellen.Erstellen eines PHP-Checkout-System

Die Tabelle der Informationen Ich verwende ist:

Produktcode             Namen                     Preis
              SW1                         Sandwich               £ 3,11
              CL1                           Schokolade             £ 5,00
              CF1                               Kaffee                   £ 11,23


Mein Code ist:

class Product { 
    public $name; 
    public $price; 

    public function __construct($name, $price) { 
     $this->item = $name; 
     $this->price = $price; 
    } 
} class Checkout { 
    protected $shopping_cart; 

    public function scan(Product $product) { 
     $shopping_cart[] = $product; 
    } 

    public function total() { 
    // Goes through the shopping cart and sums up the price of the contents 
     return array_reduce(
      $this->shopping_cart, 
      function($total, $item) { 
       return $total + $item->price; 
      }, 
      0 
     ); 
    } 
} 

$pay = new Checkout(); 
$pay->scan(new Product("Sandwich", 3.11)); 
$pay->scan(new Product("Chocolate", 5)); 
$pay->scan(new Product("Coffee", 11.23)); 
$price = $pay->total(); 


Was Ich mag würde tun, ist eine Preisregel auf einige Elemente hinzuzufügen. Zum Beispiel möchte ich, dass Sandwiches "Buy-One-Get-One-Free" und Pralinen einen Mengenrabatt haben. Nicht sicher, ob es funktionieren würde, aber ich denke ich so etwas wie dies sollte hinzufügen:

$SW1 = new Checkout("Sandwich", 3.11); 
$CL1 = new Checkout("Chocolate", 5); 
$CF1 = new Checkout("Coffee", 11.23); 

if(scan($SW1 % 2 == 0)) { 
    $price = $price/2; 
} 
elseif(scan($SW1 == 1)) { 
} 
else { 
    $price = $price/2 + $price; 
} 

if(scan($CL1 >= 3 && $CL1 % 3 == 0)) { 
    $price = $price - .50; 
} 
else { 
// insert logic to make sure purchases that include more than 3 items, though not divisible by 3, are also discounted accordingly. 
} 


Jede mögliche Hilfe würde geschätzt. Auch wenn jemand weiß, wie ich diesen Code testen könnte, wäre das großartig ☺.

+2

Der Versuch, Ihr eigenes E-Commerce-System zu bauen, wenn Sie es in der Produktion verwenden werden, ist wahrscheinlich eine sehr schlechte Idee. Sie sind besser dran, ein bestehendes System zu erweitern. Sie sollten auch mindestens den Code schreiben und dann um Hilfe bitten, wenn Sie stecken bleiben, nicht die Leute bitten, Ihren Code für Sie zu schreiben. – Difster

Antwort

0

Ich würde eine 'Rabatt' Schnittstelle erstellen, die auf das Produkt und/oder die Kasse angewendet werden kann.Zum Beispiel:

interface IDiscount { 
    public function apply(&$amount); 
} 

class Discount_HalfOff implements IDiscount { 
    public function apply(&$amount) { 
    $amount /= 2; 
    } 
} 

class Discount_FixedAmount implements IDiscount { 
    private $amount; 

    public function __construct($amount) { 
    $this->amount = $amount; 
    } 

    public function apply(&$amount) { 
    $amount -= $this->amount; 
    } 
} 

class Product { 
    private $discounts = []; 

    public function __construct($name, $price) { 
    $this->item = $name; 
    $this->price = $price; 
    } 

    public function addDiscount(IDiscount $discount) { 
    $this->discounts[] = $discount; 
    } 

    public function getPrice() { 
    $price = $this->price; 
    foreach($this->discounts as $discount) 
     $discount->apply($price); 
    return $price;   
    } 
} 

class Checkout { 
    private $contents = []; 
    private $discounts = []; 

    public function addDiscount(IDiscount $discount) { 
    $this->discounts[] = $discount; 
    } 

    public function scan(Product $product) { 
    $this->contents[] = $product; 
    } 

    public function getCount() { 
    return count($this->contents); 
    } 

    public function getTotal() { 
    $ttl = 0; 
    foreach($this->contents as $product) 
     $ttl += $product->getPrice(); 

    foreach($this->discounts as $discount) 
     $discount->apply($ttl); 

    return $ttl; 
    } 
} 

$sandwich = new Product('Sandwich', 3.11); 
$sandwich->addDiscount(new Discount_HalfOff()); 

$checkout = new Checkout(); 
$checkout->scan($sandwich); 

// this logic could be included into the Checkout class directly, depends on your use case. 
if ($checkout->getCount() > 3) { 
    if ($checkout->getCount() % 3 == 0) 
    $checkout->addDiscount(new Discount_FixedAmount(0.50)); 
    } else { 
    // insert logic to make sure purchases that include more than 3 items, though not divisible by 3, are also discounted accordingly. 
    } 
} 

echo $checkout->getTotal() . "\n"; 
+0

Vielen Dank dafür! Ich denke, ich verstehe das gut genug. Ich frage mich nur, ob es für die Kasse möglich wäre, etwas wie: $ checkout-> scan ($ sandwich, $ chocolate); oder müsste es eher wie $ checkout-> scan ($ sandwich) sein; $ checkout-> scan ($ chocolate); Vielen Dank! – Developer

+0

Es ist sicherlich möglich, zwei Methoden ... übergeben entweder ein Array von Produkten zu scannen, oder verwenden Sie 'func_get_args', um die Liste der an die Methode übergebenen Argumente abzurufen. – Geoffrey