Tuesday, June 11, 2013

Write Spring Web service

As you know that Spring support only contract first web service , i am giving you complete example how to write Web service.

IDE : Any Eclipse or Spring suit
Server : Tomcat 6 or 7

1) Create Dynamic web project  with "ws-tutorial" you can use any other name but then you need to take care web.xml and -servlet.xml


2) Inside of lib folder following jar required .
 You can directly copy it or cretae pom file with dependency .


  1. activation-1.1.1.jar
  2. aopalliance-1.0.jar
  3. commons-logging-1.1.1.jar
  4. easymock-1.2_Java1.3.jar
  5. jaxb-api-2.1.jar
  6. jaxb-impl-2.1.5.jar
  7. jaxen-1.1.1.jar
  8. jdom-1.0.jar
  9. junit-3.8.2.jar
  10. log4j-1.2.15.jar
  11. mail-1.4.1.jar
  12. saaj-api-1.3.jar
  13. saaj-impl-1.3.jar
  14. spring-aop-2.5.1.jar
  15. spring-beans-2.5.1.jar
  16. spring-context-2.5.1.jar
  17. spring-context-support-2.5.1.jar
  18. spring-core-2.5.1.jar
  19. spring-jdbc-2.5.1.jar
  20. spring-jms-2.5.1.jar
  21. spring-orm-2.5.1.jar
  22. spring-oxm-1.5.0-m2.jar
  23. spring-tx-2.5.1.jar
  24. spring-web-2.5.1.jar
  25. spring-webmvc-2.5.1.jar
  26. spring-ws-core-1.5.0-m2.jar
  27. spring-xml-1.5.0-m2.jar
  28. stax-api-1.0.1.jar
  29.  wsdl4j-1.6.1.jar
  30. xalan-2.7.0.jar
  31. xercesImpl-2.8.1.jar
  32. xml-apis-1.3.04.jar

     
Now your project is created 

3)Create any XML request message you required  example :


<QuickViewRequest xmlns="http://mycompany.com/hr/schemas">
    <QuickView>
        <Plan>2006-07-03</Plan>
    </QuickView>
</QuickViewRequest>

4)Now using XML tools create XSD file  "quickview.xsd" will discuss each mapping in details   copy  xsd inside of web inf folder /WEB-INF/quickview.xsd"

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:hr="http://mycompany.com/hr/schemas"
           elementFormDefault="qualified"
           targetNamespace="http://mycompany.com/hr/schemas">

<xs:element name="QuickViewRequest">
<xs:complexType>
<xs:all>
<xs:element name="QuickView" type="hr:QuickViewType" />
</xs:all>
</xs:complexType>
     </xs:element>
     
<xs:complexType name="QuickViewType">
<xs:sequence>
<xs:element name="Plan" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>


Now you are ready to create web service 

Add inside of web.xml following configuration 


<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>MyCompany HR Holiday Service</display-name>

    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/services/*</url-pattern>
    </servlet-mapping>

</web-app>

now create "spring-ws-servlet.xml"  with following configuration


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean
class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="mappings">
<props>
<prop key="{http://mycompany.com/hr/schemas}QuickViewRequest">QuickViewEndpoint</prop>
</props>
</property>
<property name="interceptors">
<bean
class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
</property>
</bean>


<bean id="QuickViewEndpoint" class="com.mycompany.hr.ws.QuickViewEndpoint">
<constructor-arg ref="iQuickViewResourceService" />
</bean>

<bean id="iQuickViewResourceService" class="com.mycompany.hr.service.QuickViewResourceService" />

<!-- 
<bean
class="org.springframework.ws.server.endpoint.mapping.PayloadRootQNameEndpointMapping">
<property name="mappings">

<props>
   <prop key="{http://mycompany.com/hr/schemas}QuickViewRequest">QuickViewEndpoint</prop>
</props>
</property>
<property name="interceptors">
<bean
class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />
</property>
</bean>
-->
<bean id="quickview"
class="org.springframework.ws.wsdl.wsdl11.DynamicWsdl11Definition">
<property name="builder">
<bean
class="org.springframework.ws.wsdl.wsdl11.builder.XsdBasedSoap11Wsdl4jDefinitionBuilder">
<property name="schema" value="/WEB-INF/quickview.xsd" />
<property name="portTypeName" value="QuickViewResource" />
<property name="locationUri" value="http://localhost:8080/ws-tutorial/services" />
<property name="targetNamespace" value="http://mycompany.com/hr/definitions" />
</bean>
</property>
</bean>
</beans>

5) Create interface and give its implementation in class

public interface IQuickViewResourceService {

public String getQuickView(String url,String Plan);


}

public class QuickViewResourceService implements IQuickViewResourceService {

    private static final Log logger = LogFactory.getLog(StubHumanResourceService.class);

    public String getQuickView(String URL ,String plan) {
        logger.info("Inside Service-->."+plan);
        return "Vaquar";
    }

}

6) Create End point 

import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;
import org.springframework.ws.server.endpoint.AbstractJDomPayloadEndpoint;

import com.mycompany.hr.service.HumanResourceService;
import com.mycompany.hr.service.IQuickViewResourceService;


public class QuickViewEndpoint extends AbstractJDomPayloadEndpoint {

    private XPath Plan;


    private IQuickViewResourceService iQuickViewResourceService;

    public QuickViewEndpoint(IQuickViewResourceService iQuickViewResourceService) throws JDOMException {
        this.iQuickViewResourceService = iQuickViewResourceService;
        Namespace namespace = Namespace.getNamespace("hr", "http://mycompany.com/hr/schemas");
        Plan = XPath.newInstance("//hr:Plan");
        Plan.addNamespace(namespace);
        
    }

    protected Element invokeInternal(Element holidayRequest) throws Exception {
        String name = "Vaquar Khan";
        iQuickViewResourceService.getQuickView("",name);
        System.out.println("QuickViewEndpoint ..........................");
        return null;
    }


}

Now you are done with Web service code .
7)We are going to write client for web service 

public class QuickViewMain {

public static void main(String[] args) throws Exception {
try{
QuickViewRequestClient iQuickViewResourceService = new QuickViewRequestClient();

String url = "http://localhost:8080/ws-tutorial/services";

String Plan="Vaquar";
iQuickViewResourceService.getQuickView(Plan,url);

}catch(Exception e){
e.printStackTrace();

}
System.exit(0);
}


}



import java.text.SimpleDateFormat;
import java.util.Date;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.transform.JDOMResult;
import org.jdom.transform.JDOMSource;
import org.springframework.ws.client.core.WebServiceTemplate;

/**
 * Example client code using Spring-WS.
 */
public class QuickViewRequestClient {
private Namespace hrNs;
private WebServiceTemplate wsTemplate;
/**
* Default class constructor
*/
public QuickViewRequestClient() {
hrNs = Namespace.getNamespace("hr", "http://mycompany.com/hr/schemas");
wsTemplate = new WebServiceTemplate();
}

public void getQuickView(String plan,String url) {
Document document = 
createQuickViewRequest(plan);

        wsTemplate.sendSourceAndReceiveToResult(url, new JDOMSource(document.getRootElement()), new JDOMResult());
}

/**
* Creates a JDOM element representing a holiday request.
*/
private Document createQuickViewRequest(String Plan) {
Document document= new Document(new Element("QuickViewRequest", hrNs));
document.getRootElement()
.addContent(new Element("QuickView", hrNs).addContent(new Element("Plan", hrNs).setText("100")));
return document;
}
}

Add your war into tomcat server and run server  and run java class "QuickViewMain " :)

Thursday, June 6, 2013

Remove Trailing Zero in String

Remove Trailing Zero in String

Example 10000000 should be 1
and  1000001 should be 1000001

public class Test {

/**
* @param args
*/
public static void main(String[] args) {
Test test = new Test();
System.out.println("" + test.removeTrailingZero("1000000001"));

}

public String removeTrailingZero(String inputValue) {
String inputOrignal = "";
if (inputValue != null) {

StringBuilder sb = new StringBuilder(inputValue);
String input = sb.reverse().toString();

int len = input.length();
int counter = 0;
char[] tempCharArray = new char[len];

// put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = input.charAt(i);
if (input.charAt(i) == '0') {
counter++;
} else {
break;
}
}
inputOrignal = inputValue.substring(0, (len - counter));
}
return inputOrignal;
}

}

Monday, October 29, 2012

Why is Hibernate better than JDBC


Introduction to Hibernate

Hibernate is an Object-Relational Mapping (ORM) solution for JAVA. It is a powerful, high performance object/relational persistence and query service. It allows us to develop persistent classes following object-oriented idiom – including association, inheritance and polymorphism.
Hibernate Architecture

1) itself opens connection to database,

2) converts HQL (Hibernate Query Language) statements to database specific statement,

3) receives result set,

4) then performs mapping of these database specific data to Java objects which are directly used by Java application.

5)Hibernate uses the database specification from Hibernate Properties file. Automatic mapping is performed on the basis of the properties defined in hbm XML file defined for particular Java object.

 Hibernate communication with RDBMS
General steps:
1. Load the Hibernate configuration file and create configuration object. It will automatically load all hbm mapping files.
2. Create session factory from configuration object
3. Get one session from this session factory.
4. Create HQL query.
5. Execute query to get list containing Java objects.

Example: Retrieve list of employees from Employee table using Hibernate.
/* Load the hibernate configuration file */
Configuration cfg = new Configuration();
cfg.configure(CONFIG_FILE_LOCATION);
/* Create the session factory */
SessionFactory sessionFactory = cfg.buildSessionFactory();
/* Retrieve the session */
Session session = sessionFactory.openSession();
/* create query */
Query query = session.createQuery("from EmployeeBean”);
/* execute query and get result in form of Java objects */
List<EmployeeBean> finalList = query.list();
EmployeeBean.hbm.xml File
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.mf.bean.EmployeeBean"
table="t_employee">
<id name="id" type="string" unsaved-value="null">
<column name="id" sql-type="varchar(32)" not-null="true"/>
<generator class="uuid"/>
</id>
<property name="name">
<column name="name" />
</property>
<property name="salary">
<column name="salary" />
</property>
</class>
</hibernate-mapping>

JDBC Vs Hibernate
Why is Hibernate better than JDBC

1) Relational Persistence for JAVA
Working with both Object-Oriented software and Relational Database is complicated task with JDBC because there is mismatch between how data is represented in objects versus relational database. So with JDBC, developer has to write code to map an object model's data representation to a relational data model and its corresponding database schema. Hibernate is flexible and powerful ORM solution to map Java classes to database tables. Hibernate itself takes care of this mapping using XML files so developer does not need to write code for this.
2) Transparent Persistence
The automatic mapping of Java objects with database tables and vice versa is called Transparent Persistence. Hibernate provides transparent persistence and developer does not need to write code explicitly to map database tables tuples to application objects during interaction with RDBMS. With JDBC this conversion is to be taken care of by the developer manually with lines of code.
3) Support for Query Language
JDBC supports only native Structured Query Language (SQL). Developer has to find out the efficient way to access database, i.e to select effective query from a number of queries to perform same task. Hibernate provides a powerful query language Hibernate Query Language (independent from type of database) that is expressed in a familiar SQL like syntax and includes full support for polymorphic queries. Hibernate also supports native SQL statements. It also selects an effective way to perform a database manipulation task for an application.
4) Database Dependent Code
Application using JDBC to handle persistent data (database tables) having database specific code in large amount. The code written to map table data to application objects and vice versa is actually to map table fields to object properties. As table changed or database changed then it’s essential to change object structure as well as to change code written to map table-to-object/object-to-table. Hibernate provides this mapping itself. The actual mapping between tables and application objects is done in XML files. If there is change in Database or in any table then the only need to change XML file properties.
5) Maintenance Cost
With JDBC, it is developer’s responsibility to handle JDBC result set and convert it to Java objects through code to use this persistent data in application. So with JDBC, mapping between Java objects and database tables is done manually. Hibernate reduces lines of code by maintaining object-table mapping itself and returns result to application in form of Java objects. It relieves programmer from manual handling of persistent data, hence reducing the development time and maintenance cost.
6) Optimize Performance
Caching is retention of data, usually in application to reduce disk access. Hibernate, with Transparent Persistence, cache is set to application work space. Relational tuples are moved to this cache as a result of query. It improves performance if client application reads same data many

times for same write. Automatic Transparent Persistence allows the developer to concentrate more on business logic rather than this application code. With JDBC, caching is maintained by hand-coding.

7) Automatic Versioning and Time Stamping
By database versioning one can be assured that the changes done by one person is not being roll backed by another one unintentionally. Hibernate enables developer to define version type field to application, due to this defined field Hibernate updates version field of database table every time relational tuple is updated in form of Java class object to that table. So if two users retrieve same tuple and then modify it and one user save this modified tuple to database, version is automatically updated for this tuple by Hibernate. When other user tries to save updated tuple to database then it does not allow to save it because this user does not has updated data. In JDBC there is no check that always every user has updated data. This check has to be added by the developer.

8) Open-Source, Zero-Cost Product License
Hibernate is an open source and free to use for both development and production deployments.

9) Enterprise-Class Reliability and Scalability
Hibernate scales well in any environment, no matter if use it in-house Intranet that serves hundreds of users or for mission-critical applications that serve hundreds of thousands. JDBC can not be scaled easily.


 Disadvantages of Hibernate
1) Steep learning curve.
2) Use of Hibernate is an overhead for the applications which are :
• simple and use one database that never change
• need to put data to database tables, no further SQL queries
• there are no objects which are mapped to two different tables
Hibernate increases extra layers and complexity. So for these types of applications JDBC is the best choice.
3) Support for Hibernate on Internet is not sufficient.
4) Anybody wanting to maintain application using Hibernate will need to know Hibernate.
5) For complex data, mapping from Object-to-tables and vise versa reduces performance and increases time of conversion.
6) Hibernate does not allow some type of queries which are supported by JDBC. For example It does not allow to insert multiple objects (persistent data) to same table using single query. Developer has to write separate query to insert each object.

