Consider a City class that has:
Implement a well-structured Java program that uses the City class and CityDriver class to enable a climate scientist to maintain city monthly average rainfalls of up to 12 months. The average rainfall information is kept in a text-file rainfall.txt of the form:
Arusha Tanzania 50.0 80.0 170.0
Athens Greece 60.0 45.0 40.0
Bali Indonesia 90.0 90.0 90.0
DarEsSalaam Tanzania 76.3 54.9 138.1
Dubai UAE 18.8 25.0 22.1
Dublin NorthernIreland 70.0 60.0 70.0
London UK 55.2 40.9 41.6
Manila Philippines 20.0 10.0 10.0
where each line of the text-file contains a city name (a single word), a country name (a single word), followed by up to twelve monthly average rainfall values in millimeters (The above table shows the monthly average rainfall for January, February, and March only)
Note: The initial input file has no rainfall information. It is in the form:
Arusha Tanzania
Athens Greece
Bali Indonesia
DarEsSalaam Tanzania
Dubai UAE
Dublin NorthernIreland
London UK
Manila Philippines
Your CityDriver program must have the following main menu:
1. Display Rainfall Infomation for all cities.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for each city.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next month for all cities.
6. Add New city.
7. Delete a city.
8. Exit.
Please select your choice:
Your program must loop as long as option 8 has not been selected. It must display an appropriate error message if an invalid choice is entered. After executing each of the options 1 to 7, your program must pause and display the message: Press Enter key to continue . . . . Your program must display the main menu after pressing the Enter key. Each of the options 1 to 7 must be implemented in a separate private static method. The code for Press Enter key to continue . . . must also be implemented is a separate private static method. Your main method must handle IOException, IllegalArgumentException, and InputMismatchException by displaying an appropriate error message, then waiting for a key press, before returning control to the main menu.
The options must have the following behaviors:
Option 1: Display Rainfall Info for all cities
It displays the rainfall information of all cities if rainfall.txt contains at least one column of rainfall information; otherwise the error message: There is no rainfall information. is displayed. The option must be implemented by reading directly from rainfall.txt without using an array of City references. It then waits for the Enter key to be pressed before returning control to the main menu.
Below are sample runs for this option when there are 0, 1, or 3 columns of monthly rainfall averages in rainfall.txt :
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 1
There is no rainfall information in rainfall.txt
Press Enter key to continue. . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 1
City Country Jan
Arusha Tanzania 50.0
Athens Greece 60.0
Bali Indonesia 90.0
DarEsSalaam Tanzania 76.3
Dubai UAE 18.8
Dublin NorthernIreland 70.0
London UK 55.2
Manila Philippines 20.0
Press Enter key to continue . . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 1
City Country Jan Feb March
Arusha Tanzania 50.0 80.0 170.0
Athens Greece 60.0 45.0 40.0
Bali Indonesia 90.0 90.0 90.0
DarEsSalaam Tanzania 76.3 54.9 138.1
Dubai UAE 18.8 25.0 22.1
Dublin NorthernIreland 70.0 60.0 70.0
London UK 55.2 40.9 41.6
Manila Philippines 20.0 10.0 10.0
Press Enter key to continue . . .
Option 2: Display Rainfall Info for a particular city
If there is no rainfall information in rainfall.txt an error message There is no rainfall information. is displayed; otherwise the option prompts for and reads a city name and its country name. It then searches, without case-sensitivity, for this city name and country name pair in rainfall.txt. If the name pair is not found an appropriate error message is displayed, otherwise; the city rainfall information is displayed, together with the annual rainfall of this city in millimiters. In both cases, the option waits for the Enter key to be pressed before returning control to the main menu. The option must be implemented by reading directly from rainfall.txt without using an array of City references.
Below are sample runs for this option when there is 0 and 3 of monthly rainfall averages in rainfall.txt and when an invalid city and country pair is entered:
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 2
There is no rainfall information in rainfall.txt
Press Enter key to continue. . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 2
Enter city name:
DarEsSalaam
Enter country name:
Tanzania
City Country Jan Feb March
DarEsSalaam Tanzania 76.3 54.9 138.1
Press Enter key to continue. . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 2
Enter city name:
Manila
Enter country name:
India
Error: Invalid city and country pair.
Press Enter key to continue. . .
Option 3: Display total rainfall for each city.
If there is no rainfall information in rainfall.txt an error message There is no rainfall information. is displayed; otherwise the option displays the monthly rainfall averages and the total rainfall of all cities by reading directly from rainfall.txt without using an array of City references. Control is then returned to the main menu after pressing the Enter key.
Below are sample runs for this option when there are 0, 1, or 3 columns of monthly rainfall averages in rainfall.txt :
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 3
There is no rainfall information in rainfall.txt
Press Enter key to continue. . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 3
City Country Jan Total(mm)
Arusha Tanzania 50.0 50.0
Athens Greece 60.0 60.0
Bali Indonesia 90.0 90.0
DarEsSalaam Tanzania 76.3 76.3
Dubai UAE 18.8 18.8
Dublin NorthernIreland 70.0 70.0
London UK 55.2 55.2
Manila Philippines 20.0 20.0
Press Enter key to continue. . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 3
City Country Jan Feb March Total (mm)
Arusha Tanzania 50.0 80.0 170.0 300.0
Athens Greece 60.0 45.0 40.0 145.0
Bali Indonesia 90.0 90.0 90.0 270.0
DarEsSalaam Tanzania 76.3 54.9 138.1 269.3
Dubai UAE 18.8 25.0 22.1 65.9
Dublin NorthernIreland 70.0 60.0 70.0 200.0
London UK 55.2 40.9 41.6 137.7
Manila Philippines 20.0 10.0 10.0 40.0
Press Enter key to continue . . .
Option 4. Modify a particular rainfall average for a particular city and country pair
If there is no rainfall information in rainfall.txt an error message There is no rainfall information. is displayed; otherwise the option prompts for and reads a city name, a country name, and the month number, and the average rainfall. If the month number is invalid (less than 1 or greater than the current number of months in the text-file), or the new average rainfall is negative or greater than 10000 millimeters, an appropriate error message is displayed; otherwise it searches, without case-sensitivity, for this city and country pair in rainfall.txt. If the pair is not found an appropriate error message is displayed, otherwise; the contents of the text-file are copied into an array of City references (Count the number of lines in the text-file, and use this number as the size of the City array. Count the number of rainfall averages in any line and use it as the size of each averageMonthlyRainfall array). The city average rainfall for the required month is updated in the City array, and the rainfall.txt is then updated and the modified city averages are displayed:
Below are sample runs of this option if there are 0 and 3 columns in rainfall.txt and if invalid month and city/country pair are entered:
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 4
There is no rainfall information in rainfall.txt
Press Enter key to continue. . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 4
Please enter city name:
Bali
Please enter country name:
Indonesia
Please enter month number [1 – 3]: 2
Please enter new monthly rainfall average for month#2: 120.0
Before modification: [City name = Bali, Country name = Indonesia,Average monthly rainfall = [90.0, 90.0, 90.0]]
After modification: [City name = Bali, Country name = Indonesia,Average monthly rainfall = [90.0, 120.0, 90.0]]
Rainfall file has been updated . . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 4
Please enter city name:
Athens
Please enter country name:
Greece
Please enter month number [1 – 3]: 4
Error: Invalid month number
Press Enter key to continue. . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 4
Please enter city name:
Dhahran
Please enter country name:
SaudiArabia
Error: City and country pair not in rainfall.txt
Press Enter key to continue. . .
Option 5: Add monthly rainfall average for the current next month for all cities
It prompts the user to enter rainfall averages for month n + 1, where n is the current number of months for each city. If n is 12 an error message is displayed, otherwise the contents of the rainfall.txt are copied into an array of City references (Count the number of lines in the text-file, and use this number as the size of the City array. Count the number of rainfall averages n in a line and use an array of size n + 1 for each averageMonthlyRainfall array). The program then prompts for and reads the new rainfall average of each city. The file rainfall.txt is then updated.
Below are sample runs if this option if there are 0, 1, or 2 columns of average rainfalls in rainfall.txt and if an invalid monthly average rainfall value is entered:
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 5
Please enter the average rainfall values for Month#1
Please enter average rainfall value [mm] of Month#1 for the City of Arusha of Tanzania: 50.0
Please enter average rainfall value [mm] of Month#1 for the City of Athens of Greece: 60.0
Please enter average rainfall value [mm] of Month#1 for the City of Bali of Indonesia: 90.0
Please enter average rainfall value [mm] of Month#1 for the City of DarEsSalaam of Tanzania: 76.3
Please enter average rainfall value [mm] of Month#1 for the City of Dubai of UAE: 18.8
Please enter average rainfall value [mm] of Month#1 for the City of Dublin of NorthernIreland: 70.0
Please enter average rainfall value [mm] of Month#1 for the City of London of UK: 55.2
Please enter average rainfall value [mm] of Month#1 for the City of Manila of Philippines: 20.0
Rainfall file has been updated . . .
Press Enter key to continue . . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 5
Please enter the average rainfall values for Month#2
Please enter average rainfall value [mm] of Month#2 for the City of Arusha of Tanzania: 80.0
Please enter average rainfall value [mm] of Month#2 for the City of Athens of Greece: 45.0
Please enter average rainfall value [mm] of Month#2 for the City of Bali of Indonesia: 90.0
Please enter average rainfall value [mm] of Month#2 for the City of DarEsSalaam of Tanzania: 54.9
Please enter average rainfall value [mm] of Month#2 for the City of Dubai of UAE: 25.0
Please enter average rainfall value [mm] of Month#2 for the City of Dublin of NorthernIreland: 60.0
Please enter average rainfall value [mm] of Month#2 for the City of London of UK: 40.9
Please enter average rainfall value [mm] of Month#2 for the City of Manila of Philippines: 10.0
Rainfall file has been updated . . .
Press Enter key to continue . . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 5
Please enter the average rainfall values for Month#3
Please enter average rainfall value [mm] of Month#3 for the City of Arusha of Tanzania: 170.0
Please enter average rainfall value [mm] of Month#3 for the City of Athens of Greece: 40.0
Please enter average rainfall value [mm] of Month#3 for the City of Bali of Indonesia: 90.0
Please enter average rainfall value [mm] of Month#3 for the City of DarEsSalaam of Tanzania: 138.1
Please enter average rainfall value [mm] of Month#3 for the City of Dubai of UAE: 22.1
Please enter average rainfall value [mm] of Month#3 for the City of Dublin of NorthernIreland: 70.0
Please enter average rainfall value [mm] of Month#3 for the City of London of UK: 41.6
Please enter average rainfall value [mm] of Month#3 for the City of Manila of Philippines: 10.0
Rainfall file has been updated . . .
Press Enter key to continue . . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 5
Please enter the average rainfall values for Month#4
Please enter average rainfall value [mm] of Month#4 for the City of Arusha of Tanzania: 360.0
Please enter average rainfall value [mm] of Month#4 for the City of Athens of Greece: 30.0
Please enter average rainfall value [mm] of Month#4 for the City of Bali of Indonesia: 70.0
Please enter average rainfall value [mm] of Month#4 for the City of DarEsSalaam of Tanzania: -54.0
Error: java.lang.IllegalArgumentException: Invalid average monthly rainfall
Press Enter key to continue . . .
Option 6: Add New City
It prompts for and read the city name and the country name of the city to be added. It then searches the file rainfall.txt for the name pair. If the pair exists, an error message is displayed and control is returned to the main menu; otherwise the option prompts for and reads n monthly rainfall averages, where n is the current number of monthly rainfall averages for each city in rainfall.txt. The program finally appends the city name, the country name, and the monthly rainfall averages to rainfall.txt. If n is zero, the city name and country name are appended without rainfall information.
Note: Append the new city information to the end of the text-file by an output statement that first generates a new line; but that does not generate an extra new line at the end of the text-file.
Below are sample runs if this option:
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 6
Enter city name:
Athens
Enter country name:
Greece
Error: Duplicate City and Country pair.
Press Enter key to continue . . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 6
Enter city name:
Marakesh
Enter country name:
Morocco
Enter month#1 average rainfall value [mm]: 32.0
Enter month#2 average rainfall value [mm]: 38.0
Enter month#3 average rainfall value [mm]: 38.0
rainfall.txt file successfully updated . . .
Press Enter key to continue . . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 6
Enter city name:
Miami
Enter country name:
USA
Enter month#1 average rainfall value [mm]: 45.7
Enter month#2 average rainfall value [mm]: 20000.0
Error: Invalid monthly average rainfall value.
Press Enter key to continue . . .
Option 7: Delete City
To implement option 7, search rainfall.txt for the city name and country name pair of the city to be deleted, keeping track of its line number in the file. If the pair does not exist, display an error; otherwise read the rainfall informations from the text-file into an array of City references, then overwrite the text-file with the contents of the array, but skipping the information of the city you want to delete. Make sure you write the last line without generating an extra blank line at the end of the text-file.
Below are sample runs of this option:
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 7
Enter city name:
Sydney
Enter country name:
Australia
Error: No such city and country pair.
Press Enter key to continue . . .
1. Display Rainfall Infomation for all.
2. Display Rainfall Infomation for a particular city.
3. Display total rainfall for all cities.
4. Modify a particular rainfall average for a particular city and country pair.
5. Add monthly rainfall average for the current next rrcnth for all cities.
6. Add New City.
7. Delete City.
8. Exit.
Please select your choice: 7
Enter city name:
Bali
Enter country name:
Indonesia
City Bali of Indonesia deleted
Rainfall file has been updated . . .
Press Enter key to continue . . .
Note:
Hints:
The following is a table of some cities and their average monthly rainfall in millimeters. You may use some of this data:
City Country Pair Jan Feb March April May Jun July Aug Sept Oct Nov Dec
Arusha Tanzania 50.0 80.0 170.0 360.0 210.0 30.0 10.0 10.0 20.0 30.0 110.0 100.0
Athens Greece 60.0 45.0 40.0 30.0 23.0 10.0 8.0 8.0 15.0 55.0 60.0 90.0
Bali Indonesia 90.0 90.0 90.0 70.0 70.0 50.0 40.0 40.0 50.0 60.0 70.0 90.0
DarEsSalaam Tanzania 76.3 54.9 138.1 254.2 197.8 42.9 26.6 24.1 22.8 69.3 125.9 117.8
Dubai UAE 18.8 25.0 22.1 7.2 0.4 0.0 0.0 0.0 0.0 1.1 2.7 16.2
Dublin NorthernIreland 70.0 60.0 70.0 50.0 50.0 70.0 50.0 80.0 60.0 80.0 60.0 80.0
London UK 55.2 40.9 41.6 43.7 49.4 45.1 44.5 49.5 49.1 68.5 59.0 55.2
Manila Philippines 20.0 10.0 10.0 30.0 120.0 260.0 400.0 360.0 340.0 190.0 130.0 60.0
Marrakesh Morocco 32.0 38.0 38.0 39.0 25.0 5.0 2.0 3.0 6.0 24.0 42.0 15.0
Miami USA 53.1 59.2 76.2 81.3 126.5 210.1 110.5 161.8 200.2 113.5 69.6 52.1
Moscow Russia 52.0 41.0 35.0 37.0 49.0 80.0 85.0 82.0 68.0 71.0 55.0 52.0
Moscow USA 80.0 59.9 68.0 64.0 64.0 48.0 24.9 24.9 31.0 55.1 92.0 76.0
Mumbai India 0.6 1.3 0.2 0.7 12.5 523.1 799.7 529.7 312.3 55.8 16.8 5.3
NewYork USA 80.0 60.0 85.0 90.0 115.0 95.0 100.0 80.0 105.0 80.0 65.0 80.0
Paris France 18.0 22.0 24.0 25.0 26.0 24.0 23.0 22.0 16.0 25.0 23.0 26.0
Riyadh SaudiArabia 10.0 10.0 30.0 30.0 10.0 0.0 0.0 0.0 0.0 0.0 0.0 10.0
Sydney Australia 96.0 136.6 109.4 137.0 117.6 117.8 80.8 91.8 69.2 82.2 104.8 79.4
Sydney Canada 19.0 13.0 15.0 13.0 7.0 5.5 4.5 7.0 8.0 16.0 21.0 24.0
Tokyo Japan 40.0 53.0 80.0 120.0 130.0 170.0 125.0 140.0 170.0 155.0 70.0 40.0
Waterloo Canada 28.7 29.7 36.8 68.0 81.8 82.4 98.6 83.9 87.8 66.1 75.0 38.0
Waterloo Belgium 65.0 55.0 70.0 58.0 67.0 79.0 75.0 62.0 58.0 70.0 80.0 78.0