Learning The "J"

Home » JAVA Learning » JAVA EE » The MVC » Understanding MVC

Understanding MVC


//MODEL
package com.awais.mvc.model;

public class Calculator {
//add numbers. 
    public int add(inta, intb) {
        return a + b;
    }
// multiply numbers
    public int multiply(inta, intb) {
        return a * b;
    }
}

//CONTROLLER
package com.awais.mvc.controller;

import com.awais.mvc.model.Calculator;

public class CalculatorController {

    private Calculator calculator = new Calculator();
    private intfirstNumber  = 0;
    private intresult  = 0;
    private intsecondNumber  = 0;

    public CalculatorController() {
        super();
    }

    public void setCalculator(CalculatoraCalculator) {
        this.calculator = aCalculator;
    }

    public void setFirstNumber(intaFirstNumber) {
        this.firstNumber = aFirstNumber;
    }

    public int getFirstNumber() {
        return firstNumber;
    }

    public int getResult() {
        return result;
    }

    public void setSecondNumber(intaSecondNumber) {
        this.secondNumber = aSecondNumber;
    }

    public intgetSecondNumber() {
        return secondNumber;
    }

    public String add() {
        result = calculator.add(firstNumber, secondNumber);
        return "success";
    }

    public String multiply() {
        result = calculator.multiply(firstNumber, secondNumber);
        return "success";
    }
}

//VIEW
package com.awais.mvc.view;

import com.awais.mvc.model.CalculatorController

public class MyCalculator{
    public static void main(String ar[]){
        CalculatorController myCal=new CalculatorController();
        myCal.setFirstNumber(10);
        myCal.setSecondNumber(20);
        if (myCal.add().equalsIgnoreCase("success")){
            System.out.println(myCal.getResult());
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Start here

%d bloggers like this: