2017-08-14 7 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(); 


は、いくつかの項目に価格設定ルールを追加することです。たとえば、サンドイッチは1つ買って1つ買わないようにし、チョコレートには1つのバルク購入割引を付けることができます。 それが仕事だろうが、私は、私はこのような何かを追加する必要があります考えているわからない場合:

$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

実際に使用する予定の場合は、自分のeコマースシステムを構築しようとするのはおそらく本当に悪い考えです。あなたは既存のシステムを拡張する方が良いです。 また、少なくともコードを書いた後、立ち往生したときに助けを求めて、コードを書くように人々に依頼しないでください。 – 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-> scan($チョコレート);ありがとう! – Developer

+0

2つのメソッド...スキャンする製品の配列を渡すか、 'func_get_args'を使ってメソッドに渡される引数のリストを取得することも可能です。 – Geoffrey