Loen10
Loen10

Reputation: 71

How can I avoid duplicate switch statements

I have an enum of phone conditions. At one point in my application I need to update a price based on the condition of the phone, e.g.

switch (condition)
{
    case Condition.Good:
        price = goodPrice;
        break;
    case Condition.ScreenCrack:
        price = screenCrackPrice;
        break;
    case Condition.CameraCrack:
        price = cameraCrackPrice;
        break;
    case Condition.BadBattery:
        price = badBatteryPrice;
        break;
    case Condition.ScreenCrack | Condition.CameraCrack:
        price = screenCrackCameraCrackPrice;
        break;
    case Condition.ScreenCrack | Condition.BadBattery:
        price = screenCrackBadBatteryPrice;
        break;
    case Condition.CameraCrack | Condition.BadBattery:
        price = cameraCrackBadBatteryPrice;
        break;
case Condition.ScreenCrack | Condition.CameraCrack | Condition.BadBattery:
        price = screenCrackCameraCrackBadBatteryPrice;
        break;
}

Later on, the user may want to edit the price associated with that same condition. How can I do this without making another switch statement?

Upvotes: 1

Views: 119

Answers (1)

Origin
Origin

Reputation: 2023

Why not use an object with multiple properties, like Name and Price? Then it's just price += condition.Price, and then when you want to change the price you only change it in one place. Eventually, this could be saved in a DB instead of hard-coded objects.

Upvotes: 3

Related Questions