Advertisement
HristoBaychev

shop

Apr 20th, 2023
1,002
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.98 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. Създайте основата за електронен магазин - клас Product, Cart и Payment.
  5. Различните типове продукти ще бъдат обединени от общ абстрактен клас,
  6. съдържащ общите им методи и пропъртита. Различните Payment методи ще
  7. имат общ интерфейс. За да е успешно завършена задачата:
  8. - Създайте интерфейс и множество класове, които го имплементират: Нека
  9. интерфейсът бъде с един или повече методи и след това създайте
  10. множество класове, които го прилагат.
  11. - Създадат абстрактен клас с един или повече конкретни методи и един
  12. или повече абстрактни методи. След това трябва да създадате един или
  13. повече класове, които наследяват този абстрактен клас и прилагат
  14. неговите абстрактни методи.
  15. - Създайте final клас със статични методи: нагледен пример за енумератор
  16. - Създайте йерархия на класове с наследяване, интерфейси и абстрактни
  17. класове
  18.  
  19. 1. интерфейс - плащане
  20. 1.1. тип плащане:
  21. - картово
  22. - в кеш
  23. 2. интерфейс - валута
  24. 2.1 тип валута
  25.  
  26. 3. Абастрактен клас продукт
  27. -име
  28. -цена
  29. 3.1 абстрактна функция - описание
  30.  
  31. 4. клас женски дрехи
  32. - нови възможности
  33. - вкарване на стойностите от родителският клас
  34. - извикване на абстрактният клас за описание
  35.  
  36. 5. клас мъжки дрехи
  37. - нови възможности
  38. - вкарване на стойностите от родителски клас
  39. - извикване на абстрактният клас за описание
  40.  
  41. 6. продукти за хегиена
  42. - нови възможности
  43. - вкарване на стойностите от родителски клас
  44. - извикване на абстранкният клас за описание
  45.  
  46. 7. клас кошница
  47. - нови възможности
  48. - вкарване на интерфейс плащане с карта или кеш
  49. - вкарване на интерфейс за валута
  50. - добавяне на продукта в кошница (в масив)
  51. - сетване на метода за плащане
  52. - сетване на валутата
  53. - сумиране на сметката (връщане на сумата и валутата)
  54.  
  55. 8. създаване на обекти и извикването на методите:
  56. - женска дреха
  57. - мъжка дреха
  58. - нещо за хигиената
  59. - добавяне в картата, като се прави нова
  60. - добавяне на продуктите в нея
  61. - извикване на плащане
  62. - добавяне начин на плащане
  63. - добавяне на валута
  64. - какво има в количката
  65. - общата сума в количката
  66. - финални методи за плащане
  67.  
  68. */
  69.  
  70.  
  71.  
  72. interface PayMethod {        // common payment - interface // connect all
  73.     public function pay(float $amount);
  74. }
  75.  
  76. class CardPay implements PayMethod {   // first type of payment
  77.     public function pay(float $amount):float {
  78.         return $amount;
  79.     }
  80. }
  81.  
  82. class CashPay implements PayMethod {  // second type of payment
  83.     public function pay(float $amount):float {
  84.         return $amount;
  85.     }
  86. }
  87.  
  88.  
  89. interface Sign {
  90.     public function currency(string $symbol);
  91. }
  92.  
  93. class BgnSign implements Sign {
  94.     public function currency(string $symbol) {
  95.         return $symbol;
  96.     }
  97. }
  98.  
  99.  
  100. abstract class Product {   // common product --- like a items, because this is abstract for one shop
  101.     protected $name;       // we can have shoes and dress, and etc.
  102.     public $price;
  103.  
  104.     public function __construct(string $name, float $price) {  // main construct or parent::
  105.         $this->name = $name;
  106.         $this->price = $price;
  107.     }
  108.  
  109.     abstract public function getDescription(): string;   // description ... other common
  110.        
  111.  
  112.     public function getPrice(): float {  // take the price
  113.         return $this->price;
  114.     }
  115. }
  116.  
  117. class womenClothesDress extends Product {
  118.     private $color;   // different on woman store - color
  119.     private $made;
  120.     private $material;
  121.  
  122.     public function __construct(string $name, float $price, string $color, string $made, string $material) {  // new __construct
  123.         parent::__construct($name, $price); // from abstract  Products
  124.         $this->color = $color;
  125.         $this->made = $made;
  126.         $this->material = $material;
  127.     }
  128.  
  129.     public function getDescription(): string {   // other abstract Products
  130.         return "Dress: $this->name in $this->color - $this->price. Made in: $this->made. Material - $this->material".PHP_EOL;
  131.     }
  132. }
  133.  
  134. class manClothesShoes extends Product {
  135.     private $size;
  136.  
  137.     public function __construct(string $name, float $price, int $size) {  // same
  138.         parent::__construct($name, $price);  // same
  139.         $this->size = $size;
  140.     }
  141.  
  142.     public function getDescription(): string {
  143.         return "Shoes: $this->name in size $this->size - $this->price".PHP_EOL; // same
  144.     }
  145. }
  146.  
  147. class Hygiene extends Product {
  148.     private $brand;
  149.     private $smell;
  150.  
  151.     public function __construct(string $name, float $price, string $brand, string $smell){
  152.         parent::__construct($name, $price);
  153.         $this->brand = $brand;
  154.         $this->smell = $smell;
  155.     }
  156.  
  157.     public function getDescription(): string {
  158.         return "Soap is: $this->name from $this->brand for $this->price and smell like $this->smell".PHP_EOL;
  159.     }
  160. }
  161.  
  162. class Cart {  // shoping cart or bag
  163.     private $items = [];
  164.     private PayMethod $payMethod;  // implement paymethod
  165.     private Sign $sign;
  166.  
  167.     public function addProduct(Product $product) {  // add product in bascet
  168.         $this->items[] = $product;  // collect items in array
  169.     }
  170.  
  171.     public function setPayment(PayMethod $payMethod) {
  172.         $this->payMethod = $payMethod;
  173.     }
  174.     public function setSign(Sign $sign){
  175.         $this->sign = $sign;
  176.     }
  177.  
  178.     public function checkout() {  // calculate total sum
  179.         $total = 0;  
  180.         foreach ($this->items as $item) {
  181.             $total += $item->price;
  182.         }
  183.  
  184.         return $this->payMethod->pay($total) . ' ' . $this->sign->currency('BGN');
  185.     }
  186.     public function getItems(): array {
  187.         return $this->items;
  188.     }
  189. }
  190.  
  191. final class PayMethods {
  192.     const CREDITCARD = "CREDIT CARD";
  193.     const CASH = "CASH";
  194.  
  195.     public static function getAll(): array {
  196.         return [self::CREDITCARD, self::CASH];
  197.     }
  198. }
  199.  
  200.  
  201. $dress = new womenClothesDress("Summer dress", 39.99, "blue", "Italy", "Cotton");
  202. $shoes = new manClothesShoes("Sneakers", 59.99, 42);
  203. $flower = new Hygiene("Dove", 10.00 , "Procter and Gamble", "Chrysanthemums");
  204. echo $dress->getDescription();
  205. echo $shoes->getDescription();
  206. echo $flower->getDescription();
  207.  
  208. $cart = new Cart();
  209.  
  210.  
  211. $cart->addProduct($dress);
  212. $cart->addProduct($shoes);
  213. $cart->addProduct($flower);
  214.  
  215.  
  216. $creditCard = new cardPay();
  217. $cart->setPayment($creditCard);
  218. $bgnSign = new BgnSign();
  219. $cart->setSign($bgnSign);
  220.  
  221.  
  222. $cart->checkout();
  223. echo "Cart Contents: ".PHP_EOL;
  224. print_r($cart->getItems());
  225.  
  226.  
  227. $totalFinal = $cart->checkout();
  228. echo "Total: " . $totalFinal.PHP_EOL;
  229.  
  230. $allPaymentMethods = PayMethods::getAll();
  231. foreach($allPaymentMethods as $paymentMethod) {
  232.     echo $paymentMethod . PHP_EOL;
  233. }
  234.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement