1
1
package com .codefortomorrow .beginner .chapter9 .solutions ;
2
2
3
- import java .util .Scanner ;
3
+ /*
4
+ * Create a program called VirusStats
5
+ * which takes user input for a country
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
+ * countries, capitals, population, number of cases,
12
+ * deaths, recoveries, death rates, recovery rates,
13
+ * infection rates, and "risk grade." (Please note that
14
+ * "risk grades" are not based on actual scientific
15
+ * fact and were made up for the purposes of this exercise.)
16
+ *
17
+ * The data you need can be found here:
18
+ * https://docs.google.com/spreadsheets/d/1XZ0GjALsu2mhraGMJoj37jTmy_S2sw51FFMeywqkcnw/edit?usp=sharing
19
+ *
20
+ * To calculate the death rate:
21
+ * Take that country's deaths and divide it by the number of cases.
22
+ *
23
+ * To calculate the recovery rate:
24
+ * Take that country's number of recoveries and divide it
25
+ * by the number of cases.
26
+ *
27
+ * To calculate the infection rate:
28
+ * Take that country's number of cases and divide it
29
+ * by the population.
30
+ *
31
+ * For the rates: Bonus points if you can round them (or format them)
32
+ * to the nearest hundredths place.
33
+ *
34
+ * To determine the "risk grade", where 'A' is most dangerous
35
+ * and 'F' is least dangerous:
36
+ *
37
+ * 'A' if infection rate > 0.25 OR death rate > 25
38
+ * 'B' if infection rate > 0.2 OR death rate > 15
39
+ * 'C' if infection rate > 0.15 OR death rate > 10
40
+ * 'D' if infection rate > 0.1 OR death rate > 5
41
+ * 'F' for all else
42
+ *
43
+ * Sample output: https://youtu.be/rFuGY2wXcKc
44
+ */
4
45
5
- public class VirusStats
6
- {
46
+ import java .util .Scanner ;
7
47
8
- public static void main (String [] args )
9
- {
10
- // Case, death, and recovery numbers are those reported by John Hopkins University at 2:01 PM on Wednesday 4/22/2020
11
- // Population is as reported by worldometers.info at 2:01 PM on 4/22/2020
12
- // Death rate, recovery rate, and infection rate are calculated using above data.
13
- // Alveera's Risk Letter Grade is self determined and is a fun educated guess not actual scientific fact
48
+ public class VirusStats {
49
+ public static void main (String [] args ) {
50
+ // types of statistics
51
+ String [] stats = {"capital" , "number of cases" , "population" , "deaths" , "recoveries" , "death rate" , "recovery rate" , "infection rate" , "risk grade" };
14
52
15
53
// Data for 10 countries on COVID-19
16
54
String [] countries = {"United States" , "Spain" , "Italy" , "France" , "Germany" , "United Kingdom" , "Turkey" , "Iran" , "China" , "Russia" };
17
- String [] capitals = {"Washington D.C." , "Spain " , "Rome" , "Paris" , "Berlin" , "London" , "Ankara" , "Tehran" , "Beijing" , "Moscow" };
55
+ String [] capitals = {"Washington D.C." , "Madrid " , "Rome" , "Paris" , "Berlin" , "London" , "Ankara" , "Tehran" , "Beijing" , "Moscow" };
18
56
int [] population = {331002651 , 46754778 , 60461826 , 65273511 , 83783942 , 67886011 , 84339067 , 83992949 , 1439323776 , 145934462 };
19
57
int [] numberOfCases = {826248 , 208389 , 183957 , 159315 , 149044 , 134635 , 95591 , 85996 , 83868 , 57999 };
20
58
int [] deaths = {45894 , 21717 , 25085 , 20829 , 5165 , 18151 , 2376 , 5391 , 4636 , 513 };
@@ -27,138 +65,150 @@ public static void main(String[] args)
27
65
28
66
// calculate death rate, recovery rate,
29
67
// and infection rate for all countries
30
- for (int i = 0 ; i < countries .length ; i ++)
31
- {
68
+ for (int i = 0 ; i < countries .length ; i ++) {
69
+ // convert rates to a percent, and round to the nearest hundredth
32
70
deathRate [i ] = Math .round (deaths [i ] / (double ) numberOfCases [i ] * 10000 ) / 100.0 ;
33
71
recoveryRate [i ] = Math .round (recoveries [i ] / (double ) numberOfCases [i ] * 10000 ) / 100.0 ;
34
72
infectionRate [i ] = Math .round (numberOfCases [i ] / (double ) population [i ] * 10000 ) / 100.0 ;
35
73
}
36
74
37
- // Determining Risk Grade
38
- for (int x = 0 ; x < countries .length ; x ++)
39
- {
40
- if (infectionRate [x ] > 0.25 || deathRate [x ] > 25 )
41
- {
75
+ // determine risk grade
76
+ for (int x = 0 ; x < countries .length ; x ++) {
77
+ if (infectionRate [x ] > 0.25 || deathRate [x ] > 25 ) {
42
78
riskGrade [x ] = 'A' ;
43
- }
44
- else if (infectionRate [x ] > 0.2 || deathRate [x ] > 15 )
45
- {
79
+ } else if (infectionRate [x ] > 0.2 || deathRate [x ] > 15 ) {
46
80
riskGrade [x ] = 'B' ;
47
- }
48
- else if (infectionRate [x ] > 0.15 || deathRate [x ] > 10 )
49
- {
81
+ } else if (infectionRate [x ] > 0.15 || deathRate [x ] > 10 ) {
50
82
riskGrade [x ] = 'C' ;
51
- }
52
- else if (infectionRate [x ] > 0.1 || deathRate [x ] > 5 )
53
- {
83
+ } else if (infectionRate [x ] > 0.1 || deathRate [x ] > 5 ) {
54
84
riskGrade [x ] = 'D' ;
55
- }
56
- else
57
- {
85
+ } else {
58
86
riskGrade [x ] = 'F' ;
59
87
}
60
88
}
61
89
62
- // Printing out instructions
90
+ // Print out user instructions
63
91
System .out .println ("The following countries have been stored in our database:" );
64
- System .out .println ("[United States, Spain, Italy, France, Germany, United Kingdom, Turkey, Iran, China, Russia]\n " );
92
+
93
+ // print countries in the countries array
94
+ System .out .print ("[" );
95
+ for (int i = 0 ; i < countries .length ; i ++) {
96
+ System .out .print (countries [i ]);
97
+ if (i < countries .length - 1 ) {
98
+ System .out .print (", " );
99
+ } else {
100
+ System .out .println ("]" );
101
+ }
102
+ }
103
+
104
+ System .out .println ();
105
+
106
+ // print stat types
65
107
System .out .println ("The following stats are available for selection:" );
66
- System .out .println ("[Capital, Number of Cases, Population, Deaths, Recoveries, Death Rate, Recovery Rate, Infection Rate, Risk Grade]" );
108
+ System .out .print ("[" );
109
+ for (int i = 0 ; i < stats .length ; i ++) {
110
+ System .out .print (stats [i ]);
111
+ if (i < stats .length - 1 ) {
112
+ System .out .print (", " );
113
+ } else {
114
+ System .out .println ("]" );
115
+ }
116
+ }
117
+
67
118
System .out .println ("\t **Risk Grade is a rating of how dangerous the virus is in that country, A being most dangerous and F being least dangerous.\n " );
68
119
System .out .println ("Type \" quit\" to end the program.\n " );
69
120
70
- String country = "" ;
71
- String stat ;
72
- int index = -1 ;
121
+ String country ; // name of the country the user is researching
122
+ String stat ; // type of statistic the user wants
123
+ int index = -1 ; // index of the country
73
124
74
125
Scanner reader = new Scanner (System .in );
75
-
76
- // Enter a country
77
- while (!country .equalsIgnoreCase ("quit" ))
78
- {
79
- while (index == -1 )
80
- {
81
- System .out .print ("Which country’s stats would you like to research? " );
82
- country = reader .nextLine ();
83
- // Break While
84
- if (country .equalsIgnoreCase ("quit" ))
85
- {
86
- index = -2 ;
87
- }
88
- // Finds index
89
- for (int x = 0 ; x < countries .length ; x ++)
90
- {
91
- if (countries [x ].equalsIgnoreCase (country ))
92
- {
93
- index = x ;
126
+
127
+ boolean quit = false ; // whether the user wants to quit
128
+ do {
129
+ // keep asking the user for a country until
130
+ // they give a valid country
131
+ while (index == -1 ) {
132
+ System .out .print ("Which country's stats would you like to research? " );
133
+ country = reader .nextLine ().toLowerCase (); // case insensitive
134
+
135
+ // if user quits, exit the program
136
+ if (country .equals ("quit" )) {
137
+ System .out .println ("Quitting..." );
138
+ quit = true ;
139
+ break ;
140
+ } else { // user enters a country
141
+ // find the index of that country
142
+ for (int i = 0 ; i < countries .length ; i ++) {
143
+ if (country .equalsIgnoreCase (countries [i ])) { // case insensitive
144
+ index = i ; // match found
145
+ }
146
+ }
147
+
148
+ // if the country given is invalid
149
+ if (index == -1 ) {
150
+ // print error message
151
+ System .out .println ("Sorry, we don't have data for that country. Please try again." );
94
152
}
95
- }
96
- // Invalid Response
97
- if (index == -1 )
98
- {
99
- System .out .println ("This response was invalid; try again." );
100
153
}
101
154
}
102
-
103
- // QUIT GAME
104
- if (country .equalsIgnoreCase ("quit" ))
105
- {
106
- break ;
107
- }
108
-
109
- // Instructions
110
- System .out .print ("Which stat would you like? " );
111
- stat = reader .nextLine ();
112
- System .out .println ();
113
-
114
- // Bring stat
115
- if (stat .equalsIgnoreCase ("capital" ))
116
- {
117
- System .out .println (countries [index ] + "'s capital is " + capitals [index ] + "." );
118
- }
119
- else if (stat .equalsIgnoreCase ("number of cases" ))
120
- {
121
- System .out .println (countries [index ] + " has " + numberOfCases [index ] + " cases of COVID-19." );
122
- }
123
- else if (stat .equalsIgnoreCase ("population" ))
124
- {
125
- System .out .println (countries [index ] + " has a population of " + population [index ] + " people." );
126
- }
127
- else if (stat .equalsIgnoreCase ("deaths" ))
128
- {
129
- System .out .println (countries [index ] + " has " + deaths [index ] + " coronavirus related deaths." );
130
- }
131
- else if (stat .equalsIgnoreCase ("recoveries" ))
132
- {
133
- System .out .println (countries [index ] + " has " + recoveries [index ] + " people who recovered from COVID-19." );
134
- }
135
- else if (stat .equalsIgnoreCase ("death rate" ))
136
- {
137
- System .out .println (countries [index ] + " has a " + deathRate [index ] + "% coronavirus death rate." );
138
- }
139
- else if (stat .equalsIgnoreCase ("recovery rate" ))
140
- {
141
- System .out .println (countries [index ] + " has a " + recoveryRate [index ] + "% coronavirus recovery rate." );
142
- }
143
- else if (stat .equalsIgnoreCase ("infection rate" ))
144
- {
145
- System .out .println (countries [index ] + " has a " + infectionRate [index ] + "% COVID-19 infection rate." );
146
- }
147
- else if (stat .equalsIgnoreCase ("risk grade" ))
148
- {
149
- System .out .println (countries [index ] + " has a risk grade of " + riskGrade [index ] + "." );
150
- }
151
- else
152
- {
153
- System .out .println ("This response was invalid; try again." );
155
+
156
+ if (!quit ) {
157
+ boolean validStatistic = false ; // whether the user has entered a valid stat type
158
+
159
+ // keep asking user for a stat type until they give a valid stat type or quit the program
160
+ while (!validStatistic ) {
161
+ System .out .print ("Which stat would you like? " );
162
+ stat = reader .nextLine ().toLowerCase (); // case insensitive
163
+ validStatistic = true ; // assume stat type is valid
164
+
165
+ // bring up the stat the user wants
166
+ switch (stat ) {
167
+ case "capital" :
168
+ System .out .println (countries [index ] + "'s capital is " + capitals [index ] + "." );
169
+ break ;
170
+ case "number of cases" :
171
+ System .out .println (countries [index ] + " has " + numberOfCases [index ] + " cases of COVID-19." );
172
+ break ;
173
+ case "population" :
174
+ System .out .println (countries [index ] + " has a population of " + population [index ] + " people." );
175
+ break ;
176
+ case "deaths" :
177
+ System .out .println (countries [index ] + " has " + deaths [index ] + " COVID-19 related deaths." );
178
+ break ;
179
+ case "recoveries" :
180
+ System .out .println (countries [index ] + " has " + recoveries [index ] + " people who recovered from COVID-19." );
181
+ break ;
182
+ case "death rate" :
183
+ System .out .println (countries [index ] + " has a " + deathRate [index ] + "% COVID-19 death rate." );
184
+ break ;
185
+ case "recovery rate" :
186
+ System .out .println (countries [index ] + " has a " + recoveryRate [index ] + "% COVID-19 recovery rate." );
187
+ break ;
188
+ case "infection rate" :
189
+ System .out .println (countries [index ] + " has a " + infectionRate [index ] + "% COVID-19 infection rate." );
190
+ break ;
191
+ case "risk grade" :
192
+ System .out .println (countries [index ] + " has a risk grade of " + riskGrade [index ] + "." );
193
+ break ;
194
+ case "quit" :
195
+ System .out .println ("Quitting..." );
196
+ quit = true ;
197
+ break ;
198
+ default : // invalid stat type
199
+ System .out .println ("Sorry, we don't have that stat. Please try again." );
200
+ validStatistic = false ;
201
+ }
202
+ }
154
203
}
155
- // todo if you enter quit after asking "Which stat would you like?" it doesn't quit immediately
156
204
157
- // Prepare for loop
205
+ // reset index (next iteration will ask user for country again)
158
206
index = -1 ;
159
207
System .out .println ();
160
- System .out .println ();
161
- }
208
+ } while (!quit );
209
+
210
+ System .out .println ("You have successfully exited the program." );
211
+
162
212
reader .close ();
163
213
}
164
214
}
0 commit comments