|
1 | 1 | package com.codefortomorrow.beginner.chapter9.solutions;
|
2 | 2 |
|
| 3 | +/* |
| 4 | + * Create a program called TelevisionStats |
| 5 | + * which takes user input for a show |
| 6 | + * and type of statistic and displays the |
| 7 | + * statistic asked for. The program should |
| 8 | + * end whenever the user enters "quit". |
| 9 | + * |
| 10 | + * Use separate 1D arrays for the type of stats, |
| 11 | + * shows, seasons, episodes, wins, nominations, |
| 12 | + * rating, start year, and end year. |
| 13 | + * |
| 14 | + * The data you need can be found here: |
| 15 | + * |
| 16 | + * |
| 17 | + * Sample output: |
| 18 | + */ |
| 19 | + |
3 | 20 | import java.util.Scanner;
|
4 | 21 |
|
5 |
| -public class TelevisionStats |
6 |
| -{ |
| 22 | +public class TelevisionStats { |
| 23 | + public static void main (String[]args) { |
| 24 | + // types of statistics |
| 25 | + String[] stats = {"seasons", "episodes", "wins", "nominations", "rating", "start year", "end year"}; |
7 | 26 |
|
8 |
| - public static void main (String[]args) |
9 |
| - { |
| 27 | + // show statistics |
| 28 | + String[] shows = { |
| 29 | + "Game of Thrones", |
| 30 | + "Breaking Bad", |
| 31 | + "The Walking Dead", |
| 32 | + "Supernatural", |
| 33 | + "FRIENDS", |
| 34 | + "Doctor Who", |
| 35 | + "Black Mirror", |
| 36 | + "The Simpsons", |
| 37 | + "The Big Bang Theory", |
| 38 | + "How I Met Your Mother" |
| 39 | + }; |
10 | 40 |
|
11 |
| - Scanner reader = new Scanner (System.in); |
12 |
| - |
13 |
| - String[] title = {"Game of Thrones", "Breaking Bad", "The Walking Dead", "Supernatural", "FRIENDS", "Doctor Who", "Black Mirror", "The Simpsons", "The Big Bang Theory", "How I Met Your Mother"}; |
14 | 41 | int[] seasons = {8, 5, 10, 15, 10, 12, 5, 31, 12, 9};
|
15 | 42 | int[] episodes = {73, 62, 144, 319, 236, 156, 22, 676, 281, 208};
|
16 | 43 | int[] wins = {313, 138, 69, 22, 71, 113, 23, 173, 67, 25};
|
17 | 44 | int[] nominations = {505, 217, 195, 85, 211, 195, 75, 309, 236, 90};
|
18 | 45 | double[] rating = {8.8, 9.0, 8.1, 8.5, 8.5, 7.7, 7.9, 7.2, 7.9, 8.2};
|
19 | 46 | int[] start = {2011, 2008, 2010, 2005, 1994, 2005, 2011, 1989, 2007, 2005};
|
20 | 47 | int[] end = {2019, 2013, 2020, 2020, 2004, 2020, 2020, 2020, 2019, 2014};
|
21 |
| - |
22 |
| - System.out.println("The following shows have been stored in the array:\n"); |
23 |
| - System.out.println("[Game of Thrones, Breaking Bad, The Walking Dead, Supernatural, FRIENDS, Doctor Who, Black Mirror, The Simpsons, The Big Bang Theory, How I Met Your Mother]"); |
24 |
| - System.out.println("The following stats are available for selection:\n"); |
25 |
| - System.out.println("[Seasons, Episodes, Wins, Nominations, Rating, Start Year, End Year]\n"); |
26 |
| - System.out.println("Type \"quit\" to end the program.\n"); |
27 |
| - |
28 |
| - String show = ""; |
29 |
| - String stat; |
30 |
| - int index = 0; |
31 |
| - while (!show.equalsIgnoreCase("quit")) |
32 |
| - { |
33 |
| - System.out.print("Which show’s stats would you like to research? "); |
34 |
| - show = reader.nextLine(); |
35 |
| - if (show.equalsIgnoreCase("quit")) |
36 |
| - { |
37 |
| - break; |
38 |
| - } |
39 |
| - for (int x = 0; x < title.length; x++) |
40 |
| - { |
41 |
| - if (title[x].equalsIgnoreCase(show)) |
42 |
| - { |
43 |
| - index = x; |
44 |
| - } |
45 |
| - } |
46 |
| - System.out.print("Which stat would you like? "); |
47 |
| - stat = reader.nextLine(); |
48 |
| - if (stat.equalsIgnoreCase("seasons")) |
49 |
| - { |
50 |
| - System.out.println(show + " has aired " + seasons[index] + " seasons."); |
51 |
| - } |
52 |
| - if (stat.equalsIgnoreCase("episodes")) |
53 |
| - { |
54 |
| - System.out.println(show + " has aired " + episodes[index] + " episodes."); |
55 |
| - } |
56 |
| - if (stat.equalsIgnoreCase("wins")) |
57 |
| - { |
58 |
| - System.out.println(show + " recieved " + wins[index] + " wins."); |
59 |
| - } |
60 |
| - if (stat.equalsIgnoreCase("nominations")) |
61 |
| - { |
62 |
| - System.out.println(show + " recieved " + nominations[index] + " nominations."); |
| 48 | + |
| 49 | + // Print out user instructions |
| 50 | + System.out.println("The following shows have been stored in our database:"); |
| 51 | + |
| 52 | + // print shows in the shows array |
| 53 | + System.out.print("["); |
| 54 | + for (int i = 0; i < shows.length; i++) { |
| 55 | + System.out.print(shows[i]); |
| 56 | + if (i < shows.length - 1) { |
| 57 | + System.out.print(", "); |
| 58 | + } else { |
| 59 | + System.out.println("]"); |
63 | 60 | }
|
64 |
| - if (stat.equalsIgnoreCase("rating")) |
65 |
| - { |
66 |
| - System.out.println(show + " has a rating of " + rating[index]); |
| 61 | + } |
| 62 | + |
| 63 | + System.out.println(); |
| 64 | + |
| 65 | + // print stat types |
| 66 | + System.out.println("The following stats are available for selection:"); |
| 67 | + System.out.print("["); |
| 68 | + for (int i = 0; i < stats.length; i++) { |
| 69 | + System.out.print(stats[i]); |
| 70 | + if (i < stats.length - 1) { |
| 71 | + System.out.print(", "); |
| 72 | + } else { |
| 73 | + System.out.println("]"); |
67 | 74 | }
|
68 |
| - if (stat.equalsIgnoreCase("start year")) |
69 |
| - { |
70 |
| - System.out.println(show + " started airing in " + start[index] + " seasons."); |
| 75 | + } |
| 76 | + |
| 77 | + System.out.println("\nType \"quit\" to end the program.\n"); |
| 78 | + |
| 79 | + String show; // name of the show the user is researching |
| 80 | + String stat; // type of statistic the user wants |
| 81 | + int index = -1; // index of the show |
| 82 | + |
| 83 | + Scanner reader = new Scanner(System.in); |
| 84 | + |
| 85 | + boolean quit = false; // whether the user wants to quit |
| 86 | + do { |
| 87 | + // keep asking the user for a show until |
| 88 | + // they give a valid show |
| 89 | + while (index == -1) { |
| 90 | + System.out.print("Which show's stats would you like to research? "); |
| 91 | + show = reader.nextLine().toLowerCase(); // case insensitive |
| 92 | + |
| 93 | + // if user quits, exit the program |
| 94 | + if (show.equals("quit")) { |
| 95 | + System.out.println("Quitting..."); |
| 96 | + quit = true; |
| 97 | + break; |
| 98 | + } else { // user enters a show |
| 99 | + // find the index of that show |
| 100 | + for (int i = 0; i < shows.length; i++) { |
| 101 | + if (show.equalsIgnoreCase(shows[i])) { // case insensitive |
| 102 | + index = i; // match found |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // if the show given is invalid |
| 107 | + if (index == -1) { |
| 108 | + // print error message |
| 109 | + System.out.println("Sorry, we don't have data for that show. Please try again."); |
| 110 | + } |
| 111 | + } |
71 | 112 | }
|
72 |
| - if (stat.equalsIgnoreCase("end year")) |
73 |
| - { |
74 |
| - System.out.println(show + " last aired in " + end[index] + " seasons."); |
| 113 | + |
| 114 | + if (!quit) { |
| 115 | + boolean validStatistic = false; // whether the user has entered a valid stat type |
| 116 | + |
| 117 | + // keep asking user for a stat type until they give a valid stat type or quit the program |
| 118 | + while (!validStatistic) { |
| 119 | + System.out.print("Which stat would you like? "); |
| 120 | + stat = reader.nextLine().toLowerCase(); // case insensitive |
| 121 | + validStatistic = true; // assume stat type is valid |
| 122 | + |
| 123 | + // bring up the stat the user wants |
| 124 | + switch (stat) { |
| 125 | + case "seasons": |
| 126 | + System.out.println(shows[index] + " has aired " + seasons[index] + " seasons."); |
| 127 | + break; |
| 128 | + case "episodes": |
| 129 | + System.out.println(shows[index] + " has aired " + episodes[index] + " episodes."); |
| 130 | + break; |
| 131 | + case "wins": |
| 132 | + System.out.println(shows[index] + " received " + wins[index] + " wins."); |
| 133 | + break; |
| 134 | + case "nominations": |
| 135 | + System.out.println(shows[index] + " received " + nominations[index] + " nominations."); |
| 136 | + break; |
| 137 | + case "rating": |
| 138 | + System.out.println(shows[index] + " has a rating of " + rating[index]); |
| 139 | + break; |
| 140 | + case "start year": |
| 141 | + System.out.println(shows[index] + " started airing in " + start[index] + " seasons."); |
| 142 | + break; |
| 143 | + case "end year": |
| 144 | + System.out.println(shows[index] + " last aired in " + end[index] + " seasons."); |
| 145 | + break; |
| 146 | + case "quit": |
| 147 | + System.out.println("Quitting..."); |
| 148 | + quit = true; |
| 149 | + break; |
| 150 | + default: // invalid stat type |
| 151 | + System.out.println("Sorry, we don't have that stat. Please try again."); |
| 152 | + validStatistic = false; |
| 153 | + } |
| 154 | + } |
75 | 155 | }
|
76 |
| - |
77 |
| - System.out.println(); |
| 156 | + |
| 157 | + // reset index (next iteration will ask user for show again) |
| 158 | + index = -1; |
78 | 159 | System.out.println();
|
79 |
| - } |
| 160 | + } while (!quit); |
| 161 | + |
| 162 | + System.out.println("You have successfully exited the program."); |
80 | 163 |
|
| 164 | + reader.close(); |
81 | 165 | }
|
82 | 166 | }
|
0 commit comments