Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. # Interview Quizz: Cashier
  2.  
  3. A cashier in a shop need a program that will print out a receipt for a given order. And you're going to implement that.
  4.  
  5. ## Q1: Simple Printing
  6.  
  7. We're going to use some objects interaction to represent a payment process, and then print a receipt.
  8.  
  9. ```python
  10. # python version
  11. class Order(object):
  12. def __init__(self, *products):
  13. # Not implemented
  14. pass
  15. def statement(self):
  16. # Not implemented
  17. pass
  18.  
  19. class Product(object):
  20. def __init__(self, name, price):
  21. self.name = name
  22. self.price = price
  23.  
  24. def __repr__(self):
  25. return '<{0} {1:.2f}>'.format(self.name, self.price)
  26.  
  27.  
  28. class Water(Product):
  29. def __init__(self):
  30. super(Water, self).__init__('water', 5)
  31.  
  32.  
  33. class Coffee(Product):
  34. def __init__(self):
  35. super(Coffee, self).__init__('coffee', 20.0)
  36.  
  37.  
  38. class Milk(Product):
  39. def __init__(self):
  40. super(Milk, self).__init__('milk', 15.0)
  41. ```
  42. ```java
  43. // java version
  44. public static class Product {
  45. public String name;
  46. public double price;
  47.  
  48. public String toString() {
  49. return String.format("%s: %2.2f", this.name, this.price);
  50. }
  51. }
  52.  
  53. public static class Water extends Product {
  54. public Water() {
  55. this.name = "water";
  56. this.price = 5.0;
  57. }
  58. }
  59.  
  60. public static class Coffee extends Product {
  61. public Coffee() {
  62. this.name = "coffee";
  63. this.price = 20.0;
  64. }
  65. }
  66.  
  67. public static class Milk extends Product {
  68. public Milk() {
  69. this.name = "milk";
  70. this.price = 15.0;
  71. }
  72. }
  73.  
  74. public static class Order {
  75. public Order(Product... products) {
  76. // Not implemented
  77. }
  78.  
  79. public double charge() {
  80. // Not implemented
  81. return 0.0;
  82. }
  83.  
  84. public String statement() {
  85. // Not implemented
  86. return "Not implemented";
  87. }
  88. }
  89. ```
  90.  
  91. Here's how we represent an order and get the receipt
  92.  
  93. ```python
  94. # python version
  95. o = Order(
  96. Water(),
  97. Milk(),
  98. Water(),
  99. Milk(),
  100. Coffee(),
  101. Coffee(),
  102. Coffee(),
  103. )
  104.  
  105. print(o.statement())
  106. ```
  107. ```java
  108. // java version
  109. Order o = new Order(
  110. new Water(),
  111. new Milk(),
  112. new Water(),
  113. new Milk(),
  114. new Coffee(),
  115. new Coffee(),
  116. new Coffee()
  117. );
  118.  
  119. System.out.println(o.statement());
  120. ```
  121.  
  122. Output shoud looks like this, the order of products must be the same as input: `water`, `milk`, `coffee` (water is the first item and then milk then coffee)
  123.  
  124. ```
  125. NAME PRICE
  126. water 5.00 x 2 = 10.00
  127. milk 15.00 x 2 = 30.00
  128. coffee 20.00 x 3 = 60.00
  129. SUM 100.00
  130. ```
  131.  
  132. ## Q2: Sales Promotion
  133.  
  134. One day this shop want to have a sales promotion, all products will have 10% off.
  135.  
  136. Try to change your program to make the output looks like this.
  137.  
  138. ```
  139. NAME PRICE
  140. water 5.00 x 2 x 90.00%(10% off) = 9.00
  141. milk 15.00 x 2 x 90.00%(10% off) = 27.00
  142. coffee 20.00 x 3 x 90.00%(10% off) = 54.00
  143. SUM 90.00
  144. ```
  145.  
  146. ## Q3: More Than One Discount Strategy
  147.  
  148. The milk in this shop is going to past-date soon. It's better to sell milk out as fast as possible. So the shop decided to have milk 50% off and 10% off discount is still available.
  149.  
  150. Try to change your program to make the output looks like this.
  151.  
  152. ```
  153. NAME PRICE
  154. water 5.00 x 2 x 90.00%(10% off) = 9.00
  155. milk 15.00 x 2 x 50.00%(milk 50% off) x 90.00%(10% off) = 13.50
  156. coffee 20.00 x 3 x 90.00%(10% off) = 54.00
  157. SUM 76.50
  158. ```
  159.  
  160. ## Q4: Complex Discount Strategy
  161.  
  162. Now we have 2 discount strategies:
  163.  
  164. 1. All product get 10% off
  165. 2. Milk get 50% off
  166.  
  167. Let's add one more discount strategy
  168.  
  169. If an order have both water and milk all of them get 70% off, but the "Milk get 50% off" is not available now.
  170.  
  171. So the final discount strategy is as follow:
  172.  
  173. 1. All product get 10% off
  174. 2. Milk get 50% off
  175. 3. If an order have both water and milk all of them get 70% off, But the "#2: Milk get 50% off" is not available
  176.  
  177. if the order is
  178.  
  179. ```python
  180. # python version
  181. o = Order(
  182. Water(),
  183. Milk(),
  184. Water(),
  185. Milk(),
  186. Coffee(),
  187. Coffee(),
  188. Coffee(),
  189. )
  190. ```
  191. ```java
  192. // java version
  193. Order o = new Order(
  194. new Water(),
  195. new Milk(),
  196. new Water(),
  197. new Milk(),
  198. new Coffee(),
  199. new Coffee(),
  200. new Coffee()
  201. );
  202.  
  203. System.out.println(o.statement());
  204. ```
  205.  
  206. the output should be
  207.  
  208. ```
  209. NAME PRICE
  210. water 5.00 x 2 x 30.00%(milk&water 70% off) x 90.00%(10% off) = 2.70
  211. milk 15.00 x 2 x 30.00%(milk&water 70% off) x 90.00%(10% off) = 8.10
  212. coffee 20.00 x 3 x 90.00%(10% off) = 54.00
  213. SUM 64.80
  214. ```
  215.  
  216. if the order is
  217.  
  218. ```python
  219. # python version
  220. o = Order(
  221. Milk(),
  222. Milk(),
  223. Coffee(),
  224. Coffee(),
  225. Coffee(),
  226. )
  227. ```
  228. ```java
  229. // java version
  230. Order o = new Order(
  231. new Milk(),
  232. new Milk(),
  233. new Coffee(),
  234. new Coffee(),
  235. new Coffee()
  236. );
  237. ```
  238.  
  239. the output should be
  240.  
  241. ```
  242. NAME PRICE
  243. milk 15.00 x 2 x 50.00%(milk 50% off) x 90.00%(10% off) = 13.50
  244. coffee 20.00 x 3 x 90.00%(10% off) = 54.00
  245. SUM 67.50
  246. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement