Your question is Java Code Review and Improvements. Take a moment with it on the right.
Talk me through your thinking if you like. When you're confident, submit your answer and I'll grade it like a real screen (7/10 or better passes).
Morgan Stanley's trade settlement service processes batches of trades submitted by different desks throughout the day. A teammate wrote the class below to mark trades as settled and keep a running total, and it's now going through code review before it ships.
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class SettlementProcessor {
private List<String> processedTradeIds = new ArrayList<>();
private BigDecimal totalSettled = BigDecimal.ZERO;
public void processBatch(List<Trade> trades) {
for (Trade trade : trades) {
try {
if (processedTradeIds.contains(trade.getId())) {
continue;
}
if (trade.getStatus() == "SETTLED") {
continue;
}
settle(trade);
processedTradeIds.add(trade.getId());
} catch (Exception e) {
// ignore and move on
}
}
}
private void settle(Trade trade) {
totalSettled = totalSettled.add(trade.getAmount());
DatabaseConnection conn = new DatabaseConnection();
conn.open();
conn.write(trade.getId(), trade.getAmount());
}
public BigDecimal getTotalSettled() {
return totalSettled;
}
}
Review this code and explain what you would flag, and what core design changes you'd suggest so it's safe to call processBatch from multiple settlement batches running at the same time. Explain your reasoning, you don't need to write out the full corrected file.