/* * @topic T02755 Assignment a8 -- Airline Flight Schedule demo Verion 2 * @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_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_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("______________________________________________"); return choice; } } public String getString( String prompt ) { System.out.print( prompt ); String temp = sc.next(); sc.nextLine(); return temp; } 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(); } }