arthur
arthur

Reputation: 27

How to write code that performs actions based on what type is

Please bear with me as I struggle through this phrasing this question. So let's say I have a user-defined type called "UserType" and that this type has 4 values: North, South, East, and West.

public enum UserType {
    NORTH, SOUTH, EAST, WEST
}

Then, I want to write a method statement in a different class that will increase the value of two variables based on the type. For example,

public class direction {
    private int peopleWhoGoNorth;
    private int peopleWhoGoSouth;

so peopleWhoGoNorth should track the number of people who go North every time the following method is called:

public void whichWay(UserType type) {
        }

I'm wondering how I would create a statement in between the brackets that would basically say: if the type is North, add 1 to peopleWhoGoNorth and if the type is South, add 1 to peopleWhoGoSouth. I'm guessing an if/else statement should do it, but I'm not sure how the syntax would work. Is the following OK?

public void whichWay (UserType type){
    if (UserType = NORTH){peopleWhoGoNorth = peopleWhoGoNorth + 1);
    if (UserType = SOUTH){peopleWhoGoSouth = peopleWhoGoSouth + 1);
    }

Please let me know if there is any way I can clarify the question.

Upvotes: 3

Views: 98

Answers (6)

kfrisbie
kfrisbie

Reputation: 896

I would say that you are on the correct track.

Consider just the method "whichWay":

public void whichWay(UserType type) {
    if(type == UserType.NORTH) { peopleWhoGoNorth++; }
    else if(type == UserType.SOUTH) { peopleWhoGoSouth++; }
}

Upvotes: 0

assylias
assylias

Reputation: 328727

One alternative would be to use an EnumMap:

private final EnumMap<UserType, Integer> counter = new EnumMap<> (UserType.class);
{
  for (UserType u : UserType.values()) counter.put(u, 0);
}

public void whichWay (UserType type){
  counter.put(type, counter.get(type) + 1);
}

Or with Java 8:

private final EnumMap<UserType, Integer> counter = new EnumMap<> (UserType.class);

public void whichWay (UserType type){
  counter.compute(type, (u, i) -> i == null ? 0 : i + 1);
}

(Note: this is not thread safe)

Upvotes: 0

Marv
Marv

Reputation: 3557

You can simply compare your type with the types you wanna check for.

public void whichWay(UserType type) {
    if (type == UserType.NORTH) {
        //Execute code
    }
    //Other types analog
}

or you can use a switch statement:

public void whichWay(UserType type) {
    switch (type) {
        case NORTH:
            //Execute code
            break;
        //Other types analog
        default:
    }
}

Upvotes: 2

Jean Logeart
Jean Logeart

Reputation: 53839

You need to use ==: if (type == NORTH).

You can also use a switch statement:

switch(type) {
  case NORTH: 
    peopleWhoGoNorth++;
    break;
  case SOUTH: 
    peopleWhoGoSouth++;
    break;
  default: // do nothing
}

Upvotes: 3

MadConan
MadConan

Reputation: 3767

switch and if-else has been covered, but you could also use a Map to contain the count of those who have gone in a given direction.

public enum UserType {
    NORTH, SOUTH, EAST, WEST
}

public class Example{
    private final Map<UserType,Integer> typeMap = new HashMap<>();

    public void whichWay(UserType type){
        int val = typeMap.getOrDefault(type,0);
        typeMap.put(type,val++);
    }
}

Of course, you would need to add methods to get those values in order to make this useful.

Upvotes: 0

dting
dting

Reputation: 39287

You can use a switch for enums:

public void whichWay (UserType type){
  switch (type) {
    case NORTH:
      peopleWhoGoNorth++;
      break;
    case SOUTH:
      peopleWhoGoSouth++;
      break;
    default:
      throw new IllegalArgumentException();
  }
}

Upvotes: 4

Related Questions