-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: master
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,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); | ||
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(); | ||
|
||
} | ||
} |
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
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. Following the previous comment, if you wanted to assure that the menu would show the options with ascending order, you could use a |
||
|
||
CustomMenuInputScanner scanner = new CustomMenuInputScanner(options); | ||
scanner.setMessage("Choose someone:"); | ||
|
||
System.out.println(prompt.getUserInput(scanner)); | ||
} | ||
} |
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.
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 aLinkedHashMap
for instance, you could let the presentation order to be decided when the optionMap is actually created, and assure that the order is maintained.