Pages

Wednesday, October 8, 2014

Stock Smart - iOS app

Stock Smart

EASY - FAST and always FREE

Stock smart will provide real time updates for your stock portfolio.

It’s the fastest, easiest and most elegant way to manage your personal stock portfolio.


        


Wednesday, April 23, 2014

SPRING-WS 2.X + JUNIT + MAVEN + CARGO

More information: http://projects.spring.io/spring-ws/
Download Eclipse project from Github: https://github.com/wcursino/simpleSpring-ws

SimpleEndpoint.java :
package com.cursino.wilson;
import org.jdom2.Element;
import org.jdom2.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
@Endpoint
public class SimpleEndpoint {
    private static final String NAME_SPACE = "http://javacaffeine.com/service/schemas";
    @Autowired
    private SimpleService simpleService;
    public void setSimpleService(SimpleService simpleService) {
        this.simpleService = simpleService;
    }    @PayloadRoot(namespace = NAME_SPACE, localPart = "SimpleRequest")
    public @ResponsePayload
    Element handleRequest(@RequestPayload Element request) {
        Namespace ns = Namespace.getNamespace("wsc", NAME_SPACE);
        int a = new Integer(request.getChild("a", ns).getValue().trim());
        int b = new Integer(request.getChild("b", ns).getValue().trim());
        Element response = new Element("SimpleResponse", ns);
        response.addNamespaceDeclaration(ns);
        Element outcomes = new Element("sum", ns);
        outcomes.setText(simpleService.sum(a, b));
        response.addContent(outcomes);
        return response;
    }
}

SimpleService.java : 
package com.cursino.wilson;
import org.springframework.stereotype.Service;
@Service
public class SimpleService {
    public String sum(int a, int b) {
        return "The sum of A(" + a + ") and B(" + b + ") is " + (a + b);
    }
}

SimpleEndpointTest.java:
package com.cursino.wilson;

import static org.springframework.ws.test.server.RequestCreators.withPayload;
import static org.springframework.ws.test.server.ResponseMatchers.payload;
import static org.springframework.ws.test.server.ResponseMatchers.validPayload;
import javax.xml.transform.Source;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ws.test.server.MockWebServiceClient;
import org.springframework.xml.transform.StringSource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:spring-ws-servlet.xml")
public class SimpleEndpointTest {
    private MockWebServiceClient mockClient;
    private Resource xsd;
    @Before
    public void createClient() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(ApplicationConfig.class);
        context.refresh();
        Assert.assertNotNull(context.getBean(SimpleEndpoint.class));
        xsd = context.getResource("file:./src/main/webapp/WEB-INF/simple.xsd");
        mockClient = MockWebServiceClient.createClient(context);
    }
    @Test
    public void outcomesEndpointTest() throws Exception {
        Source requestPayload = new StringSource("<wsc:SimpleRequest "
                + "xmlns:wsc=\"http://javacaffeine.com/service/schemas\">"
                + "<wsc:a>1</wsc:a>"
                + "<wsc:b>2</wsc:b>"
                + "</wsc:SimpleRequest>");
        Source responsePayload = new StringSource("<wsc:SimpleResponse "
                + "xmlns:wsc=\"http://javacaffeine.com/service/schemas\">"
                + "<wsc:sum>The sum of A(1) and B(2) is 3</wsc:sum>"
                + "</wsc:SimpleResponse>");
        mockClient.sendRequest(withPayload(requestPayload))
        .andExpect(validPayload(xsd)); // validate based on the XSD.
        
        mockClient.sendRequest(withPayload(requestPayload))
        .andExpect(payload(responsePayload)); // validate based on response.
    }
}
@Configuration
@ComponentScan("com.cursino.wilson")
class ApplicationConfig {}// Workaround to register the annotated beans.

simple.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:wsc="http://javacaffeine.com/service/schemas"
 elementFormDefault="qualified"
 targetNamespace="http://javacaffeine.com/service/schemas">
    <xs:element name="SimpleRequest">
       <xs:complexType>
       <xs:sequence>
          <xs:element name="atype="xs:integer" />
          <xs:element name="btype="xs:integer" />
       </xs:sequence>
    </xs:complexType>
    </xs:element>
       <xs:element name="SimpleResponse">
         <xs:complexType>
       <xs:sequence>
         <xs:element name="sumtype="xs:string" />
           </xs:sequence>
      </xs:complexType>
    </xs:element>
</xs:schema>

spring-ws-servlet.xml:

