30. Using Vector class, write the same process program above.
import java.util.Vector;
import javax.swing.JOptionPane;
public class ShoppingList{
public static void main(String[] args) {
Vector amante=new Vector();
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