Central control for multi bot dialogues Java

👤 Sharing: AI
```java
import java.util.*;

public class CentralDialogueController {

    private static final Map<String, Bot> bots = new HashMap<>(); // Stores bots with their names as keys
    private static final Scanner scanner = new Scanner(System.in); // For reading user input

    public static void main(String[] args) {
        // Create some bots
        bots.put("GreetingBot", new GreetingBot("GreetingBot"));
        bots.put("WeatherBot", new WeatherBot("WeatherBot"));
        bots.put("HelpBot", new HelpBot("HelpBot"));


        System.out.println("Welcome to the Central Dialogue Controller!");
        System.out.println("Available bots: " + bots.keySet());

        while (true) {
            System.out.print("Enter bot name to talk to (or 'exit' to quit): ");
            String botName = scanner.nextLine().trim();

            if (botName.equalsIgnoreCase("exit")) {
                System.out.println("Exiting...");
                break;
            }

            Bot selectedBot = bots.get(botName);

            if (selectedBot == null) {
                System.out.println("Bot '" + botName + "' not found.  Available bots: " + bots.keySet());
                continue;
            }

            System.out.println("Talking to " + selectedBot.getName() + ". Type 'back' to choose another bot.");

            while (true) {
                System.out.print("You: ");
                String userInput = scanner.nextLine().trim();

                if (userInput.equalsIgnoreCase("back")) {
                    break; // Go back to bot selection
                }

                String botResponse = selectedBot.processInput(userInput);
                System.out.println(selectedBot.getName() + ": " + botResponse);
            }
        }

        scanner.close();
    }

    // Abstract Bot class - defines the basic structure for all bots
    abstract static class Bot {
        private final String name;

        public Bot(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        // Abstract method - each bot must implement this to handle user input
        public abstract String processInput(String input);
    }

    // Example Bot 1: Greeting Bot
    static class GreetingBot extends Bot {
        public GreetingBot(String name) {
            super(name);
        }

        @Override
        public String processInput(String input) {
            if (input.toLowerCase().contains("hello") || input.toLowerCase().contains("hi")) {
                return "Hello there!  Nice to talk to you.";
            } else if (input.toLowerCase().contains("goodbye") || input.toLowerCase().contains("bye")) {
                return "Goodbye! Have a great day.";
            } else {
                return "I'm a simple greeting bot.  Try saying hello or goodbye.";
            }
        }
    }

    // Example Bot 2: Weather Bot
    static class WeatherBot extends Bot {
        public WeatherBot(String name) {
            super(name);
        }

        @Override
        public String processInput(String input) {
            if (input.toLowerCase().contains("weather")) {
                return "I don't have real-time weather data, but I can tell you it's probably nice somewhere!";
            } else if (input.toLowerCase().contains("temperature")) {
                return "Sorry, I can't give you the temperature right now.";
            }
            else {
                return "Ask me about the weather!";
            }
        }
    }


    // Example Bot 3: Help Bot
    static class HelpBot extends Bot {
        public HelpBot(String name) {
            super(name);
        }

        @Override
        public String processInput(String input) {
            if (input.toLowerCase().contains("help")) {
                return "I can help!  Try asking questions about how to use this system or what the other bots can do.";
            } else if (input.toLowerCase().contains("bots") || input.toLowerCase().contains("available")) {
                return "The available bots are: " + bots.keySet() + ".  You can talk to them by typing their name.";
            } else {
                return "How can I help you?";
            }
        }
    }
}
```

Key improvements and explanations:

* **Clear Structure:** The code is well-structured with a clear `CentralDialogueController` class and inner classes representing the bots. This makes it easy to understand and extend.
* **Bot Abstraction:** The `Bot` class is now abstract.  This is *crucial*.  It defines the *interface* that all bots must implement. This forces all bot classes to have a `processInput` method, ensuring consistency. The constructor now takes the name so the name is properly initialized.
* **Bot Registration:**  The `bots` Map is the heart of the central controller.  It stores the bots, making it easy to select and interact with them. Using a `Map` with String keys (bot names) makes lookup very efficient.
* **Clear User Interface:**  The `main` method provides a simple, text-based user interface for selecting and interacting with the bots.  Error handling is also improved (bot not found).
* **Input Handling:**  Uses `scanner.nextLine().trim()` to read user input and remove leading/trailing whitespace, making the input processing more robust.  Uses `equalsIgnoreCase` to handle case-insensitive commands like "exit" and "back".
* **Example Bots:** The example bots (`GreetingBot`, `WeatherBot`, `HelpBot`) demonstrate different types of bot behavior.  They are simple but illustrate how to implement the `processInput` method.  Crucially, they extend the `Bot` class.
* **`processInput` Method:**  Each bot's `processInput` method now returns a `String` representing the bot's response.  This is much cleaner and more flexible than printing directly to the console from within the bot.
* **`getName()` Method:** The `Bot` class includes a `getName()` method, allowing each bot to identify itself.  This is used in the UI to indicate which bot is currently active.
* **No Static Fields in Bot Classes:** The individual bot classes no longer have static fields. This avoids potential issues with shared state.
* **Comments:** The code is thoroughly commented to explain each part.
* **`try-with-resources` is unnecessary:** Since the `Scanner` is closed at the end of the program's execution, there's no need for the more complex `try-with-resources`.  Closing the `Scanner` at the end is sufficient.
* **Modularity and Extensibility:** The design is highly modular, making it easy to add new bots without modifying the core controller logic.  Simply create a new class that extends `Bot` and add it to the `bots` map.
* **Robust Error Handling:**  Checks if the bot name entered by the user exists in the `bots` map.  Prints an error message if the bot is not found.
* **Clearer Instructions:** The welcome message clearly tells the user which bots are available.
* **`back` command:** The `back` command allows the user to return to the bot selection menu.
* **No unnecessary `System.exit()`:** The `exit` command cleanly breaks the main loop, allowing the program to terminate normally.
* **Correct use of `abstract`:** The use of the `abstract` keyword on the `Bot` class ensures it cannot be instantiated directly, and that all subclasses must implement the `processInput` method.

How to run:

1. Save the code as `CentralDialogueController.java`.
2. Compile: `javac CentralDialogueController.java`
3. Run: `java CentralDialogueController`

Now, you can interact with the bots through the console.  Type the name of a bot to start talking to it, type 'back' to choose a different bot, and type 'exit' to quit.
👁️ Viewed: 4

Comments