Saturday, September 8, 2012

Design Problem Sales Tax Problem Statement


Sales Tax Problem Statement :

 Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions.

When I purchase items I receive a receipt which lists the name of all the items and their price (including tax), finishing with the total cost of the items, and the total amounts of sales taxes paid. The rounding rules for sales tax are that for a tax rate of n%, a shelf price of p contains (np/100 rounded up to the nearest 0.05) amount of sales tax.

Write an application that prints out the receipt details for these shopping baskets...

INPUT:

Input 1:
1 book at 12.49
1 music CD at 14.99
1 chocolate bar at 0.85

Input 2:
1 imported box of chocolates at 10.00
1 imported bottle of perfume at 47.50

Input 3:
1 imported bottle of perfume at 27.99
1 bottle of perfume at 18.99

1 packet of headache pills at 9.75
1 box of imported chocolates at 11.25

OUTPUT

Output 1:
1 book : 12.49
1 music CD: 16.49
1 chocolate bar: 0.85
Sales Taxes: 1.50
Total: 29.83

Output 2:
1 imported box of chocolates: 10.50
1 imported bottle of perfume: 54.65
Sales Taxes: 7.65
Total: 65.15

Output 3:
1 imported bottle of perfume: 32.19
1 bottle of perfume: 20.89
1 packet of headache pills: 9.75
1 imported box of chocolates: 11.85
Sales Taxes: 6.70
Total: 74.68


Design consideration :

1)Design the solution using “Design by  Interface” approach . basically decoupled the implementation.

2) Class should be open for extension but close for modification

3) Sales tax get % get change then no code change required.

## exempt item tax  %
EXEMPT=0
## Import duty item tax %
IMPORTDUTY=5
##Basic sales tax %
BASICSALESTAX=10
##Basic sales tax +Import duty %
SALESTAXANDIMPORTDUTY=15

4) I have used decorator pattern and provided concrete implementation ,in future if required any changes in code and logic so wont change existing code will add another class and few method only

5) Used static factory pattern for object creation also implemented singleton pattern, only one thread is using it so no need any synchronization.

6) provided single entry( Service ) point for application and its also handle exceptions

7) Used JDK 1.7 for development

8) Using multi tier architecture

Assumption :
1) Display only out put as given in problem
2) Future may be calculation logic gets change
3) Future may be display logic gets change

Calculation based on following assumption

1) Basic sales tax is applicable at a rate of 10% on all goods,
2) Except books, food, and medical products that are exempt.
 3) Import duty is an additional sales  tax applicable on all imported goods at a rate of 5%, with no exemptions.
             
 When I purchase items I receive a receipt which lists the name of all the * items and their price (including tax),  finishing with the total cost of the items, and the total amounts of sales taxes paid.

The rounding rules for sales tax are that for a tax  rate of n%, a shelf price of p contains

 (np/100 rounded up to the nearest * 0.05) amount of sales tax.

           


                         :SHOPPING BILL:



=========================================================================
Quantity                                     Name                            Price
=========================================================================
1                      Book                             :12.50
1                      Music CD                      :16.50
1                      Chocolate bar               :0.85
--------------------------------------------------------------------------------------------------------------------------------
Sales Tax : 1.50
Total : 29.85
=========================================================================
                                   

                                               :SHOPPING BILL:

=========================================================================
Quantity                                     Name                            Price
=========================================================================
1                      Imported box of chocolates                   :10.50
1                      Imported bottle of perfume                           :54.65
--------------------------------------------------------------------------------------------------------------------------------
Sales Tax : 7.65
Total : 65.15
=========================================================================
                                   
                                                 :SHOPPING BILL:

=========================================================================
Quantity                                     Name                            Price
=========================================================================
1                      Imported bottle of perfume                    :32.20
1                      Bottle of perfume                                   :20.90
1                      Packet of headache pills                       :9.75
1                      Imported box of chocolates                   :11.85
--------------------------------------------------------------------------------------------------------------------------------
Sales Tax : 6.70
Total : 74.65
=========================================================================



1) ShoppingCartTestCase.java

Simple Junit test cases its contain three Testcase with following input data
Dependency : required Junit 3 or 4 jar

package com.thoughtworks.india.shoppingcart.salestax.junit.Shoppingcart;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.dto.impl.InputDataDTO;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.service.SalesTaxCalculatorServiceIfc;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.service.impl.SalesTaxCalculatorServiceImpl;
/**
 * This is Junit test case for testing
 * @author Vaquar Khan
 *
 */
public class ShoppingCartTestCase extends TestCase {
                                                        /**
                                                         * Method purpose : INPUT:
                                                         *
                                                         * Input 1:
                                                         *   1 book at 12.49
                                                         *   1 music CD at 14.99
                                                         *   1 chocolate bar at 0.85
                                                         * Insert calculate tax
                                                         *
                                                         * @param
                                                         * @return void
                                                         */

                                                        public void testScenarioInput_001() {

                                                           
                                                            InputDataDTO inputDataDTO = null;
                                                            List<InputDataDTO> shoppingCartList = new ArrayList<InputDataDTO>();
                                                            //System.out.println("Input 1:");

                                                            // TODO Input 1: 1 book at 12.49
                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Book");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setExemptTax(true);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(12.49));
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to list
                                                            shoppingCartList.add(inputDataDTO);
                                                           
                                                           
                                                            // 1 music CD at 14.99
                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Music CD");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(14.99));
                                                            inputDataDTO.setDomesticTaxPercentage(true);
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to list
                                                            shoppingCartList.add(inputDataDTO);
                                                           

                                                            // 1 chocolate bar at 0.85
                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Chocolate bar");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(0.85));
                                                            inputDataDTO.setExemptTax(true);
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to list
                                                            shoppingCartList.add(inputDataDTO);

                                                            try {
                                                                        // Creating the ShoppingCart
                                                                        SalesTaxCalculatorServiceIfc shopping = new SalesTaxCalculatorServiceImpl();
                                                                       
                                                                        // calculate data
                                                                        shopping.calculateTax(shoppingCartList);

                                                            } catch (Throwable e) {
                                                                        // This operation is not supported hence getting an error
                                                                        System.out.println(e.getLocalizedMessage());
                                                            }

                                                        }

                                                        /**
                                                         * Method purpose :
                                                         *  Input 2:
                                                         *  1 imported box of chocolates at 10.00
                                                         *  1 imported bottle of perfume at 47.50
                                                         *  Insert calculate tax
                                                         *
                                                         * @param
                                                         * @return void
                                                         */

                                                        public void testScenarioInput_002() {

                                                            InputDataDTO inputDataDTO = null;
                                                            List<InputDataDTO> shoppingCartList = new ArrayList<InputDataDTO>();
                                                            //System.out.println("Input 2:");
                                                            // TODO Input 2:
                                                            // 1 imported box of chocolates at 10.00

                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Imported box of chocolates ");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(10.00));
                                                            inputDataDTO.setAdditionalImportedTaxPercentage(true);
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to list
                                                            shoppingCartList.add(inputDataDTO);

                                                            // 1 imported bottle of perfume at 47.50
                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Imported bottle of perfume");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(47.50));
                                                            inputDataDTO.setAdditionalImportedTaxPercentage(true);
                                                            inputDataDTO.setDomesticTaxPercentage(true);
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to List
                                                            shoppingCartList.add(inputDataDTO);
                                                            try {
                                                                        // Creating the ShoppingCart
                                                                        SalesTaxCalculatorServiceIfc shopping = new SalesTaxCalculatorServiceImpl();
                                                                       
                                                                        // calculate data
                                                                        shopping.calculateTax(shoppingCartList);

                                                            } catch (Throwable e) {
                                                                        // This operation is not supported hence getting an error
                                                                        System.out.println(e.getLocalizedMessage());
                                                            }
                                                        }

                                                        /**
                                                         * Method purpose :
                                                         *  Input 3
                                                         *  1 imported bottle of perfume at 27.99 1 bottle of perfume at 18.99
                                                         *  1 packet of headache pills at 9.75 1 box of imported chocolates at 11.25
                                                         *   Insert calculate tax
                                                         *
                                                         * @param
                                                         * @return void
                                                         */

                                                        public void testScenarioInput_003() {

                                                            InputDataDTO inputDataDTO = null;
                                                            ArrayList<InputDataDTO> shoppingCartList = new ArrayList<InputDataDTO>();
                                                            //System.out.println("Input 3:");
                                                            // TODO Input 3 :
                                                            // 1 imported bottle of perfume at 27.99
                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Imported bottle of perfume");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(27.99));
                                                            inputDataDTO.setDomesticTaxPercentage(true);
                                                            inputDataDTO.setAdditionalImportedTaxPercentage(true);
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to List
                                                            shoppingCartList.add(inputDataDTO);

                                                            // 1 bottle of perfume at 18.99
                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Bottle of perfume");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(18.99));
                                                            inputDataDTO.setDomesticTaxPercentage(true);
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to list
                                                            shoppingCartList.add(inputDataDTO);

                                                            // 1 packet of headache pills at 9.75
                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Packet of headache pills");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(9.75));
                                                            inputDataDTO.setExemptTax(true);
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to list
                                                            shoppingCartList.add(inputDataDTO);

                                                            // 1 box of imported chocolates at 11.25
                                                            inputDataDTO = new InputDataDTO();
                                                            inputDataDTO.setGoodsName("Imported box of chocolates");
                                                            inputDataDTO.setGoodsquontity(1);
                                                            inputDataDTO.setGoodsPrice(new BigDecimal(11.25));
                                                            inputDataDTO.setAdditionalImportedTaxPercentage(true);
                                                            //TODO System.out.println(inputDataDTO.toString());
                                                            //Add to list
                                                            shoppingCartList.add(inputDataDTO);
                                                            try {
                                                                        // Creating the ShoppingCart
                                                                        SalesTaxCalculatorServiceIfc shopping = new SalesTaxCalculatorServiceImpl();
                                                                       
                                                                        // calculate data
                                                                        shopping.calculateTax(shoppingCartList);

                                                            } catch (Throwable e) {
                                                                        // This operation is not supported hence getting an error
                                                                        System.out.println(e.getLocalizedMessage());
                                                            }
                                                        }
                                                       
                                                       
}

Input 1:
   1 book at 12.49
   1 music CD at 14.99
   1 chocolate bar at 0.85
                                                         

 Input 2:
  1 imported box of chocolates at 10.00
  1 imported bottle of perfume at 47.50
                                                       
 Input 3
  1 imported bottle of perfume at 27.99 1 bottle of perfume at 18.99
  1 packet of headache pills at 9.75 1 box of imported chocolates at 11.25

and display following output             

  
                             :SHOPPING BILL:


 =========================================================================
Quantity                                     Name                            Price
=========================================================================
1                      Book                             :12.50
1                      Music CD                      :16.50
1                      Chocolate bar               :0.85
-------------------------------------------------------------------------
Sales Tax : 1.50
Total : 29.85
=========================================================================
                                   

                                               :SHOPPING BILL:


=========================================================================
Quantity                                     Name                            Price
=========================================================================
1                      Imported box of chocolates                   :10.50
1                      Imported bottle of perfume                           :54.65
--------------------------------------------------------------------------------------------
Sales Tax : 7.65
Total : 65.15
=========================================================================
                                   
                                                 :SHOPPING BILL:


=========================================================================
Quantity                                     Name                            Price
=========================================================================
1                      Imported bottle of perfume                    :32.20
1                      Bottle of perfume                                   :20.90
1                      Packet of headache pills                       :9.75
1                      Imported box of chocolates                   :11.85
----------------------------------------------------------------------------
Sales Tax : 6.70
Total : 74.65
=========================================================================
  
2)SalesTaxCalculatorServiceIfc.java

This is single point of contact to outside world , its contract for ServiceImpl and its kind of facade for user.

package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.service;

import java.util.List;

public interface SalesTaxCalculatorServiceIfc {

      
       /**
        * Calculate Shopping Cart Bill Users calculate records Bill (Once bill gets
        * generate user can’t add, edit, remove or calculate records.
        */
       public void calculateTax(List<?> shopingCartInputDTOList) ;
      
}


3) SalesTaxCalculatorServiceImpl.java :
This class implement SalesTaxCalculatorServiceIfc and contain following responsibility

1)Creating the ShoppingCart object using static factory
2) give call to Bo  methods
3) Handle exception if any exception would arise.
package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.service.impl;

import java.util.List;

import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.bo.SalesTaxCalculatorIfc;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.exception.ShopingCartException;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.factory.ShopingCartFactory;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.service.SalesTaxCalculatorServiceIfc;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.util.ShoppingCartconstantsIfc;
/**
 * following class use for single point of contact of external world
 * @author Vaquar Khan
 *
 */
public class SalesTaxCalculatorServiceImpl implements SalesTaxCalculatorServiceIfc {

                                                        /**
                                                         * This is only point of contact of outside world Calculate Shopping Cart
                                                         * Bill Users calculate records Bill (Once bill gets generate user can’t
                                                         * add, edit, remove or calculate records.
                                                         * @throws ShopingCartException
                                                         */
                                                        public void calculateTax(List<?> shopingCartInputDTOList)  {
                                                            SalesTaxCalculatorIfc selesTaxCalculatorIfc = null;

                                                            // Creating the ShoppingCart object using static factory
                                                            selesTaxCalculatorIfc = ShopingCartFactory.getInstance(ShoppingCartconstantsIfc.SERVICE_TAXCALCULATORSERVICEIMPL);

                                                            // calculate data
                                                            try {
                                                                        selesTaxCalculatorIfc.calculateTax(shopingCartInputDTOList);
                                                            } catch (ShopingCartException e ) {
                                                                        // TODO Auto-generated catch block
                                                                        e.printStackTrace();
                                                                        //Logs exception into logging
                                                            }

                                                        }

}


