-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTradeProcessor.java
63 lines (51 loc) · 2.16 KB
/
TradeProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class TradeProcessor implements BrokerService {
private WalletService walletService = new WalletService();
private ShareService shareService = new ShareService();
@Override
public boolean validateTrade(Trade trade) {
if (!trade.isValid()) return false;
// Specific validation based on trade type
if (trade.getAction() == TradeAction.BUY) {
return walletService.checkWalletBalance(trade.getTraderId(), trade.getSharePrice() * trade.getShareQuantity());
} else if (trade.getAction() == TradeAction.SELL) {
return shareService.checkShareQuantity(trade.getTraderId(), trade.getShareName(), trade.getShareQuantity());
}
return false;
}
@Override
public String bookTrade(Trade trade) {
// Generate internal TradeId if not present (for new trades)
if (trade.getTradeId() == null) {
trade.setTradeId(generateTradeId());
}
// Blocking resources
if (trade.getAction() == TradeAction.BUY) {
walletService.blockAmount(trade.getTraderId(), trade.getSharePrice() * trade.getShareQuantity());
} else if (trade.getAction() == TradeAction.SELL) {
shareService.blockShares(trade.getTraderId(), trade.getShareName(), trade.getShareQuantity());
}
// Send trade to Share Exchange via messaging system (Kafka)
sendTradeToExchange(trade);
// Store trade in database
storeTrade(trade);
return trade.getTradeId();
}
@Override
public void updateTrade(Trade trade) {
// Similar logic for updating the trade
}
@Override
public void reportTradeStatus(String tradeId) {
// Logic to fetch trade status and report back to the initiator
}
private String generateTradeId() {
// Logic to generate a unique TradeId
return UUID.randomUUID().toString();
}
private void sendTradeToExchange(Trade trade) {
// Integration with Kafka/MQ to send the trade details
}
private void storeTrade(Trade trade) {
// Save trade into the database
}
}