0

I have this situation:

  • a product with an SKU
  • this product has attributes (checkboxes) who have a SKU suffix
  • (imagine a product with addons)

Attributes are always in a fixed order and are not manditory. The final SKU for a product is basesku + selected attributes' suffixes.

For example a product with basesku XXX has 3 attributes with suffixes A, B and C.

Possible combinations for this product are:

XXX-A
XXX-A-B
XXX-A-B-C
XXX-A-C
XXX-B
XXX-B-C
XXX-C

Because the attributes are in a fixed order, it is never possible to have XXX-C-B for example.

What is the best way to calculate these SKU's?

Edit: I should have told these attributes are dynamically created. So it is not possible to have attA + "-" + attB. I need a way to dynamically generate the list of possible SKU's

jaap
  • 373
  • 6
  • 15
  • Just have a Public Property SKUwSuffix and build it up. – paparazzo Dec 10 '12 at 20:33
  • 1
    possible duplicate of [How can I obtain all the possible combination of a subset?](http://stackoverflow.com/questions/13765699/how-can-i-obtain-all-the-possible-combination-of-a-subset) – Gabe Dec 10 '12 at 20:34
  • yes duplicate, thanks for the comment I could not find this before – jaap Dec 10 '12 at 21:13

3 Answers3

3

Use a StringBuilder

var sb = new StringBuilder("XXX");
if (skuACheckBox.Checked) {
    sb.Append("-A");
}
if (skuBCheckBox.Checked) {
    sb.Append("-B");
}
if (skuCCheckBox.Checked) {
    sb.Append("-C");
}
string skuWithSuffix = sb.ToString();
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

I see the other answer is a count.
Sorry if I read the question wrong.

Public String SKU { get; set; }

Public bool Aattr { get: set; }
Public bool Battr { get: set; }
Public bool Cattr { get: set; }

Public String SKUwSuffix 
{
   get
   {
      string sKUwSuffix = SKU; 
      if (Aattr) sKUwSuffix += "-A";
      if (Battr) sKUwSuffix += "-B";
      if (Cattr) sKUwSuffix += "-C";
      return sKUwSuffix;
   }
}
paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

I would recommend to maintain a non duplicate selected suffix attribute collection and finally concatenate the sku by using the collection.

humblelistener
  • 1,456
  • 12
  • 22