/* * @topic T00890 Using Comparable interface Fall 2015 * @brief Menu class */ package diceroll; import java.util.Scanner; public class Menu { public static Scanner sc = new Scanner(System.in); public static final int CHOICE_UNKNOWN = 0; public static final int CHOICE_ROLL = 1; public static final int CHOICE_ROLL_SERIES = 2; public static final int CHOICE_QUIT = 3; private String strChoices; private int choice; private int choice_range; // constructors public Menu() { strChoices = "Make your choice:\n"; choice = CHOICE_UNKNOWN; choice_range = 0; } // operations public void attach(String aChoice) { strChoices += aChoice; strChoices += "\n"; ++choice_range; } public int getUserChoice() { System.out.print(strChoices); //return Validator.getInt(sc, ">>> "); System.out.print(">>> "); for (;;) { try { choice = sc.nextInt(); if ( choice < 1 || choice > choice_range ) { System.out.print( "Sorry, the input should be [1-" +choice_range+ "], try again: "); continue; } return choice; } catch (java.util.InputMismatchException ex) { sc.nextLine(); //ex.printStackTrace(); System.out.print("Sorry, bad input, try again: "); } finally { //System.out.print("this silly message is always printed! "); } }//forever // TODO -- validate user selection } }//class Menu