Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. package com.tek.interview.question;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.text.DecimalFormat;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import java.util.Map;
  10.  
  11. /* ****************************************************************************************
  12.  
  13. Please remove all bugs from the code below to get the following output:
  14.  
  15. <pre>
  16.  
  17. *******Order 1*******
  18. 1 book: 13.74
  19. 1 music CD: 16.49
  20. 1 chocolate bar: 0.94
  21. Sales Tax: 2.84
  22. Total: 28.33
  23. *******Order 2*******
  24. 1 imported box of chocolate: 11.5
  25. 1 imported bottle of perfume: 54.62
  26. Sales Tax: 8.62
  27. Total: 57.5
  28. *******Order 3*******
  29. 1 Imported bottle of perfume: 32.19
  30. 1 bottle of perfume: 20.89
  31. 1 packet of headache pills: 10.73
  32. 1 box of imported chocolates: 12.94
  33. Sales Tax: 8.77
  34. Total: 67.98
  35. Sum of orders: 153.81
  36.  
  37. </pre>
  38.  
  39. ******************************************************************************************** */
  40.  
  41. /*
  42. * represents an item, contains a price and a description.
  43. *
  44. */
  45. class Item {
  46.  
  47. private String description;
  48. private float price;
  49.  
  50. public Item(String description, float price) {
  51. super();
  52. this.description = description;
  53. this.price = price;
  54. }
  55.  
  56. public String getDescription() {
  57. return description;
  58. }
  59.  
  60. public float getPrice() {
  61. return price;
  62. }
  63. }
  64.  
  65. /*
  66. * represents an order line which contains the @link Item and the quantity.
  67. *
  68. */
  69. class OrderLine {
  70.  
  71. private int quantity;
  72. private Item item;
  73.  
  74. /*
  75. * @param item Item of the order
  76. *
  77. * @param quantity Quantity of the item
  78. */
  79. public OrderLine(Item item, int quantity) throws Exception {
  80.  
  81. if (item == null) {
  82. System.err.println("ERROR - Item is NULL");
  83. throw new Exception("Item is NULL");
  84. }
  85. assert quantity > 0;
  86. //Issue 3. This keyword should be used to denote this class variable
  87. this.item = item;
  88. //Issue 4. This keyword should be used to denote this class variable
  89. this.quantity = quantity;
  90. }
  91.  
  92. public Item getItem() {
  93. return item;
  94. }
  95.  
  96. public int getQuantity() {
  97. return quantity;
  98. }
  99. }
  100.  
  101. class Order {
  102. //Issue 1. orderLines is not initialised
  103. private List<OrderLine> orderLines = new ArrayList<OrderLine>();
  104.  
  105. public void add(OrderLine o) throws Exception {
  106. if (o == null) {
  107. System.err.println("ERROR - Order is NULL");
  108. throw new IllegalArgumentException("Order is NULL");
  109. }
  110. orderLines.add(o);
  111. }
  112.  
  113. public int size() {
  114. return orderLines.size();
  115. }
  116.  
  117. public OrderLine get(int i) {
  118. return orderLines.get(i);
  119. }
  120.  
  121. public void clear() {
  122. //Issue 5. The orderLine must be reinitialized
  123. this.orderLines.clear();
  124. }
  125. }
  126.  
  127. class calculator {
  128.  
  129. public static double rounding(double value) {
  130. //Issue 12. Rounding to int reduces the actual cost decimal digits
  131. DecimalFormat df = new DecimalFormat("#.##");
  132. return Double.parseDouble(df.format(value));
  133. }
  134.  
  135. /**
  136. * receives a collection of orders. For each order, iterates on the order lines and calculate the total price which
  137. * is the item's price * quantity * taxes.
  138. *
  139. * For each order, print the total Sales Tax paid and Total price without taxes for this order
  140. */
  141. public void calculate(Map<String, Order> o) {
  142.  
  143. double grandtotal = 0;
  144. double grandtotalWithTax = 0;
  145. // Iterate through the orders
  146. for (Map.Entry<String, Order> entry : o.entrySet()) {
  147. System.out.println("*******" + entry.getKey() + "*******");
  148. //Issue 19. Should not be initialized in the loop
  149. //grandtotal = 0;
  150.  
  151. Order r = entry.getValue();
  152.  
  153. double totalTax = 0;
  154. double total = 0;
  155.  
  156. // Iterate through the items in the order
  157. //Issue 9 : Size should be less than r.size()
  158. for (int i = 0; i < r.size(); i++) {
  159.  
  160. // Calculate the taxes
  161. double tax = 0;
  162. //Issue 18 : contains ignore case should be checked.
  163. if (r.get(i).getItem().getDescription().toUpperCase().contains("imported".toUpperCase())) {
  164. tax = rounding(r.get(i).getItem().getPrice() * 0.15); // Extra 5% tax on
  165.  
  166. // imported items
  167. } else {
  168. tax = rounding(r.get(i).getItem().getPrice() * 0.10);
  169. }
  170. // Calculate the total price
  171. //Issue 10 : Math.floor will remove floating values
  172. //Issue 13 : rounding should be done after adding.
  173. double totalprice = rounding(rounding(r.get(i).getItem().getPrice()) + //Math.floor(
  174. rounding(tax));
  175. // Print out the item's total price
  176. //Issue 11 : Math.floor will remove floating values
  177. System.out.println(r.get(i).getItem().getDescription() + ": " + //Math.floor(
  178. totalprice);
  179.  
  180. // Keep a running total
  181. totalTax += tax;
  182. total += r.get(i).getItem().getPrice();
  183. }
  184. //Issue 14 : rounding should be done.
  185. // Print out the total taxes
  186. System.out.println("Sales Tax: " + rounding(totalTax));
  187.  
  188. //Issue 17 : Tax should be added with grandtotalwithtax variable.
  189. //total = total + totalTax;
  190.  
  191. // Print out the total amount
  192. //Issue 15 : rounding should be done.
  193. System.out.println("Total: " + rounding(total));
  194. grandtotal += total;
  195. grandtotalWithTax +=total;
  196. grandtotalWithTax +=totalTax;
  197. }
  198. //Issue 16 : rounding should be done.
  199. System.out.println("Sum of orders: " + rounding(grandtotal));
  200. }
  201. }
  202.  
  203. public class Foo {
  204.  
  205. public static void main(String[] args) throws Exception {
  206.  
  207. Map<String, Order> o = new HashMap<String, Order>();
  208.  
  209. Order c = new Order();
  210.  
  211. double grandTotal = 0;
  212.  
  213. c.add(new OrderLine(new Item("book", (float) 12.49), 1));
  214. c.add(new OrderLine(new Item("music CD", (float) 14.99), 1));
  215. c.add(new OrderLine(new Item("chocolate bar", (float) 0.85), 1));
  216.  
  217. o.put("Order 1", c);
  218.  
  219. // Reuse cart for an other order
  220. //Issue 2 : This clears the value in the order object!
  221. //c.clear();
  222. //Issue 6 : Create new order object
  223. Order c1 = new Order();
  224. c1.add(new OrderLine(new Item("imported box of chocolate", 10), 1));
  225. c1.add(new OrderLine(new Item("imported bottle of perfume", (float) 47.50), 1));
  226.  
  227. o.put("Order 2", c1);
  228.  
  229. // Reuse cart for an other order
  230. //Issue 7 : This clears the value in the order object!
  231. //c.clear();
  232. //Issue 8 : Create new order object
  233. Order c2 = new Order();
  234. c2.add(new OrderLine(new Item("Imported bottle of perfume", (float) 27.99), 1));
  235. c2.add(new OrderLine(new Item("bottle of perfume", (float) 18.99), 1));
  236. c2.add(new OrderLine(new Item("packet of headache pills", (float) 9.75), 1));
  237. //Issue 18. Imported typo error
  238. c2.add(new OrderLine(new Item("box of imported chocolates", (float) 11.25), 1));
  239.  
  240. o.put("Order 3", c2);
  241.  
  242. new calculator().calculate(o);
  243.  
  244. }
  245. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement