Refactoring code using Switch case