Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. public boolean executeBid(int userId, String stockCode, int quantity, double maxPrice)
  2. throws UserException, StockException, BalanceException {
  3. User user = users.get(userId);
  4. if(user==null) {throw new UserException();}
  5. Stock s = stocks.get(stockCode);
  6. if(s==null) {throw new StockException();}
  7. if (user.getBalance()<maxPrice*quantity){throw new BalanceException();}
  8. user.addToBid(s, quantity);
  9. List<Stock> selling = users.values().stream().flatMap(p->p.getProposal().entrySet().stream())
  10. .filter(p->p.getKey().getCode().equals(stockCode))
  11. .sorted(Comparator.comparing(p->p.getKey().getPrice())).map(p->p.getKey()).collect(Collectors.toList());
  12. if(selling.size()!=0) {
  13. selling.get(0).getUser().executeBid(quantity, maxPrice, s);
  14. user.reduceAmount(maxPrice*quantity);
  15. user.addStock(s, quantity);
  16. return true;}
  17. return false;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement