Youve been hired by City Skies to write a Java console appli

You\'ve been hired by City Skies to write a Java console application that manages city names. Use an array of size 30 to store the names. Present the following menu to the user:

          City Skies Menu

          1 – Add city

          2 – Change city name

          3 – Delete city

          4 – List cities

          9 – Exit

          Enter an option:

Continue to read an option, process it, and display the menu until the user enters option 9. Here are option descriptions:

          Add city – prompt for a city name. If the city is already in the list, don’t add it and tell the user. If the city is not in the list, add it and tell the user. You don’t need to test if the array is full before adding a city.

          Change city name – prompt for a city (array) index. If the index is invalid, tell the user. If the index is valid, prompt for the new city name. If the new city name is already in the list, don’t add it and tell the user. If the new city name is not in the list, add it and tell the user.

          Delete city – prompt for a city (array) index. If the index is invalid, tell the user. If the index is valid, delete the city and move all the cities after the index up one to close the gap.

          List cities – use printf to format the list in two columns with the first column containing an array index and the second column containing the city name. Also, show a city count after the list.

Redisplay the menu after each option is processed. Start the array with three values already in it: Detroit, Dearborn, and Windsor. Use these inputs for your last run:

          Option         Value(s)

          1                  Ann Arbor

          4

          2                  6

          2                  3, Dexter

          4

          2                  3, Dearborn

          3                  6

          3                  0

          4

Solution

Following java console application that manages city name:

Java Code:

import java.util.Scanner;

/**
* @author ramesh
*
*/
public class CitySkies {
  
   private String[] cities=new String[30];
   private int totalCities=0;
  
   /**
   * Constructor to initialize Start the array with three values already in it:
   * Detroit, Dearborn, and Windsor
   */
   public CitySkies() {
       this.cities[0]=\"Detroit\";
       this.cities[1]=\"Dearborn\";
       this.cities[2]=\"Windsor\";
       totalCities=3;
   }
  
