function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Manogna KoviManogna Kovi 

Write an apex program, to display the season name based on the month number

Best Answer chosen by Manogna Kovi
Dinesh GopalakrishnanDinesh Gopalakrishnan
Hi Manogna,

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

Dinesh GopalakrishnanDinesh Gopalakrishnan
Hi Manogna,

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


 
This was selected as the best answer
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
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>