jeudi 22 décembre 2016

Refactor multiple if statements?

I need to send a different HTTP message depending on the port number that I have. The code that I have now works but i would like to refactor it so it looks cleaner, less repetitive. Any tips are appreciated.

  if(portNumber == 52164)
  {
     json.put("reader0", 1);
     json.put("reader1", 0);
     json.put("reader2", 0);
     json.put("reader3", 0);
  }
  else if(portNumber == 52165)
  {
     json.put("reader0", 0);
     json.put("reader1", 1);
     json.put("reader2", 0);
     json.put("reader3", 0);
  }
  else if(portNumber == 52166)
  {
     json.put("reader0", 0);
     json.put("reader1", 0);
     json.put("reader2", 1);
     json.put("reader3", 0);
  }
  else if(portNumber == 52167)
  {
     json.put("reader0", 0);
     json.put("reader1", 0);
     json.put("reader2", 0);
     json.put("reader3", 1);
  }

Aucun commentaire:

Enregistrer un commentaire