Skip to content

Custom Menu Input Scanner #5

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.academiadecodigo.bootcamp</groupId>
<artifactId>prompt-view</artifactId>
<version>0.2.1-SNAPSHOT</version>
<version>0.3.1-SNAPSHOT</version>

<dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.academiadecodigo.bootcamp.scanners.menu;

import org.academiadecodigo.bootcamp.scanners.integer.IntegerSetInputScanner;
import java.util.Map;
import java.util.TreeMap;

public class CustomMenuInputScanner extends IntegerSetInputScanner {
private Map<Integer,String> optionMap;

public CustomMenuInputScanner(Map<Integer,String> optionMap) {
super(optionMap.keySet());
this.optionMap = new TreeMap<>(optionMap);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be fully customizable, I think it would be nice to let the user also decide the order by which the menuOptions are shown. By using a TreeMap you are forcing the options to appear by ascending order. If you use a LinkedHashMap for instance, you could let the presentation order to be decided when the optionMap is actually created, and assure that the order is maintained.

super.setMessage(buildMenu(getMessage()));
}

@Override
public void setMessage(String message) {
super.setMessage(buildMenu(message));
}

private String buildMenu(String message) {

StringBuilder menuBuilder = new StringBuilder("\n");
menuBuilder.append(message);
menuBuilder.append("\n");

for(Integer i: optionMap.keySet()) {
menuBuilder.append("\n");
menuBuilder.append(i).append(" - ").append(optionMap.get(i));
}

menuBuilder.append("\n> ");
return menuBuilder.toString();

}
}
23 changes: 23 additions & 0 deletions src/test/java/examples/menu/CustomMenuInputScannerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package examples.menu;

import org.academiadecodigo.bootcamp.Prompt;
import org.academiadecodigo.bootcamp.scanners.menu.CustomMenuInputScanner;

import java.util.HashMap;
import java.util.Map;

public class CustomMenuInputScannerTest {
public static void main(String[] args) {
Prompt prompt = new Prompt(System.in,System.out);

Map<Integer,String> options = new HashMap<>();
options.put(5,"Bahia");
options.put(30,"João");
options.put(2,"Tiago");
Comment on lines +13 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Following the previous comment, if you wanted to assure that the menu would show the options with ascending order, you could use a TreeMap here. If you wanted to keep the order you're adding the options, you could use a LinkedHashMap. Either way would work with this implementation.


CustomMenuInputScanner scanner = new CustomMenuInputScanner(options);
scanner.setMessage("Choose someone:");

System.out.println(prompt.getUserInput(scanner));
}
}