This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using enum for characteristics of objects  RSS feed

 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I have the following situation. Objects from several distinct classes have a characteristic, the city the objects live in. So, there are several classes:
Employees, customers,suppliers, etc. who all have the property placeOfResidency (or just city). Each city will have at most 4 extra variables (per capita income, average household size, etc.).

I thought it would be a good idea to model these cities in an Enum. The amount of cities will be limited (I will not use larger sets of cities than a size of 20 cities).
I have tried to work with Enum, but do not fully understand how to use it and what the capabilities are.

My questions are:
Is it a good idea to model these cities as Enum?
If so, how will I be able to assign a certain city to an employee (object of another class)?

What my very basic Enum-class currently looks like:



 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sander Hoovenaar wrote:I thought it would be a good idea to model these cities in an Enum. The amount of cities will be limited (I will not use larger sets of cities than a size of 20 cities).

The main problem I would see with that is that it's not very scaleable. Specifically: what if later on you decide that you do want to have more than 20? You then have to change a source file to allow any new cities, whereas if you stored the data in a file, you could simply load it at start-up.

I do think that City should be an object; I'm just not sure that an enum is the best idea.

Unfortunately, there doesn't yet seem to be an ISO standard for "city codes", but if your cities are all fairly large, you could use the IATA code for its main airport (for example "LHR" for London, "AMS" for Amsterdam or "JFK" for New York), which you could then use as a key to a Map<String, City>.

HIH

Winston
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Sander Hoovenaar wrote:I thought it would be a good idea to model these cities in an Enum. The amount of cities will be limited (I will not use larger sets of cities than a size of 20 cities).

The main problem I would see with that is that it's not very scaleable. Specifically: what if later on you decide that you do want to have more than 20? You then have to change a source file to allow any new cities, whereas if you stored the data in a file, you could simply load it at start-up...

(...) which you could then use as a key to a Map<String, City>.



The map would then be an arrayList (since you use <>)?
As I mentioned above, my model will certainly not consist of more than 20 cities, and none of the cities I'll put in now, will change. The reason I looked at enum is to make sure that input is limited to the list of predetermined cities. Would that be possible in other methods too?
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sander Hoovenaar wrote:The map would then be an arrayList (since you use <>)?

No, a map is a map and a list is a list. The <> implies that the data type is generic, not a list. You read the <> as "of", so a List<City> is read as "list of cities", and Map<String, City> is read as "map of strings to cities".

As I mentioned above, my model will certainly not consist of more than 20 cities, and none of the cities I'll put in now, will change. The reason I looked at enum is to make sure that input is limited to the list of predetermined cities. Would that be possible in other methods too?

Few requirements are ever really certain. Maybe your project will consist of no more than 20 cities now, but is someone going to pick it up after you're done? If this is just an experimental project or an assignment that will be thrown away after you get your mark, using an enum is a nice quick (and clean!) solution to the problem. However, as Winston stated, enums don't scale well, and if there's any chance that the project will get reused later, I wouldn't use an enum.

Enums are constant. You could envision the classic colors of the rainbow as enum constants, or the planets of the solar system (the fact that astronomers can't agree on some of them doesn't undo that their existence considering the time scales involved, is in essence constant).

The household size of Berlin will change with the next census. Berlin is not a constant.

I recommend that you model your cities with an immutable class, and instantiate them with values loaded from a resource (see Class.getResourceAsStream()).

A final remark: Enums, like classes, should have a singular name. If you go down the enum route, name it City.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sander Hoovenaar wrote:The reason I looked at enum is to make sure that input is limited to the list of predetermined cities. Would that be possible in other methods too?

Sure. If you put the 20 cities in a file - or, as Stephan says, a resource - and then load them, the only cities that will be "valid", are the ones you load. As he also mentioned, putting them in a file allows you to update information about a city, even if you don't add any new ones.

Winston
 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does .getResourceAsStream do different than other readers (FileReader, BufferedReader)? Can I just use a txt-file to this .getResourceAsStream?
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is that resources are like fixed data that is bundled with your application. If you want to allow a user of your application to change the configuration, you would use a file reader instead.

For instance, if your application used an image, or a default configuration, or something else without which the application wouldn't work correctly, you would bundle it as a resource.

In this particular case, either way is fine.

 
Ronald Hoovenaar
Ranch Hand
Posts: 53
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay. So with both a normal filereader and the 'resource-reader' I can put in a text-file? To me, the resource-method seems bette, since I do not want to allow for different inputs for the same city (for instance simple input errors).

In conclusion:
I can just make a class called City, with objects and their characteristics coming from a file (txt?). I presume that this data needs to be transferred into an Arraylist.

Then, my last question is: How can I assign these cities to employees, customers and suppliers?
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have a City instance variable in the class of customerA, you would create the City object and set to instance variable to it.

Does that answer your question?
 
Stephan van Hulst
Saloon Keeper
Posts: 7806
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Design a file format and then write a file that contains the data for your cities. You can give your City class a static method Map<String, City> getAvailableCities(), where the String represents an identifier by which you can retrieve a city. Winston already proposed codes such as "AMS" and "JFK". getAvailableCities() is responsible for loading the data and instantiating the City objects.

How you assign cities to customers depends on how you create your customer instances. If you want predefined customers, you can make another resource that describes your customers, which references the city a customer resides in using the identifier string. If you want to create customers dynamically, the user will have to enter the city identifier through the keyboard.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!