/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code6mar18; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author jcmtiernan */ public class Code6Mar18 { /** * @param args the command line arguments */ public static void main(String[] args) { // methods // try-catch exception handling // control statements // file input //File pref = new File("src/code6mar18/mediapref.txt"); File pref = new File("mediapref.txt"); //Scanner input = new Scanner(System.in); Scanner input; Logger.getGlobal().setLevel(Level.INFO);// Use Level.OFF to turn off ; .INFO to turn on int choice; String mobOrStat; boolean moveForward = false; char iOrO = ' '; String finalMedia; boolean fileFound = true; try { input = new Scanner(pref); } catch (FileNotFoundException fnfe) { input = new Scanner(System.in); System.out.println("File not found - please correct file input"); fileFound = false; } System.out.println("Hi! What kind of entertainment media do you prefer? "); while (fileFound && input.hasNext()) { do { System.out.println("\n\nSelecting mobile or stationary"); System.out.println("M - mobile or S - stationary "); mobOrStat = input.next(); // reads a "word" input.nextLine(); // reads end of line marker out of input buffer Logger.getGlobal().info("File value *"+mobOrStat+"*"); // Validate the user's input to insure that only one of the // following values are entered: // M, m, mobile or // S, s, stationary if (mobOrStat.equalsIgnoreCase("m") || mobOrStat.equalsIgnoreCase("mobile") || mobOrStat.equalsIgnoreCase("s") || mobOrStat.equalsIgnoreCase("stationary") ) { moveForward = true; } else { System.out.println("Your input was not recognized."); System.out.println("Please enter a valid input."); } } while (!moveForward); // !moveForward is the same as moveForward == false // alternate selection mobOrStat = mobOrStat.toUpperCase(); switch (mobOrStat) { case "S": case "STATIONARY": mobOrStat = "STATIONARY"; break; case "M": case "MOBILE": mobOrStat = "MOBILE"; break; } System.out.println("You entered "+mobOrStat+" as your first entertainment preference\n"); String inOrOut = ""; if (mobOrStat.equalsIgnoreCase("STATIONARY")) { // query user for home (TV, desktop computer) or not (movie theater, live theatre, RPG) System.out.println("Is the entertainment media in your home or outside your home?"); System.out.println("Respond with I (inside) or O (outside): "); inOrOut = input.nextLine(); Logger.getGlobal().info("User entered *"+inOrOut+"*"); iOrO = inOrOut.charAt(0); Logger.getGlobal().info("First letter of user input is *"+iOrO+"*"); if ((iOrO == 'I') || (iOrO == 'i')) { inOrOut = "INSIDE"; } else if ((iOrO == 'O') || (iOrO == 'o')) { inOrOut = "OUTSIDE"; } else // user entered invalid input { // could have set up a do-while loop like above but didn't System.out.println("You entered an invalid input."); System.out.println("The system assumes you are a mushroom and your media in INSIDE."); inOrOut = "INSIDE"; } // Given INSIDE or OUTSIDE // find user's preferred type of media // INSIDE choices are: Desktop computer, TV, Gaming system, VR // OUTSIDE choices are: Movie theater, Theatre, RPG, ComicCon // Making a method to // show choices if (inOrOut.equals("INSIDE")) { finalMedia = chooseMedia("Desktop computer", "TV", "Gaming system", "VR", input); } else { finalMedia = chooseMedia("Movie theater", "Theatre", "ComicCon", "RPG", input); } //System.out.println("Final choice is "+finalMedia); /* System.out.println("Please select your favorite media from this list:"); System.out.println("1. "+firstChoice); // firstChoice is string System.out.println("2. "+secondChoice); System.out.println("3. "+thirdChoice); System.out.println("4. "+fourthChoice); System.out.println("Enter the number of your choice: "); intChoice = input.nextInt(); String finalChoice; switch (intChoice) { case 1: finalChoice = firstChoice; break; case 2: finalChoice = secondChoice; break; case 3: finalChoice = thirdChoice; break; case 4: finalChoice = fourthChoice; break; } */ // let user select // return selected choice } else // must be MOBILE { // query user for phone, laptop, VR system, ...) finalMedia = chooseMedia("Laptop computer", "Tablet/iPad", "Phone", "Walkman", input); } System.out.println("Final choice is "+finalMedia); } // end of while (moreInput) } public static String chooseMedia(String firstChoice, String secondChoice, String thirdChoice, String fourthChoice, Scanner input) { System.out.println("Please select your favorite media from this list:"); System.out.println("1. "+firstChoice); // firstChoice is string System.out.println("2. "+secondChoice); System.out.println( thirdChoice.equals("")?"":"3. "+thirdChoice ); System.out.println( fourthChoice.equals("")?"":"4. "+fourthChoice ); //System.out.println(); System.out.println("Enter the number of your choice: "); int intChoice; try { intChoice = input.nextInt(); } catch (InputMismatchException ime) { intChoice = 1; System.out.println("Your input was invalid. Default 1 is chosen."); } String finalChoice; switch (intChoice) { case 1: finalChoice = firstChoice; break; case 2: finalChoice = secondChoice; break; case 3: finalChoice = thirdChoice; break; case 4: finalChoice = fourthChoice; break; default: finalChoice = "kazoo"; break; } return finalChoice; } }