import com.neopets.auth.ClasspathPropertiesFileCredentialsProvider;
import com.neopets.auth.NeopetsCredentialsProvider;
import com.neopets.services.stockmarket.NeopetsStockMarket;
import com.neopets.services.stockmarket.NeopetsStockMarketClient;
import com.neopets.services.stockmarket.model.Stock;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class NeopetsStockMarketSample {

  public static final String CREDENTIALS_PATH = "/npCredentials.properties";

  public static void main(String[] args) throws Exception {
    NeopetsCredentialsProvider provider = new ClasspathPropertiesFileCredentialsProvider(CREDENTIALS_PATH);
    NeopetsStockMarket market = new NeopetsStockMarketClient(provider.getCredentials());

    List<Stock> stocks = market.listAllStocks().getStocks();

    Collections.sort(stocks, new Comparator<Stock>() {
      @Override
      public int compare(Stock o1, Stock o2) {
        return o1.getCurrentPrice() - o2.getCurrentPrice();
      }
    });

    for (Stock stock : stocks) {
      if (stock.getCurrentPrice() == 15) {
        System.out.println("Buying 10 shares of " + stock.getTickerSymbol() + ".");
        market.buyShares(stock, 10);
        break;
      }
    }
  }

}

@@ -285,21 +285,17 @@
 t.model.
-Stock
+*
 ;%0A%0Aimpor
@@ -1114,64 +1114,231 @@
 ln(%22
-Buying 10 shares of %22 + stock.getTickerSymbol() + %22.
+===========================================%22);%0A        System.out.printf(%22Buying 10 shares of %25s at 15 NP per share.%5Cn%22, stock.getTickerSymbol());%0A        System.out.println(%22===========================================
 %22);%0A
+%0A
     
@@ -1398,16 +1398,1445 @@
  %7D%0A    %7D
+%0A%0A    GetPortfolioResult result = market.getPortfolio();%0A    Portfolio portfolio = result.getPortfolio();%0A%0A    System.out.println(%22Portfolio:%22);%0A    System.out.println(%22===========================================%22);%0A    for (PurchasedStock stock : portfolio.getStocks()) %7B%0A      System.out.printf(%22Stock: %25s%5CtAmount: %25d%5CtPaid: %25d%5Cn%22,%0A          stock.getTickerSymbol(), stock.getAmount(), stock.getPaid());%0A    %7D%0A%0A    boolean sellingShares = false;%0A    PurchasedStock stockToSell = null;%0A    int amountToSell = 5;%0A%0A    for (PurchasedStock stock : portfolio.getStocks()) %7B%0A      if (stock.getCurrentPrice() %3E 15 && stock.getAmount() %3E= amountToSell) %7B%0A        sellingShares = true;%0A        stockToSell = stock;%0A%0A        for (PurchasedShares shares : stock.getSharesList()) %7B%0A          if (shares.getAmount() %3E= amountToSell) %7B%0A            shares.setSellAmount(amountToSell);%0A          %7D%0A          else %7B%0A            shares.setSellAmount(shares.getAmount());%0A            amountToSell -= shares.getAmount();%0A          %7D%0A        %7D%0A        break;%0A      %7D%0A    %7D%0A%0A    if (sellingShares) %7B%0A      System.out.println(%22===========================================%22);%0A      System.out.printf(%22Selling %25d shares of %25s at %25d NP per share.%5Cn%22,%0A          amountToSell, stockToSell.getTickerSymbol(), stockToSell.getCurrentPrice());%0A      System.out.println(%22===========================================%22);%0A%0A      market.sellShares(portfolio);%0A    %7D
 %0A  %7D%0A%0A%7D%0A

Update stock market sample showing new calls