0

I have to perform basic math operations Addition, Subtraction, Multiplication and division after selecting one of them from SelectList and show the result on same VF page on label.... Please help me with simplest code.....

Thanks in advance !!!

1
  • 7
    Have you written any code? Can you post whatever you have done? Commented Jan 14, 2014 at 6:26

2 Answers 2

4

Controller

public class calculatorController {
    interface operation {
        decimal execute(decimal op1, decimal op2);
    }
    class Add implements operation {
        public decimal execute(decimal op1, decimal op2) { return op1 + op2; }
    }
    class Subtract implements operation {
        public decimal execute(decimal op1, decimal op2) { return op1 - op2; }
    }
    class Multiply implements operation {
        public decimal execute(decimal op1, decimal op2) { return op1 * op2; }
    }
    class Divide implements operation {
        public decimal execute(decimal op1, decimal op2) { return op1 / op2; }
    }
    public selectoption[] getoperators() {
        return new SelectOption[] {
            new SelectOption('add', '+'),
            new SelectOption('sub', '-'),
            new SelectOption('mul', '*'),
            new SelectOption('div', '/')
        };
    }
    public decimal op1 { get; set; }
    public decimal op2 { get; set; }
    public string op { get; set; }
    public decimal result { get; set; }

    public void calculate() {
        map<string, type> ops = new map<string, type> {
            'add' => add.class,
            'sub' => subtract.class,
            'mul' => multiply.class,
            'div' => divide.class
        };
        try {
            operation o = (operation)ops.get(op).newinstance();
            result = o.execute(op1, op2);
        } catch(exception e) {
            result = null;
            apexpages.addmessages(e);
        }
    }
}

Page

<apex:page docType="html-5.0" controller="calculatorController">
    <apex:form >
        <apex:input type="number" value="{!op1}"/>
        <apex:selectList value="{!op}">
            <apex:selectOptions value="{!operators}"/>
        </apex:selectList>
        <apex:input type="number" value="{!op2}"/>
        <apex:commandButton action="{!calculate}" value="Calculate"/>
        <br/>
        Result: <apex:outputText >{!result}</apex:outputText>
        <br/>
        <apex:pageMessages />
    </apex:form>
</apex:page>

This is just one possible method, of course.

2

Here's a simple example of how to use basic math in a Salesforce VisualForce Page:

<apex:variable value="{!Custom_Object__c.Number_1__c}" var="num1"/>
<apex:variable value="{!Custom_Object__c.Number_2__c}" var="num2"/>

<!--ADD-->
<apex:variable value="{!num1+num2}" var="sum"/>

<!--SUBTRACT-->
<apex:variable value="{!num2-num1}" var="difference"/>

<!--MULTIPLY-->
<apex:variable value="{!num1*num2}" var="product"/>

<!--DIVIDE-->
<apex:variable value="{!num2/num1}" var="quotient"/>

Not the answer you're looking for? Browse other questions tagged .