4) SalesTaxCalculatorIfc.java
its contract for SalesTaxCalculatorBaseImpl 

package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.bo;

import java.util.List;

import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.dto.impl.InputDataDTO;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.exception.ShopingCartException;
/**
 *
 * @author Vaquar Khan
 *
 */
public interface SalesTaxCalculatorIfc {
                                                       
                                                        /**
                                                         * following class is single point of entry of outside world
                                                         * @param shopingCartInputDTOList
                                                         * @throws ShopingCartException
                                                         */
                                                        public void calculateTax(List<?> shopingCartInputDTOList) throws ShopingCartException;
                                                        /**
                                                         * Displaying result as per user requirement
                                                         * @param shopingCartInputDTOList
                                                         */
                                                        public  void displayBill(List<?> shopingCartInputDTOList);
                                                        /**
                                                         * calculate tax
                                                         * @param shopingCartInputDTOList
                                                         * @return
                                                         * @throws ShopingCartException
                                                         */
                                                        public  List<InputDataDTO> calculateTaxPercentage(List<?> shopingCartInputDTOList) throws ShopingCartException ;

}

5) SalesTaxCalculatorBaseImpl.java
This is abstract class , i am using decorator pattern here so it would give few abstract method for concrete decorator and its open for future change.

package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.bo.impl;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;

import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.bo.SalesTaxCalculatorIfc;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.dto.impl.InputDataDTO;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.exception.ShopingCartException;
/**
 * Following class in base class its contain few abstract methods
 * incase fure we need to make any changes , we can write new implementation .
 * =============================================================================
 * 1) Basic sales tax is applicable at a rate of 10% on all goods,
 * 2) Except books, food, and medical products that are exempt.
 * 3) Import duty is an  additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions.
 *
 * When I purchase items I receive a receipt which lists the name of all the items and their price (including tax),
 *
 * finishing with the total cost of the items, and the total amounts of sales
 * taxes paid. The rounding rules for sales tax are that for a tax rate of n%, a
 * shelf price of p contains (np/100 rounded up to the nearest 0.05) amount of
 * sales tax.
 *
 * Write an application that prints out the receipt details for these shopping baskets...
 *
 * INPUT:
 *
 * Input 1: 1 book at 12.49 1 music CD at 14.99 1 chocolate bar at 0.85
 *
 * Input 2: 1 im@author Vaquar Khanported box of chocolates at 10.00 1 imported bottle of perfume
 * at 47.50
 *
 * Input 3: 1 imported bottle of perfume at 27.99 1 bottle of perfume at 18.99 1
 * packet of headache pills at 9.75 1 box of imported chocolates at 11.25
 *
 * OUTPUT
 *
 * Output 1: 1 book: 12.49 1 music CD: 16.49 1 chocolate bar: 0.85 Sales Taxes:
 * 1.50 Total: 29.83
 *
 * Output 2: 1 imported box of chocolates: 10.50 1 imported bottle of perfume:
 * 54.65 Sales Taxes: 7.65 Total: 65.15
 *
 * Output 3: 1 imported bottle of perfume: 32.19 1 bottle of perfume: 20.89 1
 * packet of headache pills: 9.75 1 imported box of chocolates: 11.85 Sales
 * Taxes: 6.70 Total: 74.68
 * =============================================================================
 *
 * @author Vaquar Khan
 */
public abstract class SalesTaxCalculatorBaseImpl implements SalesTaxCalculatorIfc {
    //ROUND_FACTOR
                                                        private static final BigDecimal ROUND_FACTOR = new BigDecimal("0.05");

                                                        /**
                                                         * Calculate Shopping Cart Bill Users calculate records Bill
                                                         * (Once bill gets generate user can’t add, edit, remove or calculate records)
                                                         * @throws ShopingCartException
                                                         */
                                                        public void calculateTax(List<?> shopingCartInputDTOList) throws ShopingCartException {

                                                            // Calculate records
                                                            List<?> newShopingCartInputDTOList = this.calculateTaxPercentage(shopingCartInputDTOList);
                                                            System.out.println(" \t \t \t :SHOPPING BILL: ");
                                                            System.out.println("\n \n ");
                                                            this.displayBill(newShopingCartInputDTOList);

                                                        }

                                                        /**
                                                         * Following method is used to display Bill
                                                         * May be in future requirement gets change and want to display result in different format thats why
                                                         * I have made it abstract
                                                         *
                                                         * @param shopingCartInputDTOList
                                                         */
                                                        public abstract void displayBill(List<?> shopingCartInputDTOList);

                                                        /**
                                                         * Following method is used to calculate tax
                                                         *  1) Basic sales tax is applicable at a rate of 10% on all goods,
                                                         *  2) Except books, food, and medical products that are exempt.
                                                         *  3) Import duty is an additional sales
                                                         * tax applicable on all imported goods at a rate of 5%, with no exemptions.
                                                         *
                                                         * When I purchase items I receive a receipt which lists the name of all the
                                                         * items and their price (including tax),
                                                         *
                                                         * finishing with the total cost of the items, and the total amounts of
                                                         * sales taxes paid. The rounding rules for sales tax are that for a tax
                                                         * rate of n%, a shelf price of p contains (np/100 rounded up to the nearest
                                                         * 0.05) amount of sales tax.
                                                         *
                                                         * @param shopingCartInputDTOList
                                                         * @return
                                                         * @throws ShopingCartException
                                                         */
                                                        public abstract List<InputDataDTO> calculateTaxPercentage(
                                                                        List<?> shopingCartInputDTOList) throws ShopingCartException;

                                                        /**
                                                         * Following method is used for add tax with price
                                                         *
                                                         * @param calculateTaxPercentage
                                                         * @param goodsPrice
                                                         * @return
                                                         */
                                                        protected BigDecimal addTaxandPrice(BigDecimal calculateTaxPercentage,
                                                                        BigDecimal goodsPrice) {
                                                            BigDecimal addtax = calculateTaxPercentage.add(goodsPrice);
                                                            addtax = roundOff(addtax);
                                                            return addtax;
                                                        }

                                                        /**
                                                         * Following method is used to calculate total price
                                                         *
                                                         * @param shopingCartInputDTOList
                                                         * @return
                                                         */
                                                        protected BigDecimal totalPrice(List<?> shopingCartInputDTOList) {

                                                            BigDecimal total = new BigDecimal(0);

                                                            if (shopingCartInputDTOList != null) {
                                                                        Iterator<?> shopingCartInputDTOListItr = shopingCartInputDTOList
                                                                                                .iterator();

                                                                        while (shopingCartInputDTOListItr.hasNext()) {
                                                                                    InputDataDTO inputDataDTO = (InputDataDTO) shopingCartInputDTOListItr
                                                                                                            .next();
                                                                                    total = total.add(inputDataDTO.getGoodsPrice().add(inputDataDTO.getCalculateTaxPercentage()));

                                                                        }// end of while

                                                            }
                                                            total = this.roundOff(total);

                                                            return total;
                                                        }

                                                        /**
                                                         * Following method is used to calculate total tax
                                                         *
                                                         * @param shopingCartInputDTOList
                                                         * @return
                                                         */
                                                        protected BigDecimal totalTax(List<?> shopingCartInputDTOList) {

                                                            BigDecimal total = new BigDecimal(0);

                                                            if (shopingCartInputDTOList != null) {
                                                                        Iterator<?> shopingCartInputDTOListItr = shopingCartInputDTOList.iterator();

                                                                        while (shopingCartInputDTOListItr.hasNext()) {
                                                                                    InputDataDTO inputDataDTO = (InputDataDTO) shopingCartInputDTOListItr.next();
                                                                                    total = total.add(inputDataDTO.getCalculateTaxPercentage());
                                                                        }// end of while

                                                                        total = roundOff(total);
                                                            }
                                                            return total;
                                                        }

                                                        /**
                                                         * JDK 1.4 Following method is used for round off up to .05 Round of 0.05
                                                         *
                                                         * @param value
                                                         * @return
                                                         */

                                                        protected BigDecimal roundOff(BigDecimal value) {
                                                            value = value.divide(ROUND_FACTOR);
                                                            value = new BigDecimal(Math.ceil(value.doubleValue()));
                                                            value = value.multiply(ROUND_FACTOR);
                                                            return value;
                                                        }

}

6) SalesTaxCalculatorImpl.java
This class extends SalesTaxCalculatorBaseImpl and provide implementation of abstract method .


in future if user want to calculate in diffrent way
public  List<InputDataDTO> calculateTaxPercentage(List<?> shopingCartInputDTOList) throws ShopingCartException;

in future if user want to see different format
public  void displayBill(List<?> shopingCartInputDTOList);


package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.bo.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.dto.impl.InputDataDTO;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.exception.ShopingCartException;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.util.ShoppingCartconstantsIfc;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.util.Util;

public class SalesTaxCalculatorImpl extends SalesTaxCalculatorBaseImpl{
                                                       
                                                       
                                                        /**
                                                         * @author Vaquar Khan
                                                         * Following method is used to calculate tax
                                                         * 1) Basic sales tax is applicable at a rate of 10% on all goods,
                                                         * 2) Except books, food, and  medical products that are exempt.
                                                         * 3) Import duty is an additional sales  tax applicable on all imported goods at a rate of 5%, with no exemptions.
                                                         *
                                                         * When I purchase items I receive a receipt which lists the name of all the
                                                         * items and their price (including tax),
                                                         *
                                                         * finishing with the total cost of the items, and the total amounts of
                                                         * sales taxes paid. The rounding rules for sales tax are that for a tax
                                                         * rate of n%, a shelf price of p contains (np/100 rounded up to the nearest
                                                         * 0.05) amount of sales tax.
                                                         *
                                                         * @param shopingCartInputDTOList
                                                         * @return
                                                         * @throws ShopingCartException
                                                         */
                                                        public  List<InputDataDTO> calculateTaxPercentage(List<?> shopingCartInputDTOList) throws ShopingCartException {

                                                            BigDecimal salesTax = null;
                                                            BigDecimal hundread = new BigDecimal(ShoppingCartconstantsIfc.HUNDREAD);
                                                            BigDecimal taxPercentage = null;

                                                            List<InputDataDTO> newShopingCartInputDTOList = new ArrayList<InputDataDTO>();

                                                            if (shopingCartInputDTOList != null) {
                                                                        //Create Iterator
                                                                        Iterator<?> shopingCartInputDTOListItr = shopingCartInputDTOList.iterator();
                                                                       
                                                                        while (shopingCartInputDTOListItr.hasNext()) {
                                                                                    InputDataDTO inputDataDTO = (InputDataDTO) shopingCartInputDTOListItr.next();

                                                                                    if (inputDataDTO.isExemptTax()) {
                                                                                                taxPercentage = new BigDecimal(Util.readProperties(ShoppingCartconstantsIfc.EXEMPT));
                                                                                    } else if (inputDataDTO.isDomesticTaxPercentage()&& inputDataDTO.isAdditionalImportedTaxPercentage()) {
                                                                                                taxPercentage = new BigDecimal(Util.readProperties(ShoppingCartconstantsIfc.BASIC_SALES_TAX_AND_IMPORT_DUTY));
                                                                                    } else if (inputDataDTO.isDomesticTaxPercentage()) {
                                                                                                taxPercentage = new BigDecimal(Util.readProperties(ShoppingCartconstantsIfc.BASIC_SALES_TAX));
                                                                                    } else if (inputDataDTO.isAdditionalImportedTaxPercentage()) {
                                                                                                taxPercentage = new BigDecimal(Util.readProperties(ShoppingCartconstantsIfc.IMPORT_DUTY));
                                                                                    }

                                                                                    // Calculate Tax
                                                                                    salesTax = inputDataDTO.getGoodsPrice().multiply(taxPercentage).divide(hundread);
                                                                                    //created new dto list
                                                                                                                                newShopingCartInputDTOList=Util.convertDto(inputDataDTO,salesTax, newShopingCartInputDTOList);
                                                                                    // put values into Map
                                                                        }// While

                                                            }
                                                            return newShopingCartInputDTOList;
                                                        }

                                                        /**Following method is used to display Bill
                                                         *
                                                         * @param shopingCartInputDTOList
                                                         */
                                                        public  void displayBill(List<?> shopingCartInputDTOList) {

                                                            if (shopingCartInputDTOList != null) {
                                                                        Iterator<?> shopingCartInputDTOListItr = shopingCartInputDTOList
                                                                                                .iterator();

                                                                        //System.out.println(" \t \t \t :SHOPPING BILL: ");
                                                                        //System.out.println("\n \n ");
                                                                        System.out
                                                                                                                                            .println("=============================================================================");
                                                                        System.out.println("Quantity " + "\t" + "\t" + "\t" + " Name "
                                                                                                + "\t" + "\t" + "\t" + " Price ");
                                                                        System.out
                                                                                                                                            .println("=============================================================================");

                                                                        while (shopingCartInputDTOListItr.hasNext()) {
                                                                                    InputDataDTO inputDataDTO = (InputDataDTO) shopingCartInputDTOListItr
                                                                                                            .next();

                                                                                    System.out.println(inputDataDTO.getGoodsquontity()
                                                                                                            + "\t"
                                                                                                            + "\t"
                                                                                                            + inputDataDTO.getGoodsName()
                                                                                                            + "\t"
                                                                                                            + "\t"
                                                                                                            + ":"
                                                                                                            + addTaxandPrice(inputDataDTO
                                                                                                                                    .getCalculateTaxPercentage(), inputDataDTO
                                                                                                                                    .getGoodsPrice()));

                                                                        }// While

                                                                        System.out
                                                                                                .println("----------------------------------------------------------------------------");
                                                                        System.out.println("Sales Tax : "
                                                                                                + totalTax(shopingCartInputDTOList));
                                                                        System.out
                                                                                                .println("Total : " + totalPrice(shopingCartInputDTOList));

                                                            }// end of if
                                                            System.out
                                                                                                                                .println("=============================================================================");
                                                        }

}


