This version is very easy to write the test class for... <pre> public static String getSeason( Date dt ) { return new List<String> { 'Winter', 'Spring', 'Summer', 'Summer', 'Summer', 'Monsoon' , 'Monsoon', 'Monsoon', 'Monsoon', 'Autumn', 'Autumn', 'Winter' }[ dt.month() - 1 ]; } </pre>
I Hope the Below Code will Fit your Requirement.Let me Know if you face any Issues Regarding this.
/**************************************Apex Class****************************************/
public class SwitchExample {
public Integer month { get; set; }
public String season{get;set;}
public void OutputString()
{
if(month>0 && month<=12)
{
if(month==2)
{
season = 'Spring';
}
else if(month>2 && month<=5)
{
season = 'Summer';
}
else if(month>5 && month<=9)
{
season = 'Monsoon';
}
else if(month>9 && month<=11)
{
season = 'Autumn';
}
else
{
season = 'Winter';
}
}
else
{
season = 'Invalid Month';
}
}
}
/**************************************Visual Forece Page****************************************/
<apex:page controller="SwitchExample">
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Get Season" action="{!OutputString}" />
</apex:pageBlockButtons>
<apex:pageBlockSection >
<label>Enter Month</label>
<apex:inputText value="{!month}"/>
<apex:outputText value="{!season}"></apex:outputText>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Kindly Mark this as a Best Answer if you Find this Useful!
Thanks
DineshKumar Gopalakrishnan
All Answers
I Hope the Below Code will Fit your Requirement.Let me Know if you face any Issues Regarding this.
/**************************************Apex Class****************************************/
public class SwitchExample {
public Integer month { get; set; }
public String season{get;set;}
public void OutputString()
{
if(month>0 && month<=12)
{
if(month==2)
{
season = 'Spring';
}
else if(month>2 && month<=5)
{
season = 'Summer';
}
else if(month>5 && month<=9)
{
season = 'Monsoon';
}
else if(month>9 && month<=11)
{
season = 'Autumn';
}
else
{
season = 'Winter';
}
}
else
{
season = 'Invalid Month';
}
}
}
/**************************************Visual Forece Page****************************************/
<apex:page controller="SwitchExample">
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Get Season" action="{!OutputString}" />
</apex:pageBlockButtons>
<apex:pageBlockSection >
<label>Enter Month</label>
<apex:inputText value="{!month}"/>
<apex:outputText value="{!season}"></apex:outputText>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Kindly Mark this as a Best Answer if you Find this Useful!
Thanks
DineshKumar Gopalakrishnan
<pre>
public static String getSeason( Date dt )
{
return new List<String>
{ 'Winter', 'Spring', 'Summer', 'Summer', 'Summer', 'Monsoon'
, 'Monsoon', 'Monsoon', 'Monsoon', 'Autumn', 'Autumn', 'Winter'
}[ dt.month() - 1 ];
}
</pre>