import java.util.HashMap;
import java.util.Map;
public class URLParameterMonitor {
private Map<String, String> currentParams = new HashMap<>();
public void addParameter(String key, String value) {
currentParams.put(key, value);
}
public String getParameter(String key) {
return currentParams.getOrDefault(key, null); // Return null if key not found
}
public void removeParameter(String key) {
currentParams.remove(key);
}
public Map<String, String> getCurrentParams() {
return new HashMap<>(currentParams); // Return a copy to prevent external modification
}
public static void main(String[] args) {
URLParameterMonitor monitor = new URLParameterMonitor();
// Example usage:
monitor.addParameter("name", "John Doe");
monitor.addParameter("age", "30");
System.out.println("Name: " + monitor.getParameter("name")); // Output: John Doe
System.out.println("Age: " + monitor.getParameter("age")); // Output: 30
System.out.println("City: " + monitor.getParameter("city")); // Output: null
monitor.removeParameter("age");
System.out.println("Age after removal: " + monitor.getParameter("age")); // Output: null
Map<String, String> params = monitor.getCurrentParams();
System.out.println("Current parameters: " + params);
}
}
Add your comment