Skip to content

Commit 7381551

Browse files
committed
Add "Shopping Cart" project
1 parent a5021f0 commit 7381551

File tree

4 files changed

+153
-0
lines changed

4 files changed

+153
-0
lines changed

Shopping Cart/Cart.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
4+
class Cart
5+
{
6+
/**
7+
* @var CartItem[]
8+
*/
9+
private array $items = [];
10+
11+
// TODO Generate getters and setters of properties
12+
13+
/**
14+
* Add Product $product into cart. If product already exists inside cart
15+
* it must update quantity.
16+
* This must create CartItem and return CartItem from method
17+
* Bonus: $quantity must not become more than whatever
18+
* is $availableQuantity of the Product
19+
*
20+
* @param Product $product
21+
* @param int $quantity
22+
* @return CartItem
23+
*/
24+
public function addProduct(Product $product, int $quantity): CartItem
25+
{
26+
//TODO Implement method
27+
}
28+
29+
/**
30+
* Remove product from cart
31+
*
32+
* @param Product $product
33+
*/
34+
public function removeProduct(Product $product)
35+
{
36+
//TODO Implement method
37+
}
38+
39+
/**
40+
* This returns total number of products added in cart
41+
*
42+
* @return int
43+
*/
44+
public function getTotalQuantity(): int
45+
{
46+
//TODO Implement method
47+
}
48+
49+
/**
50+
* This returns total price of products added in cart
51+
*
52+
* @return float
53+
*/
54+
public function getTotalSum(): float
55+
{
56+
//TODO Implement method
57+
}
58+
}

Shopping Cart/CartItem.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
4+
class CartItem
5+
{
6+
private Product $product;
7+
private int $quantity;
8+
9+
// TODO Generate constructor with all properties of the class
10+
// TODO Generate getters and setters of properties
11+
12+
public function increaseQuantity()
13+
{
14+
//TODO $quantity must be increased by one.
15+
// Bonus: $quantity must not become more than whatever is Product::$availableQuantity
16+
}
17+
18+
public function decreaseQuantity()
19+
{
20+
//TODO $quantity must be increased by one.
21+
// Bonus: Quantity must not become less than 1
22+
}
23+
}

Shopping Cart/Product.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
4+
class Product
5+
{
6+
private int $id;
7+
private string $title;
8+
private float $price;
9+
private int $availableQuantity;
10+
11+
// TODO Generate constructor with all properties of the class
12+
// TODO Generate getters and setters of properties
13+
14+
/**
15+
* Add Product $product into cart. If product already exists inside cart
16+
* it must update quantity.
17+
* This must create CartItem and return CartItem from method
18+
* Bonus: $quantity must not become more than whatever
19+
* is $availableQuantity of the Product
20+
*
21+
* @param Cart $cart
22+
* @param int $quantity
23+
* @return CartItem
24+
*/
25+
public function addToCart(Cart $cart, int $quantity): CartItem
26+
{
27+
//TODO Implement method
28+
}
29+
30+
/**
31+
* Remove product from cart
32+
*
33+
* @param Cart $cart
34+
*/
35+
public function removeFromCart(Cart $cart)
36+
{
37+
//TODO Implement method
38+
}
39+
}

Shopping Cart/index.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
require_once "Product.php";
4+
require_once "Cart.php";
5+
require_once "CartItem.php";
6+
7+
$product1 = new Product(1, "iPhone 11", 2500, 10);
8+
$product2 = new Product(2, "M2 SSD", 400, 10);
9+
$product3 = new Product(3, "Samsung Galaxy S20", 3200, 10);
10+
$cart = new Cart();
11+
$cartItem1 = $cart->addProduct($product1, 1);
12+
$cartItem2 = $product2->addToCart($cart, 1);
13+
echo "Number of items in cart: ".PHP_EOL;
14+
echo $cart->getTotalQuantity().PHP_EOL; // This must print 2
15+
echo "Total price of items in cart: ".PHP_EOL;
16+
echo $cart->getTotalSum().PHP_EOL; // This must print 2900
17+
18+
$cartItem2->increaseQuantity();
19+
$cartItem2->increaseQuantity();
20+
21+
echo "Number of items in cart: ".PHP_EOL;
22+
echo $cart->getTotalQuantity().PHP_EOL; // This must print 4
23+
24+
echo "Total price of items in cart: ".PHP_EOL;
25+
echo $cart->getTotalSum().PHP_EOL; // This must print 3700
26+
27+
$cart->removeProduct($product1);
28+
29+
echo "Number of items in cart: ".PHP_EOL;
30+
echo $cart->getTotalQuantity().PHP_EOL; // This must print 4
31+
32+
echo "Total price of items in cart: ".PHP_EOL;
33+
echo $cart->getTotalSum().PHP_EOL; // This must print 3700

0 commit comments

Comments
 (0)