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.
the return results from inventory search varies:
for example, return results:
1. mars snickers
2. mars M&Ms
3. mars caramel
with the above list how can I make it user selectable without switch case statements? I think I cannot use switch case statement in this scenario
It looks to me that you have far too many things going on in that one method. A method should do one thing and one only, as far as possible.
You should have a Cart object already somewhere, and should be adding an Item. So you want a getItem method, which you call. That should give you an Item (or a PurchaseLine, which includes Item and count, so you can buy 2 Mars Bars). You then add the Item/PurchaseLine to the Cart with its addItem method.
You ought to have a toString method in the Item class, and you can easily print those details. You can also print a stock number. It is very easy to iterate a List and print out its contents, but it is hard to read if the List is large. You can easily print a number against each element, change that number to the index in the List (you might only need to subtract 1), and find the element from its index. Finding something in an ArrayList from its index is very easy and very fast.