L
L
Luigi2020-09-19 23:16:34
PHP
Luigi, 2020-09-19 23:16:34

Implement the remove method in the Cart class to remove products. The method should take as a parameter the name of the product to be removed?

I don’t understand how to pass the name of the product to the remove() method , for which there is a getter in the Product class
Here is my code:

class Product
{
    private $title;
    private $price;
    private $quantity;

    public function __construct($title,$price,$quantity) {
        $this->title = $title;
        $this->price = $price;
        $this->quantity = $quantity;
    }

    public function getProductTitle() { return $this->title; }
    public function getProductPrice() { return $this->price; }
    public function getProductQuantity() { return $this->quantity; }

    public function getCost() {
        return $this->price * $this->quantity;
    }
}

class Cart
{
    private $products = [];

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

    public function removeProduct() {
        // 
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daria Motorina, 2020-09-19
Wampa @Annikangl

There is no trick here

public function removeProduct(string $productName) {
        // 
    }

Inside, find this product by name in the array of products and delete

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question