5 Replies - 521 Views - Last Post: 29 April 2016 - 06:37 AM Rate Topic: -----

#1 casta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-April 16

Giving nationalities to objects from different classes

Posted 29 April 2016 - 04:34 AM

Hi guys, I have a question concerning assigning nationalities to objects from different classes. I thought it would be a good idea to give nationality a separate class. I would like to limit the possibilities in choosing the nationality (only European countries).

example:
instances from the class Trainer have a nationality
instances from the class Player have a nationality
instances from the class Referee have a nationality.

How can I model this in such a way that all refer to the same list of nationalities and how can I assure that no mistakes are being made. So that there is only one input for Belgium, instead of Belgium (correct), belgium (lower case), Belgim (input error). Is it wise to make a class named Nationality?

Hope you understand my question and you can help me.

Is This A Good Question/Topic? 0
  • +

Replies To: Giving nationalities to objects from different classes

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3536
  • View blog
  • Posts: 16,039
  • Joined: 20-September 08

Re: Giving nationalities to objects from different classes

Posted 29 April 2016 - 04:44 AM

Quote

Is it wise to make a class named Nationality?
Certainly. It can contain various attributes, such as name and country code. Of course, a related class is java.util.Locale, but that of course is a kind of 'superset' of nationality
Was This Post Helpful? 0
  • +
  • -

#3 casta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-April 16

Re: Giving nationalities to objects from different classes

Posted 29 April 2016 - 04:50 AM

Good to hear that making a separate class for nationality is wise. I prefer to make a class myself instead of using Locale. What method could I use to:
- limit the input possibilities? (as I mentioned, Belgium only, disallowing belgium, belgim, etc.)
- Assign these nationalities to objects of other classes
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3536
  • View blog
  • Posts: 16,039
  • Joined: 20-September 08

Re: Giving nationalities to objects from different classes

Posted 29 April 2016 - 06:04 AM

You could make the country name of type enum. Nationality could even be enum

For example:

trainer.setNationality(Nationality.Belgium);

This post has been edited by g00se: 29 April 2016 - 06:07 AM
Reason for edit:: Clarification

Was This Post Helpful? 0
  • +
  • -

#5 casta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-April 16

Re: Giving nationalities to objects from different classes

Posted 29 April 2016 - 06:29 AM

I have read some stuff now about Enum, but some things are unclear. The code you gave me seems already very useful, but what I would like to know is:
- What are the nationalities when I use Enum? Clarification: Are the nationalities object of the class Nationality now? Or objects of a class Enum? Or even something else?
- Would it be possible to assign instance variables to each nationality?
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3536
  • View blog
  • Posts: 16,039
  • Joined: 20-September 08

Re: Giving nationalities to objects from different classes

Posted 29 April 2016 - 06:37 AM

enum is a special kind of class. So the following would mean that Nationality is the class but that class is an enum

public enum Nationality {
    France,
    Belgium,
    Netherlands,
    Luxembourg;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1