Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.6 kB
11
Indexable
Never
<?php

class Product
{
    protected string $name;
    protected int $price;
    protected array $options;

    public function __construct(string $name, int $price, array $options)
    {
        $this->name = $name;
        $this->price = $price;
        $this->options = $options;
    }

    public function getName(): string
    {

        return $this->name;
    }
    public function getPrice(): int
    {

        return $this->price;
    }
    public function getOptions(): array
    {

        return $this->options;
    }
}



class Shirt extends Product
{
    private string $size;
    public $type='shirt';
    private array $allows = ['sm', 'md', 'lg', 'xlg', '2xlg'];
    private Product $product;
    public function __construct(string $size, Product $product)
    {
        if (in_array($size, $this->allows)) {


            $this->product = $product;
            $this->size = $size;
        }
    }
    public function getSize(): string
    {
        return $this->size;
    }
    public function getProduct(): Product
    {
        return $this->product;
    }
}

class Pants extends Product
{
    private int $size;
    private Product $product;
    public $type = 'pants';
    public function __construct(int $size, Product $product)
    {
        if ($this->check($size)) {
            $this->product = $product;
            $this->size = $size;
        }
    }
    private function check($size): bool
    {

        return $size > 30 and $size < 60;
    }
    public function getSize(): int
    {
        return $this->size;
    }
    public function getProduct(): Product
    {
        return $this->product;
    }
}

class Shop
{
    private array $repo = [];
    private int $income = 0;

    public function setIncome($income){
        $this->income+=$income;
    }
    public function changeProduct($id,$count){

        $this->repo[$id]['count']=$count;
    }
  
    public function getRepo(): array
    {
        return $this->repo;
    }
    public function getIncome(): int
    {
        return $this->income;
    }
    public function addProduct(Product $product, int $count): bool
    {
        if ($this->checkProductCount($count)) {
            array_push($this->repo, ['product'=>$product,'count'=>$count]);
                return true;
        }
        return false;
    }
    private function checkProductCount($count): bool
    {

        return $count > 0;
    }

    public function getSuggestion(string $type='', mixed $size=[], int $maxPrice=0, array $options = []): array
    {
        
        $suggestion=[];
            foreach($this->repo as $item){
                
                    if($size==$item['product']->getSize() and $type== $item['product']->type)
                        array_push($suggestion, $item['product']);
                
            }

            return $suggestion;
    }

    public function sell(int $id): Product
    {
        $this->repo[$id]['count'] -=1;
      
        $this->income+=$this->repo[$id]['product']->getProduct()->getPrice();

        return $this->repo[$id]['product'];

    }
}

$product = new Product('produce1', '24500', ['discount' => '5%', 'coupon' => 'IR-2550']);
$pants=new Pants(50,$product);
$shirt = new Shirt('md', $product);
$shop = new Shop();

$shop->addProduct($shirt, 30);

$shop->addProduct($pants, 30);

$shop->sell(0);
$shop->sell(0);
$shop->sell(1);
$shop->sell(1);
var_dump($shop);
die();


// $shop->getSuggestion();