Hi guys,

I'm working on a console based application and I'm struck in a control flow issue for driver class. Any assistance is appreciated.

This method from the driver class call method from inventory class to search for a item that is stored in an arraylist. On query it return an arraylist of matching items if any match is found. The returned arraylist need to be numbered, formatted and presented to the user.

user select the item and the method call a method from shoppingcart class that add it to it to its arraylist. I have already completed all necessary methods for inventory and shoppingcart class.

DRIVER CLASS

private void addItemShoppingCart(){

CREATE SCANNER OBJECT
ACCEPT USER QUERY

CALL INVENTORY SEARCH METHOD FROM INVENTORY CLASS

INVENTORY SEARCH METHOD RETURN AN ARRAYLIST OF MATCHING ITEMS

IF ARRAYLIST FROM INVENTORY SEARCH METHOD IS NOT EMPTY{

PRINT AND NUMBER EACH MATCHING ITEM's PROPERTIES FROM ARRAYLIST <- I'm struck here!

CREATE SCANNER OBJECT 2

DO-WHILE LOOP for INPUT VALIDATION (ACCEPT ONLY POSITIVE INTEGER AND INTEGER MUST NOT BE LARGER THAN ARRAYLIST SIZE)

ACCEPT USER SELECTION

CALL ADD ITEM TO SHOPPING CART METHOD FROM SHOPPINGCART CLASS

}

ELSE

NO MATCH FOUND!

}

the return results from inventory search varies:

for example, return results:

1. mars snickers
2. mars M&Ms
3. mars caramel
0. cancel

with the above how should how can make it user selectable without using switch case statements?

You might be over-thinking this. Relax. Take it a step at a time.

To answer your question...
If you have an ArrayList containing "mars snickers", "mars M&Ms" and "mars caramel" then you might want to loop through the elements of the list and print each one. Note that "item #1" ("mars snickers") will be at index ZERO in the ArrayList.

Now at the next step, if the user enters a positive integer value between one and three (which is the length of the ArrayList), there is a way you could find the corresponding element of the ArrayList, given that integer numeric value. ;->

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.