Advertisement
Guest User

Untitled

a guest
Mar 6th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.39 KB | None | 0 0
  1. <?php
  2. require 'autoload.php';
  3. class Cart implements CartInterface
  4. {
  5.     private $Warenkorb = array();
  6.  
  7.     public function addItem($sku, $itemPrice, $quantity)
  8.     {
  9.         $exists = false;
  10.  
  11.         foreach($this->Warenkorb as $item){
  12.             if($item['sku'] === $sku){
  13.                 $exists = true;
  14.             }
  15.         }
  16.  
  17.         if($exists){
  18.             //Anzahl der Quanität erhöhen.
  19.         }else{
  20.             $newItem = array("sku" => $sku, "itemPrice" => $itemPrice, "quantity" => $quantity);
  21.             array_push($this->Warenkorb, $newItem);
  22.         }
  23.     }
  24.  
  25.     public function removeItem($sku)
  26.     {
  27.         $exists = false;
  28.         for($d = 0; $d < count($this->Warenkorb); $d++){
  29.             if($this->Warenkorb[$d]['sku'] === $sku){
  30.                 unset($this->Warenkorb[$d]);
  31.                 $exists = true;
  32.             }
  33.         }
  34.  
  35.         if($exists === false){
  36.             throw new Exception('Dieser Artikel ist nicht in Ihrem Warenkorb');
  37.         }
  38.     }
  39.  
  40.     public function getItemCount()
  41.     {
  42.         $total = 0;
  43.  
  44.         foreach($this->Warenkorb as $item){
  45.             $total += $item['quantity'];
  46.         }
  47.  
  48.         return $total;
  49.     }
  50.  
  51.     public function getTotalPrice()
  52.     {
  53.         $total = 0;
  54.  
  55.         foreach($this->Warenkorb as $item){
  56.             $total += $item['itemPrice'];
  57.         }
  58.  
  59.         return $total;
  60.     }
  61.  
  62.     public function getQuantity($sku)
  63.     {
  64.         $quantity = 0;
  65.  
  66.         $exist = false;
  67.  
  68.         foreach($this->Warenkorb as $item){
  69.             if($item['sku'] === $sku){
  70.                 $quantity += $item['quantity'];
  71.                 $exist = true;
  72.             }
  73.         }
  74.  
  75.         if(!$exist){
  76.             throw new Exception('Dieser Artikel ist nicht in Ihrem Warenkorb');
  77.         }
  78.  
  79.         return $quantity;
  80.     }
  81.  
  82.     public function getItems()
  83.     {
  84.         $items = array();
  85.  
  86.         foreach($this->Warenkorb as $item){
  87.             $items[$item['sku']] = $item['quantity'];
  88.         }
  89.  
  90.  
  91.         return $items;
  92.     }
  93.  
  94.     public function compareTo(CartInterface $cart)
  95.     {
  96.         $same = true;
  97.  
  98.         $WarenkorbB = $cart->getItems();
  99.  
  100.         $WarenkorbA = $this->getItems();
  101.  
  102.         if(count(array_diff_key($WarenkorbA, $WarenkorbB)) !== 0 OR array_diff($WarenkorbA, $WarenkorbB) !== 0){
  103.             $same = false;
  104.         }
  105.  
  106.         return $same;
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement