title: Dynamic switch case with Methods as Map Value - Java
body: <p>so my little project has a Scanner and two important methods:</p>
  <ol>
  <li>That shows the commands i can type in my scanner (runs other methods)</li>
  <li>A switch-case with the commands</li>
  </ol>
  <p>And the following code:</p>
  <pre><code>static void showCommands(){
      System.out.println(&quot;You can enter the following commands: &quot;
              + &quot;\nCommands: Lists your commands. &quot;
              + &quot;\nInventory: Opens up your inventory&quot;
              + &quot;\nEquipment: Let's you equip your items&quot;
              + &quot;\nSpawnItem: Spawns a random item in your inventory&quot;
              + &quot;\nTravel: Travels your character&quot;
              + &quot;\nLocate: Locates your character&quot;
              + &quot;\nClose: Exits the game&quot;);
      
      
  }
  
  static void useCommands() {
      String input = &quot;&quot;;
      Scanner sc = new Scanner(System.in);
      while (!input.equals(&quot;Close&quot;)) {
          input = sc.next();
          switch (input) {
          case &quot;commands&quot;:
              showCommands();
              break;
          case &quot;combatlevel&quot;:
              CombatLevel.showCombatLevel();
              break;
          case &quot;totatlevel&quot;:
              TotalLevel.showTotalLevel();
              break;
          case &quot;inventory&quot;:
              Inventory.showInventory();
              break;
          case &quot;equipment&quot;:
              Equipment.showEquipment();
              break;
          case &quot;equip&quot;:
              break;
          case &quot;spawnitem&quot;:
              Item.spawnItem();
              break;
          case &quot;travel&quot;:
              Map.travel();
              break;
          case &quot;increaselevel&quot;:
              Player.getInstance().combatLevel.increaseCombatLevel();
              break;
          case &quot;locate&quot;:
              Map.locate();
              break;
          case &quot;close&quot;:
              sc.close();
              System.exit(0);
              break;
          }
      }
  }
  </code></pre>
  <p>The problem is: the architecture is too static, since I always need to update both methods accordingly for each new command and itself it just feels not efficient.</p>
  <p>So I wanted to ask if it is possible to maybe use a Map with a key, the String of the user input on my scanner, and the start the method dynamically as the value of the map.</p>
  <p>Like:</p>
  <p>HashMap&lt;String, Method&gt; test = new HashMap&lt;&gt;();
  test.put(&quot;test&quot;, test());</p>
  <p>I tried to do that but it didnt work out, since my test() method had to return another method which did not make sense to me.
  Does anyone have any suggestions on how to fix that problem?</p>
  <p>Cheers</p>