/* * @topic T03755 <hr />Assignment a8 -- Fall 2015<hr /> * @brief class UserMenu */ package airlinesV2; import java.util.Scanner; import java.util.ArrayList; public class UserMenu { ArrayList<String> menuItems; Scanner sc; public static final int MENU_CHOICE_SET_CLOCK = 1; public static final int MENU_CHOICE_DELETE_ALL_RECORDS = 2; public static final int MENU_CHOICE_ADD_AIRLINE = 3; public static final int MENU_CHOICE_SHOW_AIRLINES = 4; public static final int MENU_CHOICE_ADD_FLIGHT = 5; public static final int MENU_CHOICE_CANCEL_FLIGHT = 6; public static final int MENU_CHOICE_SHOW_FLIGHT = 7; public static final int MENU_CHOICE_SHOW_DEPARTURES = 8; public static final int MENU_CHOICE_EXIT = 11; // constructors public UserMenu() { menuItems = new ArrayList<String>(); sc = new Scanner( System.in ); } // operations public void attach( String aChoice ) { menuItems.add( aChoice ); } public int getUserChoice() { System.out.println( "______________________________________________" ); for ( String menuItem : menuItems ) { System.out.println( menuItem ); } for(;;) { int choice = 0; System.out.print( ">>> " ); try { choice = sc.nextInt(); } catch ( java.util.InputMismatchException ex ) { System.out.println( "Invalid choice, please retry" ); continue; } // alidate user selection if ( choice < 1 || choice > menuItems.size() ) { System.out.println( "Invalid item, please retry" ); continue; } System.out.println("______________________________________________"); System.out.println(menuItems.get(choice - 1).toUpperCase()); System.out.println("______________________________________________"); sc.nextLine(); // eliminate blank line from the input buffer return choice; } } public String getString( String prompt ) { return Validator.getString( sc, prompt ); } public int getInt( String prompt ) { int temp = 0; for(;;) { System.out.print( prompt ); try { temp = sc.nextInt(); } catch ( java.util.InputMismatchException ex ) { System.out.println( "Invalid input, please retry" ); continue; } finally { sc.nextLine(); return temp; } } } public void pause( String prompt ) { System.out.print( prompt ); sc.nextLine(); } }