ArrayList with User Input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eikkaj
    New Member
    • Nov 2011
    • 1

    ArrayList with User Input

    I created something using an array previously that basically added ShoppingItems to a ShoppingList that would be sorted by priority and then purchased using a "budget".

    I am trying to change this from an array to an arraylist but am having trouble.

    First, I need to create the arraylist and get the input to create the shoppingitems, which include a bunch of different input (priority, cost, name, quantity).

    For the array, I had this:

    Code:
     public static void main(String[] args)
    	{
    		ShoppingList go = new ShoppingList();
                    go.getElement();
                    go.Sort();
    		System.out.println("hello");
    		go.displayResults();
    	}
    And then the getElement method was this:

    Code:
    public void getElement(){
            System.out.println("You can add seven items to purchase when prompted ");
    	shoppingList = new ShoppingItem[numItems]; //creation of new array object
    	for (int i = 0; i<= numItems - 1; i++)
            {
                shoppingList[i] = new ShoppingItem(); //shopping item objects created
                System.out.println("Enter data for the shopping item " + i);
                shoppingList[i].readInput();
                System.out.println();
            }
       }


    So with my arraylist, I'm thinking it should be something like this?

    Code:
    public static void main(String[] args) 
        {
            ArrayList<ShoppingItem>ShoppingList = new   ArrayList<ShoppingItem>();
            ShoppingList.add(getElement());
Working...