Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public boolean executeBid(int userId, String stockCode, int quantity, double maxPrice)
- throws UserException, StockException, BalanceException {
- User user = users.get(userId);
- if(user==null) {throw new UserException();}
- Stock s = stocks.get(stockCode);
- if(s==null) {throw new StockException();}
- if (user.getBalance()<maxPrice*quantity){throw new BalanceException();}
- user.addToBid(s, quantity);
- List<Stock> selling = users.values().stream().flatMap(p->p.getProposal().entrySet().stream())
- .filter(p->p.getKey().getCode().equals(stockCode))
- .sorted(Comparator.comparing(p->p.getKey().getPrice())).map(p->p.getKey()).collect(Collectors.toList());
- if(selling.size()!=0) {
- selling.get(0).getUser().executeBid(quantity, maxPrice, s);
- user.reduceAmount(maxPrice*quantity);
- user.addStock(s, quantity);
- return true;}
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement