create a menu driven program. the program should include a menu function that wi
ID: 3715028 • Letter: C
Question
create a menu driven program. the program should include a menu function that will take a list of options and return an int in the range of range(len(options)).
what kind of list do you want? Grocery MENU OPTIONS 0. quit 1. add item 2. delete item 3. print the list enter choice: 3 Grocery List The list is empty MENU OPTIONS 0. quit 1. add item 2. delete item 3. print the list enter choice: 1 enter new item: bread "bread' added to the list MENU OPTIONS 0. quit 1. add item 2. delete item 3. print the list enter choice: 1 enter new item: bread bread' is already in the list and not duplicated MENU OPTIONS 0. quit 1. add item 2. delete item 3. print the list enter choice: 2 enter item to delete: cheese "cheese' is not in the list and cannot be deletedExplanation / Answer
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CurdOperations {
public static void main(String[] args) {
List<String> outputList= new ArrayList<String>();
Scanner sc=new Scanner(System.in);
for(;;){
System.out.println("What kind of list do you want ? Geocery");
System.out.println("Menu Options:");
System.out.println("0. Quit");
System.out.println("1. Add Item");
System.out.println("2. Delte Item");
System.out.println("3. Print the list");
System.out.println("Please enter your choice");
int option=sc.nextInt();
if(option==0){
System.out.println("You selected to Quit BYEEEEEE");
break;}
switch (option) {
case 1: System.out.println("Please enter the element: ");
String element= sc.next();
outputList = AddItem(element , outputList);
break;
case 2:
System.out.println("Please enter the element: ");
String element1= sc.next();outputList = DelteItem(element1 ,outputList);
break;
case 3: outputList = PrintList( outputList);
break;
}
System.out.println(outputList);
}
}
public static List<String> AddItem( String str , List<String> outputList ){
if(outputList.contains(str)){
System.out.println(str +" is already there in the list");
}
else{outputList.add(str);}
return outputList;
}
public static List<String> DelteItem( String str , List<String> outputList ){
try {
if(outputList.contains(str)){
outputList.remove(str);
}
else{ System.out.println( str+ " is not there in the list");}
} catch (Exception e) {
e.printStackTrace();
}
return outputList;
}
public static List<String> PrintList( List<String> outputList ){
return outputList;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.