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: Houston, Charleston, and Tampa.

Solution

package com.ap.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test {

   public static void main(String[] args) {
       List<String> cities = new ArrayList<String>();
       Scanner scanner = new Scanner(System.in);
       /*
       * 1 – Add city 2 – Change city name 3 – Delete city 4 – List cities 9 –
       * Exit
       */
       int i = 1;
       while (i == 1) {
           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(\"5.exit\");
           System.out.println(\"enter your choice\");
           int choice = scanner.nextInt();
           switch (choice) {
           case 1:
               System.out.println(\"enter the city names to add to the list\");
               String city = scanner.next();
               cities.add(city);
               break;
           case 2:
               System.out.println(\"enter the orignal city name \");
               String city1 = scanner.next();
               System.out.println(\"enter the name to rename the city\");
               String rename = scanner.next();
               int count = 0;
               for (String name : cities) {

                   if (city1.trim().equals(name.trim())) {

                       cities.set(count, rename);
                       System.out.println(\"city updated\");
                   }
                   count++;
               }
               break;
           case 3:
               System.out.println(\"enter the city you want to delete\");
               String city2 = scanner.next();

               for (String name : cities) {

                   if (city2.trim().equals(name.trim())) {

                       cities.remove(city2);
                       System.out.println(\"city deleted\");
                   }
               }
               break;
           case 4:
               System.out.println(\"all cities in the list are \");

               for (String name : cities) {
                   System.out.println(name);
               }
               break;
           case 5:
               System.out.println(\"do you want to exit type y/n\");
               String con = scanner.next();
               if (con.equals(\"y\")) {
                   System.exit(0);

               }
               break;
           default:
               System.out.println(\"please enter correct option\");
               break;
           }

       }

   }
}

output

1.add city
2.change city name
3.delete city
4.list cities
5.exit
enter your choice
1
enter the city names to add to the list

bouston
1.add city
2.change city name
3.delete city
4.list cities
5.exit
enter your choice
1
enter the city names to add to the list
houston
1.add city
2.change city name
3.delete city
4.list cities
5.exit
enter your choice
4
all cities in the list are
bouston
houston
1.add city
2.change city name
3.delete city
4.list cities
5.exit
enter your choice
5
do you want to exit type y/n
y

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