@Test public void testThatYouCannotBuyIfYouRunOutOfMoney(TestContext tc) { Async async = tc.async(); service.buy(10000, quote("A", 10, 20, 100000), ar -> { tc.assertTrue(ar.failed()); tc.assertTrue(ar.cause().getMessage().contains("not enough money")); async.complete(); }); }
@Override public void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot buy " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } if (quote.getInteger("shares") < amount) { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " - not enough " + "stocks on the market (" + quote.getInteger("shares") + ")")); return; } double price = amount * quote.getDouble("ask"); String name = quote.getString("name"); if (portfolio.getCash() >= price) { portfolio.setCash(portfolio.getCash() - price); int current = portfolio.getAmount(name); int newAmount = current + amount; portfolio.getShares().put(name, newAmount); sendActionOnTheEventBus("BUY", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " of " + name + " - " + "not enough money, " + "need " + price + ", has " + portfolio.getCash())); } }
PortfolioServiceImpl implements PortfolioService { @Override public void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot buy " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } if (quote.getInteger("shares") < amount) { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " - not enough " + "stocks on the market (" + quote.getInteger("shares") + ")")); return; } double price = amount * quote.getDouble("ask"); String name = quote.getString("name"); if (portfolio.getCash() >= price) { portfolio.setCash(portfolio.getCash() - price); int current = portfolio.getAmount(name); int newAmount = current + amount; portfolio.getShares().put(name, newAmount); sendActionOnTheEventBus("BUY", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " of " + name + " - " + "not enough money, " + "need " + price + ", has " + portfolio.getCash())); } } }
PortfolioServiceImpl implements PortfolioService { @Override public void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot buy " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } if (quote.getInteger("shares") < amount) { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " - not enough " + "stocks on the market (" + quote.getInteger("shares") + ")")); return; } double price = amount * quote.getDouble("ask"); String name = quote.getString("name"); if (portfolio.getCash() >= price) { portfolio.setCash(portfolio.getCash() - price); int current = portfolio.getAmount(name); int newAmount = current + amount; portfolio.getShares().put(name, newAmount); sendActionOnTheEventBus("BUY", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " of " + name + " - " + "not enough money, " + "need " + price + ", has " + portfolio.getCash())); } } PortfolioServiceImpl(Vertx vertx, ServiceDiscovery discovery, double initialCash); }
PortfolioServiceImpl implements PortfolioService { @Override public void buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot buy " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } if (quote.getInteger("shares") < amount) { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " - not enough " + "stocks on the market (" + quote.getInteger("shares") + ")")); return; } double price = amount * quote.getDouble("ask"); String name = quote.getString("name"); if (portfolio.getCash() >= price) { portfolio.setCash(portfolio.getCash() - price); int current = portfolio.getAmount(name); int newAmount = current + amount; portfolio.getShares().put(name, newAmount); sendActionOnTheEventBus("BUY", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " of " + name + " - " + "not enough money, " + "need " + price + ", has " + portfolio.getCash())); } } 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 buy(int amount, JsonObject quote, Handler<AsyncResult<Portfolio>> resultHandler) { if (amount <= 0) { resultHandler.handle(Future.failedFuture("Cannot buy " + quote.getString("name") + " - the amount must be " + "greater than 0")); return; } if (quote.getInteger("shares") < amount) { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " - not enough " + "stocks on the market (" + quote.getInteger("shares") + ")")); return; } double price = amount * quote.getDouble("ask"); String name = quote.getString("name"); if (portfolio.getCash() >= price) { portfolio.setCash(portfolio.getCash() - price); int current = portfolio.getAmount(name); int newAmount = current + amount; portfolio.getShares().put(name, newAmount); sendActionOnTheEventBus("BUY", amount, quote, newAmount); resultHandler.handle(Future.succeededFuture(portfolio)); } else { resultHandler.handle(Future.failedFuture("Cannot buy " + amount + " of " + name + " - " + "not enough money, " + "need " + price + ", has " + portfolio.getCash())); } } 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);  }