-
Notifications
You must be signed in to change notification settings - Fork 0
Create OrderProcessor.java #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,35 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class OrderProcessor { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final String MODE_NEW = "NEW"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final String MODE_LEGACY = "LEGACY"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private static final Map<String, String> serverModes = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| serverModes.put("ServerA", MODE_NEW); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| serverModes.put("ServerB", MODE_LEGACY); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| processOrder("ServerA", 100, "BUY"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| processOrder("ServerB", 100, "BUY"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void processOrder(String server, int qty, String side) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| String mode = serverModes.get(server); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (MODE_NEW.equals(mode)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| placeOrder(qty, side); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else if (MODE_LEGACY.equals(mode)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < 1000; i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| placeOrder(qty, side); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+25
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider making the legacy order count configurable. The hard-coded value of 1000 orders in LEGACY mode reduces flexibility and could cause performance issues. + private static final int LEGACY_ORDER_COUNT = 1000;
+
public static void processOrder(String server, int qty, String side) {
// ... validation code ...
if (MODE_NEW.equals(mode)) {
placeOrder(qty, side);
} else if (MODE_LEGACY.equals(mode)) {
- for (int i = 0; i < 1000; i++) {
+ for (int i = 0; i < LEGACY_ORDER_COUNT; i++) {
placeOrder(qty, side);
}
}
}Alternatively, consider making this configurable per server or adding a parameter to control the count. 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Handle unknown servers and add input validation. The method doesn't handle cases where an unknown server is passed, leading to silent failures. Additionally, the side parameter should be validated. public static void processOrder(String server, int qty, String side) {
+ if (server == null || server.trim().isEmpty()) {
+ throw new IllegalArgumentException("Server name cannot be null or empty");
+ }
+ if (qty <= 0) {
+ throw new IllegalArgumentException("Quantity must be positive");
+ }
+ if (!"BUY".equals(side) && !"SELL".equals(side)) {
+ throw new IllegalArgumentException("Side must be either 'BUY' or 'SELL'");
+ }
+
String mode = serverModes.get(server);
+ if (mode == null) {
+ throw new IllegalArgumentException("Unknown server: " + server);
+ }
+
if (MODE_NEW.equals(mode)) {
placeOrder(qty, side);
} else if (MODE_LEGACY.equals(mode)) {
for (int i = 0; i < 1000; i++) {
placeOrder(qty, side);
}
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static void placeOrder(int qty, String side) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| System.out.println("Placed order: " + side + " " + qty + " shares"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider making the server modes map immutable for thread safety.
The static map could be accessed concurrently in a multi-threaded environment. Consider using
Map.of()orCollections.unmodifiableMap()to make it immutable.📝 Committable suggestion
🤖 Prompt for AI Agents