//snippet-sourcedescription:[StockTrade.java is a helper class]
//snippet-keyword:[Java]
//snippet-sourcesyntax:[java]
//snippet-keyword:[SDK for Java 2.0]
//snippet-keyword:[Code Sample]
//snippet-keyword:[Amazon Kinesis]
//snippet-service:[kinesis]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[3-26-2020]
//snippet-sourceauthor:scmacdon - AWS]
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package com.example.kinesis;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;

/**
 * Generates random stock trades by picking randomly from a collection of stocks, assigning a
 * random price based on the mean, and picking a random quantity for the shares.
 *
 */

public class StockTradeGenerator {

    private static final List<StockPrice> STOCK_PRICES = new ArrayList<StockPrice>();
    static {
        STOCK_PRICES.add(new StockPrice("AAPL", 119.72));
        STOCK_PRICES.add(new StockPrice("XOM", 91.56));
        STOCK_PRICES.add(new StockPrice("GOOG", 527.83));
        STOCK_PRICES.add(new StockPrice("BRK.A", 223999.88));
        STOCK_PRICES.add(new StockPrice("MSFT", 42.36));
        STOCK_PRICES.add(new StockPrice("WFC", 54.21));
        STOCK_PRICES.add(new StockPrice("JNJ", 99.78));
        STOCK_PRICES.add(new StockPrice("WMT", 85.91));
        STOCK_PRICES.add(new StockPrice("CHL", 66.96));
        STOCK_PRICES.add(new StockPrice("GE", 24.64));
        STOCK_PRICES.add(new StockPrice("NVS", 102.46));
        STOCK_PRICES.add(new StockPrice("PG", 85.05));
        STOCK_PRICES.add(new StockPrice("JPM", 57.82));
        STOCK_PRICES.add(new StockPrice("RDS.A", 66.72));
        STOCK_PRICES.add(new StockPrice("CVX", 110.43));
        STOCK_PRICES.add(new StockPrice("PFE", 33.07));
        STOCK_PRICES.add(new StockPrice("FB", 74.44));
        STOCK_PRICES.add(new StockPrice("VZ", 49.09));
        STOCK_PRICES.add(new StockPrice("PTR", 111.08));
        STOCK_PRICES.add(new StockPrice("BUD", 120.39));
        STOCK_PRICES.add(new StockPrice("ORCL", 43.40));
        STOCK_PRICES.add(new StockPrice("KO", 41.23));
        STOCK_PRICES.add(new StockPrice("T", 34.64));
        STOCK_PRICES.add(new StockPrice("DIS", 101.73));
        STOCK_PRICES.add(new StockPrice("AMZN", 370.56));
    }

    /** The ratio of the deviation from the mean price **/
    private static final double MAX_DEVIATION = 0.2; // ie 20%

    /** The number of shares is picked randomly between 1 and the MAX_QUANTITY **/
    private static final int MAX_QUANTITY = 10000;

    /** Probability of trade being a sell **/
    private static final double PROBABILITY_SELL = 0.4; // ie 40%

    private final Random random = new Random();
    private AtomicLong id = new AtomicLong(1);

    /**
     * Return a random stock trade with a unique id every time.
     *
     */
    public StockTrade getRandomTrade() {
        // pick a random stock
        StockPrice stockPrice = STOCK_PRICES.get(random.nextInt(STOCK_PRICES.size()));
        // pick a random deviation between -MAX_DEVIATION and +MAX_DEVIATION
        double deviation = (random.nextDouble() - 0.5) * 2.0 * MAX_DEVIATION;
        // set the price using the deviation and mean price
        double price = stockPrice.price * (1 + deviation);
        // round price to 2 decimal places
        price = Math.round(price * 100.0) / 100.0;

        // set the trade type to buy or sell depending on the probability of sell
        StockTrade.TradeType tradeType = StockTrade.TradeType.BUY;
        if (random.nextDouble() < PROBABILITY_SELL) {
            tradeType = StockTrade.TradeType.SELL;
        }

        // randomly pick a quantity of shares
        long quantity = random.nextInt(MAX_QUANTITY) + 1; // add 1 because nextInt() will return between 0 (inclusive)
        // and MAX_QUANTITY (exclusive). we want at least 1 share.

        return new StockTrade(stockPrice.tickerSymbol, tradeType, price, quantity, id.getAndIncrement());
    }

    private static class StockPrice {
        String tickerSymbol;
        double price;

        StockPrice(String tickerSymbol, double price) {
            this.tickerSymbol = tickerSymbol;
            this.price = price;
        }
    }
}

@@ -55,18 +55,19 @@
 er class
+.
 %5D%0A
-
 //snippe
@@ -1260,17 +1260,16 @@
 e shares
-.
 %0A *%0A */%0A
@@ -3106,16 +3106,20 @@
 lity of 
+the 
 trade be
@@ -3353,18 +3353,18 @@
  unique 
-id
+ID
  every t
@@ -3366,17 +3366,16 @@
 ery time
-.
 %0A     *%0A
@@ -3426,33 +3426,33 @@
 () %7B%0A        // 
-p
+P
 ick a random sto
@@ -3552,17 +3552,17 @@
      // 
-p
+P
 ick a ra
@@ -3703,25 +3703,25 @@
 %0A        // 
-s
+S
 et the price
@@ -3826,17 +3826,17 @@
      // 
-r
+R
 ound pri
@@ -3921,17 +3921,17 @@
      // 
-s
+S
 et the t
@@ -4186,17 +4186,17 @@
      // 
-r
+R
 andomly 
@@ -4220,16 +4220,17 @@
 f shares
+.
 %0A       
@@ -4283,17 +4283,17 @@
 + 1; // 
-a
+A
 dd 1 bec
@@ -4382,17 +4382,17 @@
 usive). 
-w
+W
 e want a

Update StockTradeGenerator.java