Guest User

Untitled

a guest
May 27th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4. @author: Nguyen Truong Quy
  5. @note: That's good enough :))
  6.  
  7. */
  8.  
  9.  
  10. /*
  11. Task:
  12. Write a basic eCommerce program in PHP that models a User, Product and ShoppingCart.
  13. * Products have a name and price.
  14. * Users have a name and email address, and associated ShoppingCart.
  15. * ShoppingCarts have an associated User and list of Products.
  16. * A User can add or remove a Product from their ShoppingCart.
  17. * A ShoppingCart must be able to provide a total price for its list of Products.
  18. The code must pass two tests:
  19. 1. Create a User "John Doe" with email address "john.doe@example.com", then add 2 "Apple" Products
  20. for $4.95 each and 1 "Orange" Product for $3.99. Check that the ShoppingCart total price is correct.
  21. 2. Create a User "John Doe" with email address "john.doe@example.com", then add 3 "Apple" products
  22. for $4.95, then remove 1 "Apple" product. Check that the ShoppingCart total price is correct.
  23. You may either use PHPUnit to run the tests, or a simple PHP script which can be run via the command
  24. line.
  25. Notes:
  26. * You should NOT use a full-featured framework (Laravel, CakePHP, etc), just plain PHP code.
  27. * No database, user interaction or other functionality is required (so no HTML/CSS/JavaScript); it just
  28. needs to be PHP code which runs and passes the tests.
  29. */
  30.  
  31. class Product {
  32. private $name;
  33. private $price;
  34.  
  35. function getName() {
  36. return $this->name;
  37. }
  38.  
  39. function setName($name) {
  40. $this->name = $name;
  41. }
  42.  
  43. function getPrice() {
  44. return $this->price;
  45. }
  46.  
  47. function setPrice($price) {
  48. $this->price = $price;
  49. }
  50. }
  51.  
  52. class User {
  53. private $name;
  54. private $email;
  55. private $shoppingCart;
  56.  
  57. function getName() {
  58. return $this->name;
  59. }
  60.  
  61. function setName($name) {
  62. $this->name = $name;
  63. }
  64.  
  65. function getEmail() {
  66. return $this->email;
  67. }
  68.  
  69. function setEmail($email) {
  70. $this->email = $email;
  71. }
  72.  
  73. function getShoppingCart() {
  74. return $this->shoppingCart;
  75. }
  76.  
  77. function setShoppingCart($shoppingCart) {
  78. $this->shoppingCart = $shoppingCart;
  79. }
  80. }
  81.  
  82. class ShoppingCart {
  83. private $user;
  84. private $products = array();
  85.  
  86. function getUser() {
  87. return $this->user;
  88. }
  89.  
  90. function setUser($user) {
  91. $this->user = $user;
  92. }
  93.  
  94. function removeProduct($productName) {
  95. $i = 0;
  96. foreach($this->products as $product) {
  97. if ($product->getName() == $productName) {
  98. unset($this->products[$i]);
  99. break;
  100. }
  101. $i++;
  102. }
  103. }
  104.  
  105. function addProduct($product) {
  106. array_push($this->products, $product);
  107. }
  108.  
  109. function getTotalPrice() {
  110. $sum = 0;
  111. foreach($this->products as $product) {
  112. $sum = $sum + $product->getPrice();
  113. }
  114. return $sum;
  115. }
  116. }
  117.  
  118. /* 1. Create a User "John Doe" with email address "john.doe@example.com",
  119. then add 2 "Apple" Products for $4.95 each and 1 "Orange" Product for $3.99.
  120. Check that the ShoppingCart total price is correct */
  121.  
  122. $user1 = new User();
  123. $user1->setName("John Doe");
  124. $user1->setEmail("john.doe@example.com");
  125. $user1->setShoppingCart(new ShoppingCart());
  126. $user1->getShoppingCart()->setUser($user1);
  127.  
  128. $apple1 = new Product();
  129. $apple1->setName("Apple");
  130. $apple1->setPrice(4.95);
  131. $user1->getShoppingCart()->addProduct($apple1);
  132.  
  133. $apple2 = new Product();
  134. $apple2->setName("Apple");
  135. $apple2->setPrice(4.95);
  136. $user1->getShoppingCart()->addProduct($apple2);
  137.  
  138. $orange = new Product();
  139. $orange->setName("Orange");
  140. $orange->setPrice(3.99);
  141. $user1->getShoppingCart()->addProduct($orange);
  142.  
  143. echo $user1->getShoppingCart()->getTotalPrice(). "\n";
  144.  
  145. /* 2. Create a User "John Doe" with email address "john.doe@example.com", then add 3 "Apple" products
  146. for $4.95, then remove 1 "Apple" product. Check that the ShoppingCart total price is correct. */
  147.  
  148. $user2 = new User();
  149. $user2->setName("John Doe");
  150. $user2->setEmail("john.doe@example.com");
  151. $user2->setShoppingCart(new ShoppingCart());
  152. $user2->getShoppingCart()->setUser($user2);
  153.  
  154. $apple1 = new Product();
  155. $apple1->setName("Apple");
  156. $apple1->setPrice(4.95);
  157. $user2->getShoppingCart()->addProduct($apple1);
  158.  
  159. $apple2 = new Product();
  160. $apple2->setName("Apple");
  161. $apple2->setPrice(4.95);
  162. $user2->getShoppingCart()->addProduct($apple2);
  163.  
  164. $apple3 = new Product();
  165. $apple3->setName("Apple");
  166. $apple3->setPrice(4.95);
  167. $user2->getShoppingCart()->addProduct($apple3);
  168.  
  169. $apple4 = new Product();
  170. $apple4->setName("Apple");
  171. $user2->getShoppingCart()->removeProduct($apple4->getName());
  172.  
  173. echo $user2->getShoppingCart()->getTotalPrice();
Add Comment
Please, Sign In to add comment