<?xml version="1.0encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:sws="http://www.springframework.org/schema/web-services"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
http://www.springframework.org/schema/web-services 
http://www.springframework.org/schema/web-services/web-services-2.0.xsd     
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <context:component-scan base-package="com.cursino.wilson" />
    <sws:annotation-driven />
    <sws:dynamic-wsdl 
        id="simple
        portTypeName="SimplePort"
        locationUri="/service/
        targetNamespace="http://javacaffeine.com/service/definitions">
        <sws:xsd location="/WEB-INF/simple.xsd" />
    </sws:dynamic-wsdl>
</beans>

web.xml: 
<?xml version="1.0encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd
"
    id="simpleSpringWS"
    version="3.0"
    metadata-complete="true">
    <servlet>
        <servlet-name>spring-ws</servlet-name>
        <servlet-class>org.springframework.ws.transport.http.MessageDispatcherServlet</servlet-class>
        <init-param>
            <param-name>transformWsdlLocations</param-name>
            <param-value>true</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-ws</servlet-name>
        <url-pattern>/service/*</url-pattern>
    </servlet-mapping>
</web-app>

pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0xmlns:xsi="http://www.w3.org/2001/XMLSchema-instancexsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  http://maven.apache.org/xsd/maven-4.0.0.xsd
">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.cursino.wilson</groupId>
    <artifactId>simpleSpringWS</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warName>simpleSpringWS</warName>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>tomcat-maven-plugin</artifactId>
                <version>1.1</version>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- Test -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-test</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <!-- Business layer -->
        <dependency>
            <groupId>com.wilson.cursino</groupId>
            <artifactId>PaiGowDraw</artifactId>
            <version>0.1-SNAPSHOT</version>
        </dependency>
        <!-- Spring Webservice -->
        <dependency>
            <groupId>org.springframework.ws</groupId>
            <artifactId>spring-ws-core</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <!-- Parsers -->
        <dependency>
            <groupId>org.jdom</groupId>
            <artifactId>jdom</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>jaxen</groupId>
            <artifactId>jaxen</artifactId>
            <version>1.1</version>
        </dependency>
    </dependencies>
</project>

Run the project with Cargo:
mvn clean verify org.codehaus.cargo:cargo-maven2-plugin:run -Dcargo.maven.containerId=tomcat7x -Dcargo.maven.containerUrl=http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.16/bin/apache-tomcat-7.0.16.zip

It will download and run a Tomcat in local.

simple.wsdl  
Based on the XSD and some conventions, Spring-WS can create the WSDL. You can access it through http://localhost:8080/simpleSpringWS/service/simple.wsdl

SoapUI:

With SoapUI (www.soapui.org) you can easily test test web service.



Monday, April 7, 2014

Baccarat Game

Recently I was asked to create a simple code that generate a Baccarat statistics.

you can find more about the rules and how to play here: http://en.wikipedia.org/wiki/Baccarat_(card_game)

BACCARAT class
/**  * Game class, which represents a Baccarat game. It will have two main methods, one for  * playing the game and another to retrieve the statistics. A third method to print  * results will be also available.  * @author Wilson Soethe Cursino  * @since 1.0  */
public class Baccarat {
    private Shoedeck shoedeck;
    private final static int DEFAULT_DECKS_TO_USE = 8;
    private final static int MINIMUM_CARDS_IN_DECK = 6;
    public Baccarat() {
        shoedeck = new Shoedeck(DEFAULT_DECKS_TO_USE);
        shoedeck.shuffle();
    }
    public BaccaratStats statistics() {
        BaccaratStats baccaratStats = new BaccaratStats();
        int shoeSize = shoedeck.getShoe().size();
        if (shoeSize < 6)
            return baccaratStats;
        int[] cards = shoedeck.getQtyInShoeByValue();
        double playerWinCombs = 0;
        double bankerWinCombs = 0;
        double tieComb = 0;
        for (int playerCardOneCount = 0; playerCardOneCount < 10; playerCardOneCount++) {
            double playerCardOneWinComb = cards[playerCardOneCount]--;
            shoeSize--;
            for (int bankerCardOneCount = 0; bankerCardOneCount < 10; bankerCardOneCount++) {
                double bankerCardOneWinComb = playerCardOneWinComb * cards[bankerCardOneCount]--;
                shoeSize--;
                for (int playerCardTwoCount = 0; playerCardTwoCount < 10; playerCardTwoCount++) {
                    double playerCardTwoWinComb = bankerCardOneWinComb * cards[playerCardTwoCount]--;
                    shoeSize--;
                    for (int bankerCardTwoCount = 0; bankerCardTwoCount < 10; bankerCardTwoCount++) {
                        double bankerCardTwoWinComb = playerCardTwoWinComb * cards[bankerCardTwoCount]--;
                        shoeSize--;
                        int playerTotalPoints = (playerCardOneCount + playerCardTwoCount) % 10;
                        int bankerTotalPoints = (bankerCardOneCount + bankerCardTwoCount) % 10;
                        if (((playerTotalPoints >= 8) || (bankerTotalPoints >= 8))
                                || ((playerTotalPoints >= 6) && (bankerTotalPoints >= 6))) {
                            if (bankerTotalPoints > playerTotalPoints) {
                                bankerWinCombs += bankerCardTwoWinComb * shoeSize * (shoeSize - 1);
                            } else if (playerTotalPoints > bankerTotalPoints) {
                                playerWinCombs += bankerCardTwoWinComb * shoeSize * (shoeSize - 1);
                            } else {
                                tieComb += bankerCardTwoWinComb * shoeSize * (shoeSize - 1);
                            }
                        } else if (playerTotalPoints < 6) {
                            for (int playerCardThreeCount = 0; playerCardThreeCount < 10; playerCardThreeCount++) {
                                double playerCardThreeWinComb = bankerCardTwoWinComb * cards[playerCardThreeCount]--;
                                shoeSize--;
                                int playerTotalPointsWithThirdCard = (playerCardOneCount + playerCardTwoCount + playerCardThreeCount) % 10;
                                boolean bankerGetsExtraCard = true;
                                if (bankerTotalPoints == 7) {
                                    bankerGetsExtraCard = false;
                                } else if ((bankerTotalPoints == 6) && ((playerCardThreeCount < 6) || (playerCardThreeCount > 7))) {
                                    bankerGetsExtraCard = false;
                                } else if ((bankerTotalPoints == 5) && ((playerCardThreeCount < 4) || (playerCardThreeCount > 7))) {
                                    bankerGetsExtraCard = false;
                                } else if ((bankerTotalPoints == 4) && ((playerCardThreeCount < 2) || (playerCardThreeCount > 7))) {
                                    bankerGetsExtraCard = false;
                                } else if ((bankerTotalPoints == 3) && (playerCardThreeCount == 8)) {
                                    bankerGetsExtraCard = false;
                                }
                                if (bankerGetsExtraCard) {
                                    for (int bankerCardThreeCount = 0; bankerCardThreeCount < 10; bankerCardThreeCount++) {
                                        double bankerCardThreeWinComb = playerCardThreeWinComb * cards[bankerCardThreeCount]--;
                                        int bankerTotalWithThirdCard = (bankerCardOneCount + bankerCardTwoCount + bankerCardThreeCount) % 10;
                                        if (bankerTotalWithThirdCard > playerTotalPointsWithThirdCard) {
                                            bankerWinCombs += bankerCardThreeWinComb;
                                        } else if (playerTotalPointsWithThirdCard > bankerTotalWithThirdCard) {
                                            playerWinCombs += bankerCardThreeWinComb;
                                        } else {
                                            tieComb += bankerCardThreeWinComb;
                                        }
                                        cards[bankerCardThreeCount]++;
                                    }
                                } else {
                                    if (bankerTotalPoints > playerTotalPointsWithThirdCard) {
                                        bankerWinCombs += playerCardThreeWinComb * shoeSize;
                                    } else if (playerTotalPointsWithThirdCard > bankerTotalPoints) {
                                        playerWinCombs += playerCardThreeWinComb * shoeSize;
                                    } else {
                                        tieComb += playerCardThreeWinComb * shoeSize;
                                    }
                                }
                                cards[playerCardThreeCount]++;
                                shoeSize++;
                            }
                        } else if (bankerTotalPoints < 6) {
                            for (int bankerCardThreeCount = 0; bankerCardThreeCount < 10; bankerCardThreeCount++) {
                                double bankerCardThreeWinComb = bankerCardTwoWinComb * cards[bankerCardThreeCount]--;
                                shoeSize--;
                                int bankerTotalPointsWithThirdCard = (bankerCardOneCount + bankerCardTwoCount + bankerCardThreeCount) % 10;
                                if (bankerTotalPointsWithThirdCard > playerTotalPoints) {
                                    bankerWinCombs += bankerCardThreeWinComb * shoeSize;
                                } else if (playerTotalPoints > bankerTotalPointsWithThirdCard) {
                                    playerWinCombs += bankerCardThreeWinComb * shoeSize;
                                } else {
                                    tieComb += bankerCardThreeWinComb * shoeSize;
                                }
                                cards[bankerCardThreeCount]++;
                                shoeSize++;
                            }
                        }
                        cards[bankerCardTwoCount]++;
                        shoeSize++;
                    }
                    cards[playerCardTwoCount]++;
                    shoeSize++;
                }
                cards[bankerCardOneCount]++;
                shoeSize++;
            }
            cards[playerCardOneCount]++;
            shoeSize++;
        }
        double totalComb = bankerWinCombs + playerWinCombs + tieComb;
        baccaratStats.setTotalCombinations(totalComb);
        double bankerWinnerProbability = bankerWinCombs / totalComb;
        double playerWinnnerProbability = playerWinCombs / totalComb;
        double tieProbability = tieComb / totalComb;
        double bankerOdds = bankerWinCombs / (playerWinCombs + tieComb);
        double playerOdds = playerWinCombs / (bankerWinCombs + tieComb);
        double tieOdds = tieComb / (playerWinCombs + bankerWinCombs);
        baccaratStats.setBankerWinnerProbability(bankerWinnerProbability);
        baccaratStats.setPlayerWinnnerProbability(playerWinnnerProbability);
        baccaratStats.setTieProbability(tieProbability);
        baccaratStats.setBankerOdds(bankerOdds);
        baccaratStats.setPlayerOdds(playerOdds);
        baccaratStats.setTieOdds(tieOdds);
        return baccaratStats;
    }
    /**      * This method will play once and will remove the cards from the shoedeck.      * @return simple game result (TIE, BANKER or PLAYER)      */
    public BaccaratResult playOnce() {
        BaccaratResult result = new BaccaratResult();
        if (shoedeck.getShoe().size() < MINIMUM_CARDS_IN_DECK) {
            result.setGameResult(GameResult.FINAL_GAME);
            return result;
        }
        Hand playerHand = new Hand();
        Hand bankerHand = new Hand();
        playerHand.addCard(shoedeck.popCard());
        bankerHand.addCard(shoedeck.popCard());
        playerHand.addCard(shoedeck.popCard());
        bankerHand.addCard(shoedeck.popCard());
        int playerThirdCardValue = -1;
        if (!(bankerHand.points() >= 8 || playerHand.points() >= 8) && playerHand.points() <= 5) {
            Card thirdCard = shoedeck.popCard();
            playerHand.addCard(thirdCard);
            playerThirdCardValue = thirdCard.getValue().getValue();
        }
        boolean bankerGetsExtraCard = true;
        if (playerThirdCardValue == -1) {
            if (bankerHand.points() >= 8 || playerHand.points() >= 8) {
                bankerGetsExtraCard = false;
            }
            if (bankerHand.points() == 7) {
                bankerGetsExtraCard = false;
            }
            if (playerHand.points() >= 6) {
                if (bankerHand.points() == 6) {
                    bankerGetsExtraCard = false;
                } else {
                    bankerGetsExtraCard = true;
                }
            }
        } else {
            switch (bankerHand.points()) {
            case 0:
            case 1:
            case 2:
                bankerGetsExtraCard = true;
            case 3:
                if (playerThirdCardValue != 8)
                    bankerGetsExtraCard = true;
                break;
            case 4:
                if (playerThirdCardValue >= 2 && playerThirdCardValue <= 7)
                    bankerGetsExtraCard = true;
                break;
            case 5:
                if (playerThirdCardValue >= 4 && playerThirdCardValue <= 7)
                    bankerGetsExtraCard = true;
                break;
            case 6:
                if (playerThirdCardValue == 6 || playerThirdCardValue == 7)
                    bankerGetsExtraCard = true;
                break;
            }
            bankerGetsExtraCard = false;
        }
        if (bankerGetsExtraCard) {
            bankerHand.addCard(shoedeck.popCard());
        }
        GameResult gameResult;
        if (bankerHand.points() - playerHand.points() == 0) {
            gameResult = GameResult.TIE;
        } else if (bankerHand.points() - playerHand.points() > 0) {
            gameResult = GameResult.BANKER;
        } else {
            gameResult = GameResult.PLAYER;
        }
        result.setGameResult(gameResult);
        result.setBankerHand(bankerHand);
        result.setPlayerHand(playerHand);
        return result;
    }
}