Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- @author: Nguyen Truong Quy
- @note: That's good enough :))
- */
- /*
- Task:
- Write a basic eCommerce program in PHP that models a User, Product and ShoppingCart.
- * Products have a name and price.
- * Users have a name and email address, and associated ShoppingCart.
- * ShoppingCarts have an associated User and list of Products.
- * A User can add or remove a Product from their ShoppingCart.
- * A ShoppingCart must be able to provide a total price for its list of Products.
- The code must pass two tests:
- 1. Create a User "John Doe" with email address "john.doe@example.com", then add 2 "Apple" Products
- for $4.95 each and 1 "Orange" Product for $3.99. Check that the ShoppingCart total price is correct.
- 2. Create a User "John Doe" with email address "john.doe@example.com", then add 3 "Apple" products
- for $4.95, then remove 1 "Apple" product. Check that the ShoppingCart total price is correct.
- You may either use PHPUnit to run the tests, or a simple PHP script which can be run via the command
- line.
- Notes:
- * You should NOT use a full-featured framework (Laravel, CakePHP, etc), just plain PHP code.
- * No database, user interaction or other functionality is required (so no HTML/CSS/JavaScript); it just
- needs to be PHP code which runs and passes the tests.
- */
- class Product {
- private $name;
- private $price;
- function getName() {
- return $this->name;
- }
- function setName($name) {
- $this->name = $name;
- }
- function getPrice() {
- return $this->price;
- }
- function setPrice($price) {
- $this->price = $price;
- }
- }
- class User {
- private $name;
- private $email;
- private $shoppingCart;
- function getName() {
- return $this->name;
- }
- function setName($name) {
- $this->name = $name;
- }
- function getEmail() {
- return $this->email;
- }
- function setEmail($email) {
- $this->email = $email;
- }
- function getShoppingCart() {
- return $this->shoppingCart;
- }
- function setShoppingCart($shoppingCart) {
- $this->shoppingCart = $shoppingCart;
- }
- }
- class ShoppingCart {
- private $user;
- private $products = array();
- function getUser() {
- return $this->user;
- }
- function setUser($user) {
- $this->user = $user;
- }
- function removeProduct($productName) {
- $i = 0;
- foreach($this->products as $product) {
- if ($product->getName() == $productName) {
- unset($this->products[$i]);
- break;
- }
- $i++;
- }
- }
- function addProduct($product) {
- array_push($this->products, $product);
- }
- function getTotalPrice() {
- $sum = 0;
- foreach($this->products as $product) {
- $sum = $sum + $product->getPrice();
- }
- return $sum;
- }
- }
- /* 1. Create a User "John Doe" with email address "john.doe@example.com",
- then add 2 "Apple" Products for $4.95 each and 1 "Orange" Product for $3.99.
- Check that the ShoppingCart total price is correct */
- $user1 = new User();
- $user1->setName("John Doe");
- $user1->setEmail("john.doe@example.com");
- $user1->setShoppingCart(new ShoppingCart());
- $user1->getShoppingCart()->setUser($user1);
- $apple1 = new Product();
- $apple1->setName("Apple");
- $apple1->setPrice(4.95);
- $user1->getShoppingCart()->addProduct($apple1);
- $apple2 = new Product();
- $apple2->setName("Apple");
- $apple2->setPrice(4.95);
- $user1->getShoppingCart()->addProduct($apple2);
- $orange = new Product();
- $orange->setName("Orange");
- $orange->setPrice(3.99);
- $user1->getShoppingCart()->addProduct($orange);
- echo $user1->getShoppingCart()->getTotalPrice(). "\n";
- /* 2. Create a User "John Doe" with email address "john.doe@example.com", then add 3 "Apple" products
- for $4.95, then remove 1 "Apple" product. Check that the ShoppingCart total price is correct. */
- $user2 = new User();
- $user2->setName("John Doe");
- $user2->setEmail("john.doe@example.com");
- $user2->setShoppingCart(new ShoppingCart());
- $user2->getShoppingCart()->setUser($user2);
- $apple1 = new Product();
- $apple1->setName("Apple");
- $apple1->setPrice(4.95);
- $user2->getShoppingCart()->addProduct($apple1);
- $apple2 = new Product();
- $apple2->setName("Apple");
- $apple2->setPrice(4.95);
- $user2->getShoppingCart()->addProduct($apple2);
- $apple3 = new Product();
- $apple3->setName("Apple");
- $apple3->setPrice(4.95);
- $user2->getShoppingCart()->addProduct($apple3);
- $apple4 = new Product();
- $apple4->setName("Apple");
- $user2->getShoppingCart()->removeProduct($apple4->getName());
- echo $user2->getShoppingCart()->getTotalPrice();
Add Comment
Please, Sign In to add comment