Convert if else statement to a Switch statement

106 Views Asked by At

I was responding to a challenge to write C# code taking a if else statement and converting it to a Select statement with all the same outcome. Here is the if else code;

// SKU = Stock Keeping Unit. 
// SKU value format: <product #>-<2-letter color code>-<size code>
string sku = "01-MN-L";

string[] product = sku.Split('-');

string type = "";
string color = "";
string size = "";

if (product[0] == "01")
{
    type = "Sweat shirt";
} else if (product[0] == "02")
{
    type = "T-Shirt";
} else if (product[0] == "03")
{
    type = "Sweat pants";
}
else
{
    type = "Other";
}

if (product[1] == "BL")
{
    color = "Black";
} else if (product[1] == "MN")
{
    color = "Maroon";
} else
{
    color = "White";
}

if (product[2] == "S")
{
    size = "Small";
} else if (product[2] == "M")
{
    size = "Medium";
} else if (product[2] == "L")
{
    size = "Large";
} else
{
    size = "One Size Fits All";
}

Console.WriteLine($"Product: {size} {color} {type}");

This is my attempt at the problem and if works with the exception, that I can't handle the else part of the code which would require more than one default: statement in the Switch statement.

using System.Drawing;
string color = "";
string type = "";
string size = "";
string sku = "02-BK-R";

string[] product = sku.Split('-');
int skuPart = product.Length;

foreach (string productPart in product)
{
    switch (productPart)
    {
        case "01":
            type = "Sweat Shirt";
            break;
        case "02":
            type = "T-Shirt ";
            break;
        case "03":
            type = "Sweat Pants";
            break;       
        case "MN":
            color = "Maroon";
            break;
        case "BK":
            color = "Black";
            break;

        case "S":
            size = "Small";
            break;
        case "M":
            size = "Medium";
            break;
        case "L":
            size = "Large";
            break;
        default:           
            break;
    }
}

 Console.WriteLine($"Product: {size} {color} {type}");
2

There are 2 best solutions below

2
Behzad Dara On

In my opinion, the best solution is to write 3 switch statement for set values of type, color and size. this is how you can write your code:

var sku = "01-MN-L";

var product = sku.Split('-');

var type = product[0] switch
{
    "01" => "Sweat shirt",
    "02" => "T-Shirt",
    "03" => "Sweat pants",
    _ => "Other"
};

var color = product[1] switch
{
    "BL" => "Black",
    "MN" => "Maroon",
    _ => "White"
};

var size = product[2] switch
{
    "S" => "Small",
    "M" => "Medium",
    "L" => "Large",
    _ => "One Size Fits All"
};

Console.WriteLine($"Product: {size} {color} {type}");

I hope this solution can help you.

3
Rayzel On
string sku = "01-MN-L";

string[] product = sku.Split('-');

string type = "";
string color = "";
string size = "";

switch (product[0])
{
    case "01":
        type = "Sweat shirt";
        break;
    case "02":
        type = "T-Shirt";
        break;
    case "03":
        type = "Sweat pants";
        break;
    default:
        type = "Other";
}

switch (product[1])
{
    case "BL":
        color = "Black";
        break;
    case "MN":
        color = "Maroon";
        break;
    default:
        color = "White";
}

switch (product[2])
{
    case "S":
        size = "Small";
        break;
    case "M":
        size = "Medium";
        break;
    case "L":
        size = "Large";
        break;
    default:
        size = "One Size Fits All";
}

Console.WriteLine($"Product: {size} {color} {type}");