@Test public void testYouCannotSellANegativeAmount(TestContext tc) { Async async = tc.async(); service.sell(-1, quote("A", 10, 20, 100), ar -> { tc.assertTrue(ar.failed()); async.complete(); }); }
@Override public void sell(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot sell " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } double price = amount * quote.getDouble("bid"); String name = quote.getString("name"); int current = portfolio.getAmount(name); if (current >= amount) { int newAmount = current - amount; if (newAmount == 0) { portfolio.getShares().remove(name); } else { portfolio.getShares().put(name, newAmount); } portfolio.setCash(portfolio.getCash() + price); sendActionOnTheEventBus("SELL", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot sell " + amount + " of " + name + " - " + "not enough stocks " + "in portfolio")); } }
PortfolioServiceImpl implements PortfolioService { @Override public void sell(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot sell " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } double price = amount * quote.getDouble("bid"); String name = quote.getString("name"); int current = portfolio.getAmount(name); if (current >= amount) { int newAmount = current - amount; if (newAmount == 0) { portfolio.getShares().remove(name); } else { portfolio.getShares().put(name, newAmount); } portfolio.setCash(portfolio.getCash() + price); sendActionOnTheEventBus("SELL", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot sell " + amount + " of " + name + " - " + "not enough stocks " + "in portfolio")); } } }
PortfolioServiceImpl implements PortfolioService { @Override public void sell(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot sell " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } double price = amount * quote.getDouble("bid"); String name = quote.getString("name"); int current = portfolio.getAmount(name); if (current >= amount) { int newAmount = current - amount; if (newAmount == 0) { portfolio.getShares().remove(name); } else { portfolio.getShares().put(name, newAmount); } portfolio.setCash(portfolio.getCash() + price); sendActionOnTheEventBus("SELL", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot sell " + amount + " of " + name + " - " + "not enough stocks " + "in portfolio")); } } PortfolioServiceImpl(Vertx vertx, ServiceDiscovery discovery, double initialCash); }
PortfolioServiceImpl implements PortfolioService { @Override public void sell(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot sell " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } double price = amount * quote.getDouble("bid"); String name = quote.getString("name"); int current = portfolio.getAmount(name); if (current >= amount) { int newAmount = current - amount; if (newAmount == 0) { portfolio.getShares().remove(name); } else { portfolio.getShares().put(name, newAmount); } portfolio.setCash(portfolio.getCash() + price); sendActionOnTheEventBus("SELL", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot sell " + amount + " of " + name + " - " + "not enough stocks " + "in portfolio")); } } PortfolioServiceImpl(Vertx vertx, ServiceDiscovery discovery, double initialCash); @Override void getPortfolio(Handler<AsyncResult<Portfolio>> resultHandler); @Override void evaluate(Handler<AsyncResult<Double>> resultHandler); @Override void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler); @Override void sell(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler); }
PortfolioServiceImpl implements PortfolioService { @Override public void sell(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot sell " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } double price = amount * quote.getDouble("bid"); String name = quote.getString("name"); int current = portfolio.getAmount(name); if (current >= amount) { int newAmount = current - amount; if (newAmount == 0) { portfolio.getShares().remove(name); } else { portfolio.getShares().put(name, newAmount); } portfolio.setCash(portfolio.getCash() + price); sendActionOnTheEventBus("SELL", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot sell " + amount + " of " + name + " - " + "not enough stocks " + "in portfolio")); } } PortfolioServiceImpl(Vertx vertx, ServiceDiscovery discovery, double initialCash); @Override void getPortfolio(Handler<AsyncResult<Portfolio>> resultHandler); @Override void evaluate(Handler<AsyncResult<Double>> resultHandler); @Override void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler); @Override void sell(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler);  }