/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package lab2fantasyscifi; import java.util.InputMismatchException; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author jcmtiernan */ public class Lab2FantasySciFi { /** * Shows/Books/Movies Fantasy Sci Fi Choices. Firefly Star Trek The Original Series The Animated Series The Next Generation Deep Space Nine Voyager Enterprise Discovery Back To The Future Dr. Who Star Wars Star Wars: A New Hope (Episode IV) Empire Strikes Back (Episode V) Return of the Jedi (Episode VI) Episode I – The Phantom Menace Episode II – Attack of the Clones Episode III – Revenge of the Sith Episode VII – The Force Awakens Episode VIII – The Last Jedi Rogue One Harry Potter The Sorcerer’s Stone (also called The Philosopher’s Stone) The Chamber of Secrets The Prisoner of Azkaban The Goblet of Fire The Order of the Phoenix The Half-Blood Prince The Deathly Hallows The Princess Bride The Lord Of The Rings The Hobbit The Lord Of The Rings The Fellowship of the Ring The Two Towers * Design a pseudocode algorithm to solve this problem: * You want to find out which book/movie/show of the choices above * would be the best liked by the current user. You will start * by asking them if they prefer fantasy genre or the genre of * science fiction. In the list above there are eight top-level * choices of specific books/movies/shows. The first three are * science fiction, the last three are fantasy, and the two in * the middle could fall in either category or both. After you * get the user’s response about fantasy or science fiction, * then ask them which top-level book/movie/show they prefer * from that genre. If the top-level choice has subchoices, * then ask the user to specify with subchoice they prefer. * Print their preference of genre, the top-level choice they * made, and if appropriate, the sub-level. * * Greet user and intro program * Ask user to give preference for fantasy or sci-fi genre * Get user input for genre preference * If user chooses fantasy * Then have them select their favorite from the following choices: * Harry Potter, The Princess Bride, The Lord Of The Rings, Dr. Who, Star Wars * Get user input for choice * If HP, LOR, or SW is selected * Then have them select a favorite subchoice * Get user input for subchoice, if any * Save preference, choice and subchoice * Else // if user has chosen sci-fi * Have them select their favorite from : * Firefly, Star Trek, Back To The Future, Dr. Who, Star Wars * Get user input for choice * If ST or SW is selected * Then have them select a favorite subchoice * Get user input for subchoice, if any * Save preference, choice and subchoice * Print user genre preference, choice and subchoice in table * * * @param args the command line arguments */ public static void main(String[] args) { String genre = ""; boolean genreChoice; // true is FANTASY; false is SCIENCE FICTION int choiceBMT = 0; //Books Moves TV String bookMovieTV = ""; int choiceSub = 0; String subChoice = ""; Scanner kybd = new Scanner(System.in); Logger.getGlobal().setLevel(Level.OFF);// Use Level.OFF to turn off ; .INFO to turn on // * Greet user and intro program System.out.println("Welcome to the Favorites Selection program."); // * Ask user to give preference for fantasy or sci-fi genre System.out.println("Which is your favorite (or your preference): "); System.out.println("Fantasy (F) or Science Fiction (S) ?"); // * Get user input for genre preference genre = kybd.nextLine(); genre = input4ChoiceValidate(genre, "F", "Fantasy", "S", "Science Fiction", kybd); genre = formal2Choice(genre, "F", "Fantasy", "S", "Science Fiction", "genre"); // 1. Use an if-else to handle the fantasy/science fiction choice and // save the user’s choice in a boolean variable if (genre.equals("FANTASY")) { genreChoice = true; } else { genreChoice = false; } // * If user chooses fantasy if (genreChoice) { // * Then have them select their favorite from the following choices: // * Harry Potter, The Princess Bride, The Lord Of The Rings, Dr. Who, Star Wars // Within that if-else, use nested if-else’s to ask about the // top-level choices and save the user’s choice System.out.println("Please select your favorite (or your preference) from these choices:"); System.out.println("1. Harry Potter \n2. The Princess Bride \n3. The Lord Of The Rings " + "\n4. Dr. Who \n5. Star Wars "); choiceBMT = chooseNumList(kybd, 2, 5, 1); bookMovieTV = numberToName12(choiceBMT, 5, "Harry Potter","The Princess Bride","The Lord Of The Rings" ,"Dr. Who","Star Wars", "","","","","","","" ); // * If HP, LOR, or SW is selected // * Then have them select a favorite subchoice // 3. If the top-level choice has subchoices, // give the user a menu of choices (i.e. a numbered // list in the output window – not a separate “menu”) // and take their choice and use it to // select the final subchoice with a switch statement // * Get user input for subchoice, if any choiceSub = 0; if (choiceBMT == 1) // Harry Potter { /* The Sorcerer’s Stone (also called The Philosopher’s Stone) The Chamber of Secrets The Prisoner of Azkaban The Goblet of Fire The Order of the Phoenix The Half-Blood Prince The Deathly Hallows */ System.out.println("Your choice was Harry Potter, " + "so please choose your favorite of these:"); System.out.println("\n1. The Sorcerer’s Stone (also called The Philosopher’s Stone)"+ "\n2. The Chamber of Secrets"+"\n3. The Prisoner of Azkaban"+ "\n4. The Goblet of Fire"+"\n5. The Order of the Phoenix"+ "\n6. The Half-Blood Prince"+"\n7. The Deathly Hallows" ); choiceSub = chooseNumList(kybd, 2, 7, 1); subChoice = numberToName12(choiceSub, 7, "The Sorcerer’s Stone (also called The Philosopher’s Stone)", "The Chamber of Secrets","The Prisoner of Azkaban", "The Goblet of Fire","The Order of the Phoenix", "The Half-Blood Prince","The Deathly Hallows", "","","","","" ); } else if (choiceBMT == 3) // The Lord Of The Rings { /* The Hobbit The Lord Of The Rings The Fellowship of the Ring The Two Towers */ System.out.println("Your choice was The Lord Of The Rings, " + "so please choose your favorite of these:"); System.out.println("\n1. The Hobbit"+"\n2. The Lord Of The Rings"+ "\n3. The Fellowship of the Ring"+"\n4. The Two Towers"); choiceSub = chooseNumList(kybd, 1, 4, 1); subChoice = numberToName12(choiceSub, 4, "The Hobbit","The Lord Of The Rings", "The Fellowship of the Ring","The Two Towers", "","","","","","","","" ); } else if (choiceBMT == 5) // Star Wars { /* Star Wars: A New Hope (Episode IV) Empire Strikes Back (Episode V) Return of the Jedi (Episode VI) Episode I – The Phantom Menace Episode II – Attack of the Clones Episode III – Revenge of the Sith Episode VII – The Force Awakens Episode VIII – The Last Jedi Rogue One */ System.out.println("Your choice was Star Wars, " + "so please choose your favorite of these:"); System.out.println("\n1. Star Wars: A New Hope (Episode IV)"+"\n2. Empire Strikes Back (Episode V)" +"\n3. Return of the Jedi (Episode VI)"+"\n4. Episode I – The Phantom Menace" +"\n5. Episode II – Attack of the Clones"+"\n6. Episode III – Revenge of the Sith" +"\n7. Episode VII – The Force Awakens"+"\n8. Episode VIII – The Last Jedi" +"\n9. Rogue One"); choiceSub = chooseNumList(kybd, 3, 9, 1); subChoice = numberToName12(choiceSub, 9, "Star Wars: A New Hope (Episode IV)","Empire Strikes Back (Episode V)" ,"Return of the Jedi (Episode VI)","Episode I – The Phantom Menace" ,"Episode II – Attack of the Clones","Episode III – Revenge of the Sith" ,"Episode VII – The Force Awakens","Episode VIII – The Last Jedi" ,"Rogue One", "","","" ); } // * Save preference, choice and subchoice } else // user choice is SCIENCE FICTION { // * Then have them select their favorite from the following choices: // * Firefly, Star Trek, Back To The Future, Dr. Who, Star Wars // Within that if-else, use nested if-else’s to ask about the // top-level choices and save the user’s choice System.out.println("Please select your favorite (or your preference) from these choices:"); System.out.println("1. Firefly \n2. Star Trek \n3. Back To The Future " + "\n4. Dr. Who \n5. Star Wars "); choiceBMT = chooseNumList(kybd, 1, 5, 1); bookMovieTV = numberToName12(choiceBMT, 5, "Firefly","Star Trek","Back To The Future " ,"Dr. Who","Star Wars", "","","","","","","" ); // * If ST or SW is selected // * Then have them select a favorite subchoice // 3. If the top-level choice has subchoices, // give the user a menu of choices (i.e. a numbered // list in the output window – not a separate “menu”) // and take their choice and use it to // select the final subchoice with a switch statement // * Get user input for subchoice, if any choiceSub = 0; if (choiceBMT == 2) // Star Trek { /* The Original Series The Animated Series The Next Generation Deep Space Nine Voyager Enterprise Discovery */ System.out.println("Your choice was Star Trek, " + "so please choose your favorite of these:"); System.out.println("\n1. The Original Series"+ "\n2. The Animated Series"+"\n3. The Next Generation"+ "\n4. Deep Space Nine"+"\n5. Voyager"+ "\n6. Enterprise"+"\n7. Discovery" ); choiceSub = chooseNumList(kybd, 2, 7, 1); subChoice = numberToName12(choiceSub, 7, "The Original Series","The Animated Series", "The Next Generation","Deep Space Nine","Voyager", "Enterprise","Discovery", "","","","","" ); } else if (choiceBMT == 5) // Star Wars { /* Star Wars: A New Hope (Episode IV) Empire Strikes Back (Episode V) Return of the Jedi (Episode VI) Episode I – The Phantom Menace Episode II – Attack of the Clones Episode III – Revenge of the Sith Episode VII – The Force Awakens Episode VIII – The Last Jedi Rogue One */ System.out.println("Your choice was Star Wars, " + "so please choose your favorite of these:"); System.out.println("\n1. Star Wars: A New Hope (Episode IV)"+"\n2. Empire Strikes Back (Episode V)" +"\n3. Return of the Jedi (Episode VI)"+"\n4. Episode I – The Phantom Menace" +"\n5. Episode II – Attack of the Clones"+"\n6. Episode III – Revenge of the Sith" +"\n7. Episode VII – The Force Awakens"+"\n8. Episode VIII – The Last Jedi" +"\n9. Rogue One"); choiceSub = chooseNumList(kybd, 3, 9, 1); subChoice = numberToName12(choiceSub, 9, "Star Wars: A New Hope (Episode IV)","Empire Strikes Back (Episode V)" ,"Return of the Jedi (Episode VI)","Episode I – The Phantom Menace" ,"Episode II – Attack of the Clones","Episode III – Revenge of the Sith" ,"Episode VII – The Force Awakens","Episode VIII – The Last Jedi" ,"Rogue One", "","","" ); } } // * Print user genre preference, choice and subchoice in table System.out.println("\nYour book/movie/TV preference is "+bookMovieTV+ (subChoice.equals("")? "" : ": "+subChoice )+ " in the genre of "+genre); System.out.println("\nYour book/movie/TV preference:"); System.out.printf("%15s%30s\n","Genre",genre); System.out.printf("%15s%30s\n","Choice",bookMovieTV); System.out.printf("%15s%30s\n", (subChoice.equals("")?"":"Subchoice"), (subChoice.equals("")?"":subChoice)); } // Validate the user's input to insure that only one of the // allowed values are entered public static String input4ChoiceValidate(String validateMe, String firstChoice, String secondChoice, String thirdChoice, String fourthChoice, Scanner input) { boolean moveForward = false; do { if (!validateMe.equals("") && (validateMe.equalsIgnoreCase(firstChoice) || validateMe.equalsIgnoreCase(secondChoice) || validateMe.equalsIgnoreCase(thirdChoice) || validateMe.equalsIgnoreCase(fourthChoice) )) { moveForward = true; } else { System.out.print("Your input was not recognized."); System.out.println("Please enter a valid input.\n"); System.out.println("Please enter your favorite (or your preference): "); validateMe = input.nextLine(); // reads line } Logger.getGlobal().info("Input value *"+validateMe+"*"); } while (!moveForward); // !moveForward is the same as moveForward == false return validateMe; } public static String formal2Choice(String formalMe, String abbrev1Choice, final String name1Choice, String abbrev2Choice, final String name2Choice, String description) { formalMe = formalMe.toUpperCase(); if (formalMe.equalsIgnoreCase(abbrev1Choice) || formalMe.equalsIgnoreCase(name1Choice)) { formalMe = name1Choice.toUpperCase(); } else if (formalMe.equalsIgnoreCase(abbrev2Choice) || formalMe.equalsIgnoreCase(name2Choice)) { formalMe = name2Choice.toUpperCase(); } System.out.println("You entered "+formalMe+" as your "+description+" preference\n"); return formalMe; } public static int chooseNumList(Scanner kybd, int defaultValue, int max, int min) { int choice; System.out.print("Choice: "); try // * Get user input for choice { choice = kybd.nextInt(); } catch (InputMismatchException ime) { choice = defaultValue; System.out.println("Your input was invalid. Default "+defaultValue+" is chosen."); } if ((choice < min) || (choice > max)) { choice = defaultValue; System.out.println("Your input was invalid. Default "+defaultValue+" is chosen."); } System.out.println(""); return choice; } public static String numberToName12(int choice, int max, String name1, String name2,String name3, String name4,String name5,String name6,String name7,String name8,String name9, String name10,String name11,String name12) { Logger.getGlobal().info("name1 is *"+name1+"* in numberToName12 and choice is "+choice); String name = ""; switch(choice) { case 1: name = name1; break; case 2: name = name2; break; case 3: name = name3; break; case 4: name = name4; break; case 5: name = name5; break; case 6: name = name6; break; case 7: name = name7; break; case 8: name = name8; break; case 9: name = name9; break; case 10: name = name10; break; case 11: name = name11; break; case 12: name = name12; break; } Logger.getGlobal().info("Name is *"+name+"* in numberToName12"); return name; } }