RAW
RAW

Reputation: 7825

switch / case request with boolean

I'm trying to create a method which checks if the Login (username and password) has a minimum of 6 charakters.

To realize that I created this method public void checkLoginData(final String username, final String password). In that method, I create to booleans (user and pass), with those I can create 4 different boolean-chains:

Now I'd like to do a switch/case request for each of them, but I don't get how to realize that...

If you ask why I need the switch, I just think I need it, because I'd like to do for every of those 4 boolean-chains, that it does/show something diffrent. Also I'd like to do this in a sexy-java-way not with tousands of diffrent 'ifs' :P, Please help!

Here's the code of the method:

public void checkLoginData(final String username, final String password){

    boolean user, pass;

    if (username.length() < 6){
        user = false;
    }else {
        user = true;
    }

    if (password.length() < 6){
        pass = false;
    }else {
        pass = true;
    }

    boolean[] logindaten = {user, pass};



}

Thx for the help in Advance!

Best Regards safari

Upvotes: 18

Views: 120613

Answers (8)

user14716113
user14716113

Reputation: 1

private static boolean checkCharOf_(String userName){

  return userName.length() >= 6;

}

private static boolean checkCharOf_(String password){

  return password.length() >= 6;

}

or

private static boolean checkCharOf_And_(String userName, String password){

  return userName.length() >= 6 && password.length() >= 6;

}

Upvotes: 0

nimo23
nimo23

Reputation: 5698

With java12, you can use expressions within switch-case and provide a Bool type (https://blog.codefx.org/java/switch-expressions/).

Upvotes: 0

StickyBandit
StickyBandit

Reputation: 1

You can do something like this and then each case is 1,2 or 3, etc.

switch((route.isComplete()?1:(route.getAuthentic()?2:(route.hasRoute()?3:0)))) {...}

Upvotes: 0

Stephen C
Stephen C

Reputation: 719576

Basically there is no simpler way than this, and no way to do it in significantly less lines of code.

if (username.length() < 6){
    if (password.length() < 6){
        // do case 1
    } else {
        // do case 2
    }
} else {
    if (password.length() < 6){
        // do case 3
    } else {
        // do case 4
    }
}

To my mind, that makes this the best solution.

Also I'd like to do this in a sexy-java-way not with tousands of diffrent 'ifs'

If by "sexy-java-way" you mean "clever" or "obscure", then there are other ways to do it. But they certainly don't make the code easier to read / more maintainable.

By the way, the above involves only 3 ... that's right THREE ... if statements.


However your (final) specific example:

public void checkLoginData(final String username, final String password){
    boolean user, pass;
    if (username.length() < 6){
        user = false;
    }else {
        user = true;
    }
    if (password.length() < 6){
        pass = false;
    }else {
        pass = true;
    }
    boolean[] logindaten = {user, pass};
    ....
}

can be simplified to the following:

public void checkLoginData(final String username, final String password){
    boolean user = username.length() >= 6;
    boolean pass = password.length() >= 6;
    boolean[] logindaten = {user, pass};
    ....
}

Note that simplification is possible here because the actions (the "cases" in your hypothetical switch) can be refactored into simple boolean assignments AND the tests are actually independent of each other. In general you can't do that ...


... but id like to have it more celver to impress my boss ;)

Seriously, if I was your boss and you wrote code like that, I'd be UN- impressed. Any boss who thinks you are clever for writing obscure and unmaintainable code is clueless.

Upvotes: 8

user85421
user85421

Reputation: 29730

If you really want a "sexy-java-way" (but that depends what you understand as such) you can do something like (Java 7 required):

boolean user, pass;

switch (user + "-" + pass) {
    case "false-false":
        ...
    case "false-true":
        ...
    case "true-false":
        ...
    case "true-true":
        ...
    default:
        throw new RuntimeException(
            "something strange happening here, user: " + user + ",pass: " + pass);
}

but I would prefer to do just 2 distinct checks each with his owns message, the message being joined for presentation. (and not sure if that could be considered "sexy-java-way", more like a 'workaround')

Upvotes: 18

crusam
crusam

Reputation: 6178

Guess thats how I would solve it with enums:

public class LoginController
{
  private void login( String username, String password )
  {
    LoginState state = determineLoginState( username, password );

    switch ( state )
    {
      case LOGIN_OK:
        //Do Something
        break;
      case USERNAME_FALSE:
        //Do Something
        break;
      case PASSWORD_FALSE:
        //Do Something
        break;
      case BOTH_FALSE:
        //Do Something
        break;
    }

  }

  private LoginState determineLoginState( String username, String password )
  {
    final boolean checkUsername = checkUsername( username );
    final boolean checkPassword = checkPassword( password );

    if ( checkUsername && checkPassword )
      return LoginState.LOGIN_OK;

    if ( !checkUsername && checkPassword )
      return LoginState.USERNAME_FALSE;

    if ( checkUsername && !checkPassword )
      return LoginState.PASSWORD_FALSE;

    if ( !checkUsername && !checkPassword )
      return LoginState.BOTH_FALSE;

    throw new AuthenticationException();
  }

  protected boolean checkUsername( String username )
  {
    return username.length() > 6;
  }

  protected boolean checkPassword( String password )
  {
    return password.length() > 6;
  }

  private enum LoginState
  {
    LOGIN_OK, USERNAME_FALSE, PASSWORD_FALSE, BOTH_FALSE;
  }

  public class AuthenticationException extends RuntimeException
  {

  }
}

Upvotes: 9

king_nak
king_nak

Reputation: 11513

You can't switch over boolean[], only over integral types. To convert the booleans to an int, you could use a bit mask for the 2 booleans, like for example this:

int val = 0;
if (user) val |= 0x1;
if (pass) val |= 0x2;

switch (val) {
case 0: // Both too short
case 1: // User Ok, pass too short
case 2: // User too short, pass ok
case 3: // Both Ok
}

Upvotes: 16

Vaandu
Vaandu

Reputation: 4945

if (user) {
    if (pass) {
        // user = true, pass = true
    } else {
        // user = true, pass = false
    }
} else {
    if (pass) {
        // user = false, pass = true
    } else {
        // user = false, pass = false
    }
}

Or

int case = user ? (pass ? 1 : 2) : (pass ? 3: 4);

switch (case) {  
  case 1:
    System.out.println(" user = true, pass = true ");
    break;
  case 2:
    System.out.println(" user = true, pass = false ");
    break;
  case 3:
    System.out.println(" user = false, pass = true ");
    break;
  case 4:
    System.out.println(" user = false, pass = false ");
    break;
  }
}

Upvotes: 5

Related Questions