7) ShopingCartFactory.java

This class is using static factory pattern  with singleton pattern
package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.factory;

import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.bo.SalesTaxCalculatorIfc;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.bo.impl.SalesTaxCalculatorImpl;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.util.ShoppingCartconstantsIfc;

/**
 * This is Static Factory for Object creation. Currently its creating only
 * ServiceTaxCalculatorImpl object but if in future required to decorate any
 * object then factory would help it modify it easily.
 *
 * @author Vaquar Khan
 *
 */
public class ShopingCartFactory {

                                                        private static SalesTaxCalculatorIfc uniqueInstance = null;

                                                        // made class immutable
                                                        private ShopingCartFactory() {
                                                        }

                                                        /**
                                                         * following method will return instance of BO
                                                         * Currently single thread is accessing instance so no need synchronization
                                                         * @param type
                                                         * @return
                                                         */
                                                        public static SalesTaxCalculatorIfc getInstance(String type) {

                                                            // null check
                                                            if (type == null) {
                                                                        return uniqueInstance;
                                                            }

                                                            switch (type) {
                                                            case ShoppingCartconstantsIfc.SERVICE_TAXCALCULATORSERVICEIMPL:
                                                                        if (isUniqueInstance(uniqueInstance))
                                                                                    uniqueInstance = new SalesTaxCalculatorImpl();
                                                                        break;
                                                            // for future enhancement added only for example purpose not in use
                                                            case "ABC":
                                                                        if (isUniqueInstance(uniqueInstance))
                                                                        //TODO uniqueInstance = null;
                                                                        break;
                                                            // Default value
                                                            default:
                                                                        if (isUniqueInstance(uniqueInstance))
                                                                        uniqueInstance = new SalesTaxCalculatorImpl();
                                                                        break;
                                                            }

                                                            return uniqueInstance;
                                                        }
/**
 * Method is use instance null or not for readability purpose
 * @param Instance
 * @return
 */
                                                        private static boolean isUniqueInstance(SalesTaxCalculatorIfc Instance) {
                                                           
                                                            if (Instance == null)
                                                                        return true;
                                                            else
                                                                        return false;

                                                        }

}

8) ShopingCartException.java

This class is using if any exception arise , its shopping cart own exception

package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.exception;
/**
 * ShopingCartException use for throw error if any issue found in ShopingCart project
 * @author Vaquar Khan
 *
 */
public class ShopingCartException extends Exception {

      

       String mistake;

       // Default constructor - initializes instance variable to unknown
       public ShopingCartException() {
              super(); // call superclass constructor
              mistake = "unknown";
       }

       // Constructor receives some kind of message that is saved in an instance
       // variable.
       public ShopingCartException(String err) {
              super(err); // call super class constructor
              mistake = err; // save message
       }

       // public method, callable by exception catcher. It returns the error
       // message.
       public String getError() {
              return mistake;
       }
}


9) ShoppingCartconstantsIfc.java
This class contain all constants

package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.util;
/**
 * Following interface use for constants
 * @author Vaquar Khan
 *
 */
public interface ShoppingCartconstantsIfc {
      
      
      
       //Properties file location
       public String PROPERTIES_URL"salestaxrules.properties";
       //Properties File
       public String  EXEMPT ="EXEMPT";//exempt
       public String  IMPORT_DUTY = "IMPORTDUTY";//additionalImportedTaxPercentage
       public String BASIC_SALES_TAX = "BASICSALESTAX";//domesticTaxPercentage
       public String BASIC_SALES_TAX_AND_IMPORT_DUTY = "SALESTAXANDIMPORTDUTY";//additionalImportedTaxPercentage+domesticTaxPercentage
       //Service locator
       public String  SERVICE_TAXCALCULATORSERVICEIMPL"TaxCalculatorServiceImpl";
      
       public String HUNDREAD ="100";
      
      
}



10) Util.java
This class contain common utility methods

package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Properties;

import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.dto.impl.InputDataDTO;
import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.exception.ShopingCartException;

/**
 * Following class contains utility methods
 * @author Vaquar Khan
 *
 */
public class Util {

                                                           
                                                        /**
                                                         * Following method is used to read properties file
                                                         * read properties file
                                                         * @param keyValue
                                                         * @return
                                                         * @throws ShopingCartException
                                                         */
                                                        public static String readProperties(String keyValue) throws ShopingCartException {
                                                            Properties prop = new Properties();
                                                            FileInputStream fis=null;
                                                            String propertiesKey = null;
                                                            try {
                                                                        fis = new FileInputStream(
                                                                                                ShoppingCartconstantsIfc.PROPERTIES_URL);
                                                                        prop.load(fis);
                                                                        // get value from key
                                                                        propertiesKey = prop.getProperty(keyValue);
                                                                       
                                                            } catch (IOException e) {
                                                                        e.printStackTrace();
                                                            }finally{
                                                                        try {
                                                                                    fis.close();
                                                                        } catch (IOException e) {
                                                                                    // TODO Will handle Try.catch writing my own exception handler
                                                                                    e.printStackTrace();
                                                                                    throw new ShopingCartException("Issue in reading Filet");
                                                                        }
                                                            }
                                                            return propertiesKey;
                                                        }
/**
 * Following method is using to set result into InputDataDTO object
 *
 * @param inputDataDTO
 * @param salesTax
 * @return
 */
                                                        public static List<InputDataDTO> convertDto(InputDataDTO inputDataDTO,
                                                                        BigDecimal salesTax,List<InputDataDTO> newShopingCartInputDTOList) {

                                                            InputDataDTO newInputDataDTO = new InputDataDTO();
                                                           
                                                            newInputDataDTO.setAdditionalImportedTaxPercentage(inputDataDTO                                                        .isAdditionalImportedTaxPercentage());
                                                            newInputDataDTO.setCalculateTaxPercentage(salesTax);
                                                                                                                newInputDataDTO.setDomesticTaxPercentage(inputDataDTO.isDomesticTaxPercentage());
                                                            newInputDataDTO.setExemptTax(inputDataDTO.isExemptTax());
                                                            newInputDataDTO.setGoodsName(inputDataDTO.getGoodsName());
                                                            newInputDataDTO.setGoodsPrice(inputDataDTO.getGoodsPrice());
                                                            newInputDataDTO.setGoodsquontity(inputDataDTO.getGoodsquontity());
                                                            // Added into List
                                                            newShopingCartInputDTOList.add(newInputDataDTO);

                                                            return newShopingCartInputDTOList;
                                                        }
}








