添加相同选项时增加数量

发布于 2024-12-07 08:11:36 字数 1490 浏览 2 评论 0原文

我已经成功实现了 Cart Basket 的 OOP

一个项目包含 1 个或多个选项。

如果我再次添加相同的 OptionID,那么数量应该增加,而不是创建另一个 Option 对象。那怎么办呢?

如果我再次添加相同的 ItemID,它应该拒绝创建另一个 Item 对象。

还有我的OOP好吗?

    class Cart {

        public $item = array();

        public function addItem($id) {
            $item = new Item();
            $item->setItem($id);
            $this->item[] = $item;
            return $item;
        }

    }

    class Item {

        private $id = array();
        private $option = array();

        public function setItem($id) {
            $this->id = $id;
            return $this;
        }

        public function addOption($id) {
            $option = new Option();
            $option->setOption($id);
            $this->option[] = $option;
        }

    }

    class Option {

        private $quantity;
        private $id;

        public function setOption($id) {
            $this->quantity = 1;
            $this->id = $id;
            return $this;
        }

    }
    $cart = new Cart();

    //ItemID 10
    $item = $cart->addItem(10);

    //OptionID
    $item->addOption(11);
    $item->addOption(22);
    $item->addOption(22); //should increase quantity

   //It should not create another object because we already have Item Object of ItemID10
   $item = $cart->addItem(10); 

    $Shop = $cart;

    echo "<pre>";
    print_r($Shop);
    echo "</pre>";

I have managed to implement OOP of Cart Basket

An Item contain 1 or more options.

If I add same OptionID again then the number of quantity should increase rather than creating another Option Object. How can that be done?

If I add same ItemID again, it should refuse to create another Item object.

Also is my OOP is good?

    class Cart {

        public $item = array();

        public function addItem($id) {
            $item = new Item();
            $item->setItem($id);
            $this->item[] = $item;
            return $item;
        }

    }

    class Item {

        private $id = array();
        private $option = array();

        public function setItem($id) {
            $this->id = $id;
            return $this;
        }

        public function addOption($id) {
            $option = new Option();
            $option->setOption($id);
            $this->option[] = $option;
        }

    }

    class Option {

        private $quantity;
        private $id;

        public function setOption($id) {
            $this->quantity = 1;
            $this->id = $id;
            return $this;
        }

    }
    $cart = new Cart();

    //ItemID 10
    $item = $cart->addItem(10);

    //OptionID
    $item->addOption(11);
    $item->addOption(22);
    $item->addOption(22); //should increase quantity

   //It should not create another object because we already have Item Object of ItemID10
   $item = $cart->addItem(10); 

    $Shop = $cart;

    echo "<pre>";
    print_r($Shop);
    echo "</pre>";

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

伴随着你 2024-12-14 08:11:36

如果购物车中只能有一件具有唯一 id 的商品 - 那么请像这样重写 addItem() 方法:

 public function addItem($id) {
        $result = false;

        if (empty($this->item[$id])) {
            $item = new Item();
            $item->setItem($id);
            $this->item[$id] = $item;

            $result = $item;
        }

        return $result;
    }

addOption() 方法也是如此:

public function addOption($id) {
        if (empty($this->option[$id])) {
            $option = new Option();
            $option->setOption($id);
            $this->option[$id] = $option;
        }
        else {
            $this->option[$id]->setQuantity($this->option[$id]->getQuantity() + 1);
        }
    }

当然,您应该在 Option 中实现 setQuantity() 和 getQuantity() 方法班级。
希望这有帮助。

If you can have only one item with the unique id in the cart - then rewrite the addItem() method like this:

 public function addItem($id) {
        $result = false;

        if (empty($this->item[$id])) {
            $item = new Item();
            $item->setItem($id);
            $this->item[$id] = $item;

            $result = $item;
        }

        return $result;
    }

The same is with addOption() method:

public function addOption($id) {
        if (empty($this->option[$id])) {
            $option = new Option();
            $option->setOption($id);
            $this->option[$id] = $option;
        }
        else {
            $this->option[$id]->setQuantity($this->option[$id]->getQuantity() + 1);
        }
    }

And of course you should implement setQuantity() and getQuantity() methods in Option class.
Hope this helps.

眉目亦如画i 2024-12-14 08:11:36

部分重写了代码并进行了测试:

<?php
class Cart {

        public $items = array();

        public function addItem($id) {
            if(array_key_exists($id, $this->items)){
                $item = $this->items[$id];
            }else{
                $item = new Item($id);
                $this->items[$id] = &$item;
            } 
            return $item;
        }

    }

    class Item {

        private $id;
        private $options = array();

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

        public function addOption($id) {
            if(array_key_exists($id, $this->options)){
                $this->options[$id]->addQuantity();
            }else{
                $option = new Option($id);
                $this->options[$id] = $option;
            }           
        }

    }

    class Option {

        private $quantity;
        private $id;

        public function __construct($id) {
            $this->quantity = 1;
            $this->id = $id;
            return $this;
        }

        public function addQuantity()
        {
            $this->quantity++;
        }
    }

    $cart = new Cart();

    //ItemID 10
    $item = $cart->addItem(10);

    //OptionID
    $item->addOption(11);
    $item->addOption(22);
    $item->addOption(22); //should increase quantity

   //It should not create another object because we already have Item Object of ItemID10
   $item = $cart->addItem(10); 

    $Shop = $cart;

    echo "<pre>";
    print_r($Shop);
    echo "</pre>";
?>

Partialy rewrote the code and tested:

<?php
class Cart {

        public $items = array();

        public function addItem($id) {
            if(array_key_exists($id, $this->items)){
                $item = $this->items[$id];
            }else{
                $item = new Item($id);
                $this->items[$id] = &$item;
            } 
            return $item;
        }

    }

    class Item {

        private $id;
        private $options = array();

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

        public function addOption($id) {
            if(array_key_exists($id, $this->options)){
                $this->options[$id]->addQuantity();
            }else{
                $option = new Option($id);
                $this->options[$id] = $option;
            }           
        }

    }

    class Option {

        private $quantity;
        private $id;

        public function __construct($id) {
            $this->quantity = 1;
            $this->id = $id;
            return $this;
        }

        public function addQuantity()
        {
            $this->quantity++;
        }
    }

    $cart = new Cart();

    //ItemID 10
    $item = $cart->addItem(10);

    //OptionID
    $item->addOption(11);
    $item->addOption(22);
    $item->addOption(22); //should increase quantity

   //It should not create another object because we already have Item Object of ItemID10
   $item = $cart->addItem(10); 

    $Shop = $cart;

    echo "<pre>";
    print_r($Shop);
    echo "</pre>";
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文