29. Using ArrayList, write a menu based program that accepts a shopping list of four items from the command line and store in a Vector and perform the following operations.
a. To add an item at specific location in the list.
b. To delete an item in the list
c. To print the contents of the vector
d. To delete all elements
e. To add an item at the end of the vector
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class ShoppingList {
public static void main(String[] args) {
ArrayList amante=new ArrayList();
String choice []= { "Add an item at specific location in the list", "Delete an item in the list", "Print the contents of the vector","Delete all elements","Exit"};
String choice1,item;
int index;
for(int a=0;a<4;a++){
item=JOptionPane.showInputDialog("Enter item: ");
amante.add(item);
}
do{
String display = "";
choice1 = (String) JOptionPane.showInputDialog(null,
"Welcome! Please select an option.",
"Options",JOptionPane.QUESTION_MESSAGE,null, choice,choice[0]);
if (choice1.compareTo(choice[0]) == 0){
index=Integer.parseInt(JOptionPane.showInputDialog("Enter specified index: "));
item=JOptionPane.showInputDialog("Enter item: ");
amante.remove(index);
amante.add(index,item);
}
else if (choice1.compareTo(choice[1]) == 0){
index=Integer.parseInt(JOptionPane.showInputDialog("Enter specified index of the item to delete: "));
amante.remove(index);
amante.add(index,null);
}
else if (choice1.compareTo(choice[2]) == 0){
for(int y=0;y<4;y++){
display=display+"Item["+y+"] - "+amante.get(y)+"\n";
}
JOptionPane.showMessageDialog(null, display);
}
else if (choice1.compareTo(choice[3]) == 0){
for(int z=0;z<4;z++){
amante.remove(z);
amante.add(z,null);
}
}
}
while(!choice1.equals("Exit"));
JOptionPane.showMessageDialog(null, "Thank you for using the program! We hope to serve you next time!","Amante Programs Inc.",JOptionPane.PLAIN_MESSAGE);
}
}
Walang komento:
Mag-post ng isang Komento