JAVA HELP PLEASE Youve been hired by City Skies to write a J

JAVA HELP PLEASE

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 Add city Change city name Delete city List cities 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.

Solution

JAVA PROGRAM

import java.util.*;
import java.util.Arrays.*;

public class CitySkies
{
   public static void main(String args[])
   {  
       String[] cities=new String[30];
       Scanner in=new Scanner(System.in);
       int count=0,choice=0;
       do
       {
       System.out.println();
       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: \");
       choice=in.nextInt();
       System.out.println();
      
       switch(choice)
       {

           case 1: System.out.println(\"Enter the city to add\");
               String city=in.next();
               boolean b=Arrays.asList(cities).contains(city);
              
               if (b==true)
               {
                   System.out.println(\"City already in the list\");
                   System.out.println();
               }
               else
               {
                   cities[count]=city;
                   count++;
               }
               break;


           case 2: System.out.println(\"Enter the city index to change\");
               int k=in.nextInt();
               if (k>=0 && k<=29 && cities[k]!=null)
               {
                   System.out.println(\"Enter the new City name\");
                   String new_city=in.next();
                   boolean n=Arrays.asList(cities).contains(new_city);
              
                   if (n==true)
                   {
                       System.out.println(\"City already in the list\");
                       System.out.println();
                   }
                   else
                   {
                       cities[k]=new_city;
                       System.out.println(\"City name is changed\");
                       System.out.println();
                   }
               }
               else
               {
                   System.out.println(\"Index is not valid\");
                   System.out.println();
               }
               break;

           case 3: System.out.println(\"Enter the city index to delete\");
               int d=in.nextInt();
               if (d>=0 && d<=29 && cities[d]!=null)
               {
                   String e=cities[d];
                   for(int i = d+1; i<cities.length; i++) {
                            cities[i-1] = cities[i];
                        }
                   count--;
                   System.out.println(\"City have been removed \");
                   System.out.println();
               }
               else
               {
                   System.out.println(\"Index is not valid\");
               }
               break;
           case 4: System.out.println(\"--------------------------\");
               for(int i=0;i<cities.length;i++)
               { /*
                   if(cities[i]!=null)
                   {
                   System.out.print(i);
                   System.out.println(\"\\t\"+cities[i]);
                   } */
                   if(cities[i]!=null)
                   {
                   System.out.print(\"|\");
                   System.out.format(\"%2d | %-18s |\ \",i,cities[i]);
                   System.out.println(\"--------------------------\");
                   }
               }
              
              
               System.out.println(\"Total cities in the list is \" +count);
               System.out.println();
               break;
              
              
           case 9:System.exit(0);
           default:System.out.println(\"enter an valid choice\");
               System.out.println();
       }
   }while(choice!=9);
  
}
}


SAMPLE OUTPUT

City Skies Menu
1 -- Add City
2 -- Change city name
3 -- Delete city
4 -- List cities
9 -- Exit
Enter an option:
1

Enter the city to add
London

City Skies Menu
1 -- Add City
2 -- Change city name
3 -- Delete city
4 -- List cities
9 -- Exit
Enter an option:
1

Enter the city to add
Newyork

City Skies Menu
1 -- Add City
2 -- Change city name
3 -- Delete city
4 -- List cities
9 -- Exit
Enter an option:
1

Enter the city to add
Newyork
City already in the list


City Skies Menu
1 -- Add City
2 -- Change city name
3 -- Delete city
4 -- List cities
9 -- Exit
Enter an option:

JAVA HELP PLEASE 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. Prese
JAVA HELP PLEASE 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. Prese
JAVA HELP PLEASE 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. Prese

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site