Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Homework HelpRSS Feeds

Program that accepts a shopping list of items from the command line and stores them in a vector. Also provide facility to perform operation

Posted By: Easy Tutor     Category: Java     Views: 5533

Write a program that accepts a shopping list of items from the command line and stores them in a vector. Also provide facility to perform following operations
a) To delete an item in the list.
b) To add an item at a specified location in the list.
c) To add an item at the end of the list.
d) To print the contents of the vector.

Code for Program that accepts a shopping list of items from the command line and stores them in a vector. Also provide facility to perform operation in Java

import java.util.*;
import java.io.*;

class  MenuDriven
{
    publicstaticvoid main(String args[]) 
    {
         Vector itemList = new Vector();
          String str,item;
          int i,j,len,choice,pos;
           
           len=args.length;
          for(i=0;i<len;i++)
              itemList.addElement(args[i]);
          
          while(true)
          {
               System.out.println("\n\nChoose your choice ...");
               System.out.println("1) Delete  Item");
               System.out.println("2) Add Item at Specified Location ");
               System.out.println("3) Add Item at  the End of the list");
               System.out.println("4) Print Vector List ");
               System.out.println("5) Exit");
               System.out.print("Enter your choice : ");
               System.out.flush();
               try{
                   BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
                   str=obj.readLine();
                   choice=Integer.parseInt(str);
                   switch(choice)
                   {
                       case 1 :    System.out.print("Enter Item you want to delete : ");
                                           str=obj.readLine();
                                        itemList.removeElement(str);  //string is not needed to convert object type as it
                                                                                          //is already object of class String
                                        break;
                      case 2 :     System.out.print("Enter Item to be Insert : ");
                                        System.out.flush();
                                        item=obj.readLine();
                                        System.out.print("Enter Position to insert item : ");
                                        str=obj.readLine();
                                        pos=Integer.parseInt(str);
                                        itemList.insertElementAt(item,pos-1);
                                        break;
                      case 3 :     System.out.print("Enter Item to be Insert : ");
                                        System.out.flush();
                                        item=obj.readLine();
                                        itemList.addElement(item);
                                        break;
                     case 4 :      len=itemList.size();
                                        System.out.println("\nItem Display ");
                                        for(i=0;i<len;i++)
                                        {
                                               System.out.println((i+1)+") "+itemList.elementAt(i));
                                        }
                                        break;
                      case 5 :     System.out.println("\n\nThank You for using this software.....");
                                        System.exit(1);
                                        break;
                      default :     System.out.println("\nEntered Choice is Invalid\nTry Again\n");
                    }
                }
            catch(Exception e) {}
         }      
    }
}
  
Share: 



Easy Tutor
Easy Tutor author of Program that accepts a shopping list of items from the command line and stores them in a vector. Also provide facility to perform operation is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in Java:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Mahendra Shelar from India Comment on: Jul 28
I want to small program of database managment using c,c++

View All Comments