2017-08-14 74 views
-1

我試圖創建一個相當簡單的 PHP結帳系統。創建一個PHP結帳系統

的我使用信息表:

產品編號           名稱                   價格
              SW1                        夾心             £3.11
              CL1                          巧克力           £5.00
              CF1                              咖啡                 £11.23


我的代碼是:

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(); 


我希望做的是增加一個定價規則,一些項目。例如,我希望三明治可以買一送一和巧克力,以獲得批量購買折扣。 不知道它會工作,但我想我應該增加這樣的:

$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. 
} 


任何幫助,將不勝感激。另外如果有人知道我如何測試這段代碼,那會很棒。

+2

試圖建立自己的電子商務系統,如果你打算在生產中使用它可能是一個非常糟糕的主意。你最好擴展現有的系統。 此外,您應該至少編寫代碼,然後在遇到困難時尋求幫助,而不是要求人們爲您編寫代碼。 – Difster

回答

0

我會創建一個可以應用於產品和/或結帳的「折扣」界面。例如:

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

非常感謝你!我覺得我很瞭解這一點。只是想知道是否可以結賬的東西是這樣的:$ checkout-> scan($ sandwich,$ chocolate);或者它必須更​​像$ checkout-> scan($ sandwich); $ checkout->掃描($巧克力);謝謝! – Developer

+0

這肯定是可能的,有兩種方法......要麼傳遞一個產品數組來掃描,要麼使用'func_get_args'來檢索傳遞給方法的參數列表。 – Geoffrey