GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Creating an Array or list from an Enum Category for Restaraunt category sorting. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating an Array or list from an Enum Category for Restaraunt category sorting." Watch "Creating an Array or list from an Enum Category for Restaraunt category sorting." New topic
Author

Creating an Array or list from an Enum Category for Restaraunt category sorting.

Jeremy Packer
Greenhorn

Joined: Jul 28, 2012
Posts: 7
I'm not sure which would be the best route to take in this sittuation, i've been looking up both options, but I want to be able to group each category in a list so i am able to pull a random value out of it for a randomizing food program. This is what I have so far.

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
The generic List<T> requires a type (The 'T'). Class, Interface and Enums are all types but an Enum constant (here PIZZA, etc.) are not.

List<Restaraunt> seems to be correct if you need to add all the Restaraunts into the List. Please elaborate your further sorting requirement.
Jeremy Packer
Greenhorn

Joined: Jul 28, 2012
Posts: 7
Hi John,

What I'm trying to do in the end, is for example, if the Pizza category, and the mexican category are both selected on the GUI, then there will be an array/list that is created with the restaurants of those types and will be added together so I can randomly pull out a restaurant and print it from the selected types.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
A way of doing this is to add all the available restaurants to a List<Restaurant> and iterate each restaurant and check if it's category matches the selected category. If yes, then add those restaurants to another List of selected restaurants.

Cosmetic correction - type names shall start with upper case, hence call it Category for enum. And variables shall start with smaller case.
Jeremy Packer
Greenhorn

Joined: Jul 28, 2012
Posts: 7
Could you possibly give me an example with what your talking about? I'm still really new to using enum, and the list's. Just so I have the logic down on it, I would add all restaurants to a huge list, and then if statements for each category confirming them?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
A sample code would be -



Let me think if I can get another possible way of doing it.
Jeremy Packer
Greenhorn

Joined: Jul 28, 2012
Posts: 7
Okay John, I see what this if statement is doing, but since I don't know the categories the user will use, how can i set this up to where it is based on the selected ones?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
John Jai wrote:Let me think if I can get another possible way of doing it.

Another way is to add restaurants for the categories, and simply get the List on the selected category.

This would be the Enum for the categories.



You can add the restaurants to specific categories after you create them.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Jeremy Packer wrote: but since I don't know the categories the user will use, how can i set this up to where it is based on the selected ones?

Sorry, I don't get you. You mean you won't know which category is selected by the user? Then I fear how will you ever show the restaurant's on user's choice?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39053
    
  23
More complicated solution:
Use a Map<RestaurantCategory, List<Restaurant>>. Assuming each Restaurant has a RestaurantCategory field, you can get that, and use it as the left half (“key”) in a Map. You use a List<Restaurant> as the right half (“Value”).
There is a slightly different example in one of the Map sections of the Java Tutorials. It counts occurrences, but if you substitute adding to a List for incrementing the count, you should be able to work out how to do it.
I think you ought not to have the List of restaurants as a field of the category class. I think it belongs in a class of its own. [edit]I think the last line is mistaken, if you have the list as a field of the category object.. Ignore it[/edit]
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

John Jai wrote:Let me think if I can get another possible way of doing it...

Just one off the top of my head would be to use an EnumSet, viz:@Jeremy: I've replied to your other Thread on this subject too. My suggestion would be to stick to one.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
GeeCON Prague 2014
 
subject: Creating an Array or list from an Enum Category for Restaraunt category sorting.