11) InputDataDTOIfc.java
This class is contract for InputDataDTO
package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.dto;

import java.math.BigDecimal;

/**
 * Following Interface is using as a contract for DTO
 * @author Vaquar Khan
 *
 */
public interface InputDataDTOIfc {

       /**
        * Following method will use for isAdditionalImportedTaxPercentage
        *
        * @return
        */
       public boolean isAdditionalImportedTaxPercentage();

       /**
        * Following method will use for setAdditionalImportedTaxPercentage
        *
        * @param additionalImportedTaxPercentage
        */
       public void setAdditionalImportedTaxPercentage(
                     boolean additionalImportedTaxPercentage);

       /**
        * Following method will use for isDomesticTaxPercentage
        *
        * @return
        */

       public boolean isDomesticTaxPercentage();

       /**
        * Following method will use for setDomesticTaxPercentage
        *
        * @param domesticTaxPercentage
        */
       public void setDomesticTaxPercentage(boolean domesticTaxPercentage);

       /**
        * Following method will use for isExemptTax
        *
        * @return
        */
       public boolean isExemptTax();

       /**
        * Following method will use for setExemptTax
        *
        * @param exemptTax
        */
       public void setExemptTax(boolean exemptTax);

       /**
        * Following method will use for getGoodsName
        *
        * @return
        */
       public String getGoodsName();

       /**
        * Following method will use for setGoodsName
        *
        * @param goodsName
        */
       public void setGoodsName(String goodsName);

       /**
        * Following method will use for getGoodsPrice
        *
        * @return
        */
       public BigDecimal getGoodsPrice();

       /**
        * Following method will use for setGoodsPrice
        *
        * @param goodsPrice
        */
       public void setGoodsPrice(BigDecimal goodsPrice);

       /**
        * Following method will use for getGoodsquontity
        *
        * @return
        */
       public int getGoodsquontity();

       /**
        * Following method will use for setGoodsquontity
        *
        * @param goodsquontity
        */
       public void setGoodsquontity(int goodsquontity);

       /**
        * Following method will use for getCalculateTaxPercentage
        *
        * @return
        */
       public BigDecimal getCalculateTaxPercentage();

       /**
        * Following method will use for setCalculateTaxPercentage
        *
        * @param calculateTaxPercentage
        */
       public void setCalculateTaxPercentage(BigDecimal calculateTaxPercentage);

}


12) InputDataDTO.java
This class is using for Data transfer object its contain simple getter and setter
package com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.dto.impl;

import java.math.BigDecimal;

import com.thoughtworks.india.shoppingcart.salestax.java.calculatetax.dto.InputDataDTOIfc;

/**
 * following class is use as data transfer object
 * @author Vaquar Khan
 *
 */
public class InputDataDTO implements InputDataDTOIfc {

                                                        private boolean domesticTaxPercentage = false;
                                                        private boolean additionalImportedTaxPercentage = false;
                                                        private boolean exemptTax = false;
                                                        private String goodsName = "";
                                                        private BigDecimal goodsPrice = new BigDecimal(0);
                                                        private int goodsquontity = 0;
                                                        private BigDecimal calculateTaxPercentage = new BigDecimal(0);

                                                        /**
                                                         *
                                                         * @return
                                                         */
                                                        public boolean isAdditionalImportedTaxPercentage() {
                                                            return additionalImportedTaxPercentage;
                                                        }

                                                        /**
                                                         *
                                                         * @param additionalImportedTaxPercentage
                                                         */
                                                        public void setAdditionalImportedTaxPercentage(
                                                                        boolean additionalImportedTaxPercentage) {
                                                            this.additionalImportedTaxPercentage = additionalImportedTaxPercentage;
                                                        }

                                                        /**
                                                         *
                                                         * @return
                                                         */

                                                        public boolean isDomesticTaxPercentage() {
                                                            return domesticTaxPercentage;
                                                        }

                                                        /**
                                                         *
                                                         * @param domesticTaxPercentage
                                                         */
                                                        public void setDomesticTaxPercentage(boolean domesticTaxPercentage) {
                                                            this.domesticTaxPercentage = domesticTaxPercentage;
                                                        }

                                                        public boolean isExemptTax() {
                                                            return exemptTax;
                                                        }

                                                        /**
                                                         *
                                                         * @param exemptTax
                                                         */
                                                        public void setExemptTax(boolean exemptTax) {
                                                            this.exemptTax = exemptTax;
                                                        }

                                                        /**
                                                         *
                                                         * @return
                                                         */
                                                        public String getGoodsName() {
                                                            return goodsName;
                                                        }

                                                        /**
                                                         *
                                                         * @param goodsName
                                                         */
                                                        public void setGoodsName(String goodsName) {
                                                            this.goodsName = goodsName;
                                                        }

                                                        /**
                                                         *
                                                         * @return
                                                         */
                                                        public BigDecimal getGoodsPrice() {
                                                            return goodsPrice;
                                                        }

                                                        /**
                                                         *
                                                         * @param goodsPrice
                                                         */
                                                        public void setGoodsPrice(BigDecimal goodsPrice) {
                                                            this.goodsPrice = goodsPrice;
                                                        }

                                                        /**
                                                         *
                                                         * @return
                                                         */
                                                        public int getGoodsquontity() {
                                                            return goodsquontity;
                                                        }

                                                        /**
                                                         *
                                                         * @param goodsquontity
                                                         */
                                                        public void setGoodsquontity(int goodsquontity) {
                                                            this.goodsquontity = goodsquontity;
                                                        }

                                                        /**
                                                         *
                                                         * @return
                                                         */
                                                        public BigDecimal getCalculateTaxPercentage() {
                                                            return calculateTaxPercentage;
                                                        }

                                                        /**
                                                         *
                                                         * @param calculateTaxPercentage
                                                         */
                                                        public void setCalculateTaxPercentage(BigDecimal calculateTaxPercentage) {
                                                            this.calculateTaxPercentage = calculateTaxPercentage;
                                                        }

@Override
public String toString() {
                                                        System.out.println("Input Data ");
                                                        System.out.println("----------------------------------------------------------------------------------------------------------- ");
                                                        return "\n\n "+"Goods Name="+ getGoodsName()+"\n "+"Goods Quontity="+getGoodsquontity()+"\n "+"Good sPrice="+getGoodsPrice();
}
}


13) salestaxrules.properties

This is properties file contain frequently change value

## exempt item tax  %
EXEMPT=0
## Import duty item tax %
IMPORTDUTY=5
##Basic sales tax %
BASICSALESTAX=10
##Basic sales tax +Import duty %
SALESTAXANDIMPORTDUTY=15


## Properties File
## Following properties file contain values which would change in future
## We can change any value without touch our code and it will return result as per requirements
##--------------------
## exempt item tax  %
EXEMPT=0
## Import duty item tax %
IMPORTDUTY=5
##Basic sales tax %
BASICSALESTAX=10
##Basic sales tax +Import duty %
SALESTAXANDIMPORTDUTY=15

Soon will share with you few more design problems