   /**
   * Add city – prompt for a city name. If the city is already in the list,
   * don’t add it and tell the user. If the city is not in the list, add it
   * and tell the user. You don’t need to test if the array is full before
   * adding a city.
   */
   public void addCity(String cityName){
       boolean isCityExist=false;
       for (String city : cities) {
           if(cityName.equals(city)){
               System.out.println(\"City name already exist.\");
               isCityExist=true;
               break;
           }
       }
      
       //check if city not present
       if(!isCityExist){
           cities[totalCities]=cityName;
           System.out.println(\"City name \"+cityName+\" added in the list.\");
           totalCities++;
       }
   }
   /**
   * Change city name – prompt for a city (array) index. If the index is invalid,
   * tell the user. If the index is valid, prompt for the new city name.
   * If the new city name is already in the list, don’t add it and tell the user.
   * If the new city name is not in the list, add it and tell the user.
   */
   public void changeCityName(int arrayIndex,Scanner scanner){
       //Check array index boundries for totalCities array
       if(arrayIndex>=0 && arrayIndex<30){
           System.out.println(\"The new city name\");
           //Call add city method to add city to list
           addCity(scanner.next());
       }else{
           System.out.println(\"Index is invalid\");
       }
   }

   /**
   * Delete city – prompt for a city (array) index. If the index is invalid, tell the user.
   * If the index is valid, delete the city and move all the cities after the index up one
   * to close the gap
   */
   public void deleteCity(int arrayIndex){
       //Check array index boundries for totalCities array
       if(arrayIndex>=0 && arrayIndex<totalCities){
           //Logic to delete city
           for(int index=arrayIndex;index<totalCities;index++){
               cities[index]=cities[index+1];
           }
           totalCities--;
       }else{
           System.out.println(\"Index is invalid\");
       }
   }

   /**
   * List cities – use printf to format the list in two columns with the first column containing
   * an array index and the second column containing the city name. Also, show a city count after
   * the list.
   */
   public void listCities(){
       //Header
       System.out.println(\"Array index \\t\\t City name\");
       int arrayIndex=0;
       //Loop to iterate cities
       while (cities[arrayIndex]!=null) {
           System.out.printf(\"%d \\t\\t\\t %s\ \",arrayIndex,cities[arrayIndex]);
           arrayIndex++;
       }
       System.out.println(\"City count:\"+totalCities);
      
   }

   /**
   * @param args
   */
   public static void main(String[] args) {
       try (Scanner scanner = new Scanner(System.in)) {
           //CitySkies object creation
           CitySkies citySkies=new CitySkies();
           while (true) {
               System.out.println(\"City Skies Menu\");
               System.out.println(\"1 – Add city\");
               System.out.println(\"2 – Change city name\");
               System.out.println(\"3 – Delete city\");
               System.out.println(\"4 – List cities\");
               System.out.println(\"9 – Exit\");
               System.out.println(\"Enter an option:\");
               // Store user choice
               int choice = scanner.nextInt();
               // Check user choice is 9 then exit the program
               if (choice == 9)
                   break;
               // Switch case for other operation
               switch (choice) {
               case 1:
                   System.out.println(\"City name\");
                   citySkies.addCity(scanner.next());  
                   break;
               case 2:
                   System.out.println(\"A city (array) index\");
                   citySkies.changeCityName(scanner.nextInt(),scanner);
                   break;
               case 3:
                   System.out.println(\"A city (array) index\");
                   citySkies.deleteCity(scanner.nextInt());
                   break;
               case 4:
                   citySkies.listCities();
                   break;

               default:
                   System.out.println(\"Invalid option.\");
                   break;
               }

           }
       }
   }

}

Sample Output:

City Skies Menu
1 – Add city
2 – Change city name
3 – Delete city
4 – List cities
9 – Exit
Enter an option:
4
Array index        City name
0 Detroit
1 Dearborn
2 Windsor
City count:3
City Skies Menu
1 – Add city
2 – Change city name
3 – Delete city
4 – List cities
9 – Exit
Enter an option:
1
City name
India
City name India added in the list.
City Skies Menu
1 – Add city
2 – Change city name
3 – Delete city
4 – List cities
9 – Exit
Enter an option:
4
Array index        City name
0 Detroit
1 Dearborn
2 Windsor
3 India
City count:4
City Skies Menu
1 – Add city
2 – Change city name
3 – Delete city
4 – List cities
9 – Exit
Enter an option:
2
A city (array) index
2
The new city name
US
City name US added in the list.
City Skies Menu
1 – Add city
2 – Change city name
3 – Delete city
4 – List cities
9 – Exit
Enter an option:
4
Array index        City name
0 Detroit
1 Dearborn
2 Windsor
3 India
4 US
City count:5
City Skies Menu
1 – Add city
2 – Change city name
3 – Delete city
4 – List cities
9 – Exit
Enter an option:
3
A city (array) index
2
City Skies Menu
1 – Add city
2 – Change city name
3 – Delete city
4 – List cities
9 – Exit
Enter an option:
4
Array index        City name
0 Detroit
1 Dearborn
2 India
3 US
City count:4
City Skies Menu
1 – Add city
2 – Change city name
3 – Delete city
4 – List cities
9 – Exit
Enter an option:

You\'ve been hired by City Skies to write a Java console application that manages city names. Use an array of size 30 to store the names. Present the following
You\'ve been hired by City Skies to write a Java console application that manages city names. Use an array of size 30 to store the names. Present the following
You\'ve been hired by City Skies to write a Java console application that manages city names. Use an array of size 30 to store the names. Present the following
You\'ve been hired by City Skies to write a Java console application that manages city names. Use an array of size 30 to store the names. Present the following
You\'ve been hired by City Skies to write a Java console application that manages city names. Use an array of size 30 to store the names. Present the following

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site