Untitled
user_5151433
plain_text
3 years ago
4.1 kB
14
Indexable
<?php
class Product
{
protected string $name;
protected int $price;
protected array $options;
public function __construct(string $name, int $price, array $options)
{
$this->setName($name);
$this->setPrice($price);
$this->setOptions($options);
}
public function getName(){
return $this->name;
}
public function getPrice(){
return $this->price;
}
public function getOptions(){
return $this->options;
}
public function setName($name){
$this->name = $name;
}
public function setPrice($price){
$this->price = $price;
}
public function setOptions($options){
$this->options = $options;
}
}
class Shirt extends Product
{
private string $size;
private $allowSize = ['sm' , 'md' , 'lg' , 'xlg' , '2xlg'];
public function __construct(string $name, int $price, array $options , string $size)
{
parent::__construct($name, $price, $options);
$this->setSize($size);
}
public function setSize($size){
if (in_array($size , $this->allowSize)){
$this->size = $size;
}
}
public function getSize(){
return $this->size;
}
}
class Pants extends Product
{
private int $size;
public function __construct(string $name, int $price, array $options , int $size)
{
parent::__construct($name, $price, $options);
$this->setSize($size);
}
public function setSize($size){
if ($size > 30 && $size < 60 && $size % 2 == 0){
$this->size = $size;
}
}
public function getSize(){
return $this->size;
}
}
class Shop
{
private array $repo = [];
private int $income = 0;
public function addProduct(Product $product, int $count): bool
{
$lastRepoItem = end($this->repo);
if ($count >= 0){
$this->repo[] = [
'id' => empty($lastRepoItem) ? 1 : $lastRepoItem['id'] + 1 ,
'product' => $product,
'count' => $count
];
return true;
} else {
return false;
}
}
public function getSuggestion(string $type, mixed $size, int $maxPrice, array $options = []): array
{
$iMax = count($options);
$items = [];
foreach ($this->repo as $item) {
if (get_class($item['product']) !== $type) {
continue;
}
if ($item['product']->getSize() !== $size) {
continue;
}
if ($item['product']->getPrice() >= $maxPrice) {
continue;
}
// for ($i=0, $i < $iMax; $i++;) {
// if ($item->options[$i] !== $options[$i]) {
//
// }
// }
$items[] = $item;
}
return $items;
}
public function sell(int $id): Product
{
$product = [];
foreach ($this->repo as $key => $item) {
if ($item['id'] === $id) {
if ($item['count'] !== 0) {
$product = $item['product'];
$this->income += $product->getPrice();
$this->repo[$key]['count']--;
} else {
return 'mojood nist';
}
}
}
return $product;
}
}
$pants = new Pants('zirshalvar', '200', [
'color' => 'red',
'model' => 'dampa kesh'
], 38);
$pants2 = new Pants('zirshalvar', '500', [
'color' => 'red',
'model' => 'dampa kesh'
], 38);
$shirt = new Shirt('pirahan', '200', [
'color' => 'meshki',
'model' => ''
], 'sm');
$shop = new Shop();
$shop->addProduct($pants, 5);
$shop->addProduct($pants2, 10);
$shop->addProduct($shirt, 2);
echo '<pre>';
print_r($shop->sell(2));
print_r($shop);
echo '</pre>';
Editor is loading...