tags:

views:

253

answers:

4

Friends

How do I implement following complex logic?

flag1 can be "N" or "A" or "I"
flag2 can be "N" or "A" or "I"
flag3 can be "N" or "A" or "I"

function (string flag1, string flag2, string flag3) begin

The function needs to return:

  1. return "None" if flag1, flag2 and flag3 are "N"

  2. else return "Active" if flag1, flag2 and flag3 are "A"

  3. else return "Inactive" if flag1, flag2 and flag3 are "I"

  4. else return "both" if flag1, flag2 and flag3 are either "A" AND "I" (OR "N")

e.g. 1) flag1 is "A" and flag2 is "I" and flag3 is "I"
e.g. 2) flag1 is "I" and flag2 is "A" and flag3 is "I"
e.g. 2) flag1 is "A" and flag2 is "N" and flag3 is "I"

retrun result

end

Thanks for reply but none of post gives answer. I know if else constrauct and looking for logic to implement above psedocode. All four are possibel conditions specially #4 is complex and need to know how to implement that.

+19  A: 

Your logic for point 4 is confusing...

I would use an enum value for this, rather than strings - it is much more type-safe (eg what if someone passed "WIBBLEWOBBLE" to your method? What should it return?)

enum Value { None, Active, Inactive, Both }

private Value GetValue(Value flag1, Value flag2, Value flag3) {
    if (flag1 == flag2 && flag2 == flag3)    // they are all the same
        return flag1;
    else return Value.Both;    // there is a difference
}
thecoop
+1 for enums...and 3 lines soln to a bloated Q :)
Misnomer
+1 for the simple single if/else
rlb.usa
get rid of the else, you already returned if the if was true. Otherwise +1 for being able to figure out the arcane requirements posted.. Assuming your intepretation is accurate..
Jimmy Hoffa
Ay, the `else` is superfluous, but it does make it obvious what the logic is
thecoop
As a note, the `enum` makes this more type safe but it would still be possible to send in invalid inputs. You can cast any integral value to a particular `enum` type. (I would have suggested the `enum` as well. I just don't want people getting a false sense of security.)
Matthew Whited
4.else return "both" if flag1, flag2 and flag3 are either "A" AND "I" (OR "N")-- actually I am looking implementation for this logic. It means both "A" and "I" must be there in flag1, flag2 and flag3 and remaning flag can be "N" or can be "A" and "I". please see my examples for #4
Kumar
Hopefully he will flip this from me to you. I tried to delete my answer but it won't let me because it's accepted.
Matthew Whited
Thank you very much! Perfectly working as expected.
Kumar
there... all fixed now :o)
Matthew Whited
A: 

This would* work. Can't say I like it.

 string key = flag1 + flag2 + flag3;
 switch(key){
     case "NNN":
         return "None";
      case "AAA":
        return "Active";
      case "III":
        return "Inactive";
      default:
        break;
  }
  // ;)
 //Trying to make it as confusing as your requirement #4
  var four = (
     from s in key
     select s
   ).Distinct();

  if(four.Count() > 1){
    return "Both";
  }



 }
Nix
I welcome the negative. Because I know its bad ;)
Nix
You justified the bad code, there's no reason you should be voted down here.
Loren Pechtel
+2  A: 
[Flags]
enum SomeWierdReturn
{ Both = 0, None = 1, Active = 2, Inactive = 4 }

public SomeWierdReturn DoSomething(SomeWierdReturn flag1, SomeWierdReturn flag2, SomeWierdReturn flag3)
{
    return (SomeWierdReturn)(flag1 & flag2 & flag3);
}
Jimmy Hoffa
A: 

Robaticus gave you the right answer but in a comment rather than a post so I will expand upon it.

We have three flags that can take each of three states. There are thus 3 * 3 * 3 = 27 possible options.

When facing complex if..then logic and such a small address space it makes no sense at all to try to code all the if..then's. Instead, one three-dimensional array holding a total of 27 elements.

const int None = 0; const int Inactive = 1; const int Active = 2;

private int ParseFlag(string Flag) { switch (Flag) { case "N": return None; case "I": return Inactive; case "A": return Active; default throw new Exception(string.Format("Got a flag value of {0} but expected N, I or A", Flag)); } }

public FlagResult Lookup(string Flag1, string Flag2, string Flag3) { return FlagData[ParseFlag(Flag1), ParseFlag(Flag2), ParseFlag(Flag3)]; }

I'll let you build the array.

Loren Pechtel