Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.tek.interview.question;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /* ****************************************************************************************
- Please remove all bugs from the code below to get the following output:
- <pre>
- *******Order 1*******
- 1 book: 13.74
- 1 music CD: 16.49
- 1 chocolate bar: 0.94
- Sales Tax: 2.84
- Total: 28.33
- *******Order 2*******
- 1 imported box of chocolate: 11.5
- 1 imported bottle of perfume: 54.62
- Sales Tax: 8.62
- Total: 57.5
- *******Order 3*******
- 1 Imported bottle of perfume: 32.19
- 1 bottle of perfume: 20.89
- 1 packet of headache pills: 10.73
- 1 box of imported chocolates: 12.94
- Sales Tax: 8.77
- Total: 67.98
- Sum of orders: 153.81
- </pre>
- ******************************************************************************************** */
- /*
- * represents an item, contains a price and a description.
- *
- */
- class Item {
- private String description;
- private float price;
- public Item(String description, float price) {
- super();
- this.description = description;
- this.price = price;
- }
- public String getDescription() {
- return description;
- }
- public float getPrice() {
- return price;
- }
- }
- /*
- * represents an order line which contains the @link Item and the quantity.
- *
- */
- class OrderLine {
- private int quantity;
- private Item item;
- /*
- * @param item Item of the order
- *
- * @param quantity Quantity of the item
- */
- public OrderLine(Item item, int quantity) throws Exception {
- if (item == null) {
- System.err.println("ERROR - Item is NULL");
- throw new Exception("Item is NULL");
- }
- assert quantity > 0;
- //Issue 3. This keyword should be used to denote this class variable
- this.item = item;
- //Issue 4. This keyword should be used to denote this class variable
- this.quantity = quantity;
- }
- public Item getItem() {
- return item;
- }
- public int getQuantity() {
- return quantity;
- }
- }
- class Order {
- //Issue 1. orderLines is not initialised
- private List<OrderLine> orderLines = new ArrayList<OrderLine>();
- public void add(OrderLine o) throws Exception {
- if (o == null) {
- System.err.println("ERROR - Order is NULL");
- throw new IllegalArgumentException("Order is NULL");
- }
- orderLines.add(o);
- }
- public int size() {
- return orderLines.size();
- }
- public OrderLine get(int i) {
- return orderLines.get(i);
- }
- public void clear() {
- //Issue 5. The orderLine must be reinitialized
- this.orderLines.clear();
- }
- }
- class calculator {
- public static double rounding(double value) {
- //Issue 12. Rounding to int reduces the actual cost decimal digits
- DecimalFormat df = new DecimalFormat("#.##");
- return Double.parseDouble(df.format(value));
- }
- /**
- * receives a collection of orders. For each order, iterates on the order lines and calculate the total price which
- * is the item's price * quantity * taxes.
- *
- * For each order, print the total Sales Tax paid and Total price without taxes for this order
- */
- public void calculate(Map<String, Order> o) {
- double grandtotal = 0;
- double grandtotalWithTax = 0;
- // Iterate through the orders
- for (Map.Entry<String, Order> entry : o.entrySet()) {
- System.out.println("*******" + entry.getKey() + "*******");
- //Issue 19. Should not be initialized in the loop
- //grandtotal = 0;
- Order r = entry.getValue();
- double totalTax = 0;
- double total = 0;
- // Iterate through the items in the order
- //Issue 9 : Size should be less than r.size()
- for (int i = 0; i < r.size(); i++) {
- // Calculate the taxes
- double tax = 0;
- //Issue 18 : contains ignore case should be checked.
- if (r.get(i).getItem().getDescription().toUpperCase().contains("imported".toUpperCase())) {
- tax = rounding(r.get(i).getItem().getPrice() * 0.15); // Extra 5% tax on
- // imported items
- } else {
- tax = rounding(r.get(i).getItem().getPrice() * 0.10);
- }
- // Calculate the total price
- //Issue 10 : Math.floor will remove floating values
- //Issue 13 : rounding should be done after adding.
- double totalprice = rounding(rounding(r.get(i).getItem().getPrice()) + //Math.floor(
- rounding(tax));
- // Print out the item's total price
- //Issue 11 : Math.floor will remove floating values
- System.out.println(r.get(i).getItem().getDescription() + ": " + //Math.floor(
- totalprice);
- // Keep a running total
- totalTax += tax;
- total += r.get(i).getItem().getPrice();
- }
- //Issue 14 : rounding should be done.
- // Print out the total taxes
- System.out.println("Sales Tax: " + rounding(totalTax));
- //Issue 17 : Tax should be added with grandtotalwithtax variable.
- //total = total + totalTax;
- // Print out the total amount
- //Issue 15 : rounding should be done.
- System.out.println("Total: " + rounding(total));
- grandtotal += total;
- grandtotalWithTax +=total;
- grandtotalWithTax +=totalTax;
- }
- //Issue 16 : rounding should be done.
- System.out.println("Sum of orders: " + rounding(grandtotal));
- }
- }
- public class Foo {
- public static void main(String[] args) throws Exception {
- Map<String, Order> o = new HashMap<String, Order>();
- Order c = new Order();
- double grandTotal = 0;
- c.add(new OrderLine(new Item("book", (float) 12.49), 1));
- c.add(new OrderLine(new Item("music CD", (float) 14.99), 1));
- c.add(new OrderLine(new Item("chocolate bar", (float) 0.85), 1));
- o.put("Order 1", c);
- // Reuse cart for an other order
- //Issue 2 : This clears the value in the order object!
- //c.clear();
- //Issue 6 : Create new order object
- Order c1 = new Order();
- c1.add(new OrderLine(new Item("imported box of chocolate", 10), 1));
- c1.add(new OrderLine(new Item("imported bottle of perfume", (float) 47.50), 1));
- o.put("Order 2", c1);
- // Reuse cart for an other order
- //Issue 7 : This clears the value in the order object!
- //c.clear();
- //Issue 8 : Create new order object
- Order c2 = new Order();
- c2.add(new OrderLine(new Item("Imported bottle of perfume", (float) 27.99), 1));
- c2.add(new OrderLine(new Item("bottle of perfume", (float) 18.99), 1));
- c2.add(new OrderLine(new Item("packet of headache pills", (float) 9.75), 1));
- //Issue 18. Imported typo error
- c2.add(new OrderLine(new Item("box of imported chocolates", (float) 11.25), 1));
- o.put("Order 3", c2);
- new calculator().calculate(o);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement