ShowAll Questionssorted byDate Posted
Nonick

# "Case"/"Swith" or "OR" operator for weekday calculator. (help me build first app)

Hi all,
Learning visual force at the moment my making a small applet which would allow you to enter two dates and it outputs the the total amount of week days.

Now remember this is made from scratch on a first go, and even then still a pitiful attempt but here is my page:

main
Code: Page
`<apex:page controller="dateCalculator">    <apex:form >        <apex:pageBlock title="Date Calculator">        <p>Start Date: <apex:inputField id="StartDate" value="{!o.EngagementStartDate__c}" />:</p>        <p>Close Date: <apex:inputField id="CloseDate" value="{!o.EngagementCloseDate__c}" />:</p>        <apex:commandButton action="{!calculate}" rerender="calculation" value="Go"/>        <apex:outputPanel id="calculation">        <p><apex:outputLabel title="Calculated Amount: " id="lbCalc" value="Output hopefully here" /></p>        </apex:outputPanel>        </apex:pageBlock>    </apex:form> </apex:page>`

controller:
Code:
public class dateCalculator {

private Date startDate;
private Date closeDate;
private Opportunity o = new Opportunity();
private Integer result;
private Integer formula1;
private Integer formula2;

public Date getStartDate(){
return this.startDate;
}
public void setStartDate(Date s){
this.startDate = s;
}
public Date getCloseDate(){
return this.closeDate;
}
public void setCloseDate(Date s){
this.startDate = s;
}
public Integer calculateFormula(){
Date sundayDate = date.newInstance(1985, 6, 24);
formula1 = closeDate.daysBetween(startDate) + 1;
formula2 = math.mod(2, 5);
//        CASE(MOD(sundayDate.daysBetween(startDate), 7) , 0
//        , CASE( MOD( closeDate - startDate, 7 ),1,0,2,0,3,0,4,0,5,1,6,2,0 ), 1
//        , CASE( MOD( closeDate - startDate, 7 ),0,0,1,0,2,0,3,0,4,0,5,2,2 ), 2
//        , CASE( MOD( closeDate - startDate, 7 ),0,0,1,0,2,0,3,1,2), 3
//        , CASE( MOD( closeDate - startDate, 7 ),0,0,1,0,2,1,2), 4
//        , CASE( MOD( closeDate - startDate, 7 ),0,0,1,1,2), 5
//        , CASE( MOD( closeDate - startDate, 7 ),0,1,2), 6
//        , CASE( MOD( closeDate - startDate, 7 ),6,2,1)
//        , 666 )
//        +
//        ( FLOOR( ( closeDate - startDate ) / 7 ) * 2 );

return (formula2 - formula1);
}

public PageReference calculate() {
return null;
}
public Opportunity getO() {
return this.o;
}

}

the formula is mostly stolen from a different message, the problem is that apparently there's no 'case' operator in apex. What's the work around for this? I was thinking of using a combination of "if/else" and "or" but can't find the OR operator either. Any suggestions?

Also, what would be the best method to display the result, plan above was to change the value of the label and re-render page, any guidance appreciated.

Message Edited by Nonick on 09-21-2008 10:56 PM
mikef
An or statement is;
if(x > y || z > y){
//do something;
}

the double pipe is an or, also if you want and it's &&.

This is explained in the Apex docs, http://www.salesforce.com/us/developer/docs/apexcode/index.htm
under the section 'Understanding Expression Operators'.
Nonick
yup, familiar with the OR command, I'm looking for an OR function though: give you and example. Say I only wanted to represent the first case:

in a formula it would look like
CASE( MOD( closeDate - startDate, 7 ),1,0,2,0,3,0,4,0,5,1,6,2,0 ), 1

in current apex that would look like:
Code:
if(math.mod(closeDate.daysBetween(startDate) == 1 || closeDate.daysBetween(startDate) == 0 || closeDate.daysBetween(startDate) == 2 || closeDate.daysBetween(startDate) == 0 || closeDate.daysBetween(startDate) == 3 || closeDate.daysBetween(startDate) == 0 || closeDate.daysBetween(startDate) == 4 || closeDate.daysBetween(startDate) == 0 || closeDate.daysBetween(startDate) == 5 || closeDate.daysBetween(startDate) == 1 || closeDate.daysBetween(startDate) == 6 || closeDate.daysBetween(startDate) == 2 || closeDate.daysBetween(startDate) == 0){
formula2=1;
//hands up if this is the ugliest expression you've ever seen.
}
Else if(
//repeat for another 6 if statements.

Was wondering if there's a cleaner way of doing this.

Also what do you reccomend as the best way to display such info (not load into any object, simply display on screen?

Message Edited by Nonick on 09-23-2008 04:07 PM

Message Edited by Nonick on 09-23-2008 04:08 PM