com.swcguild.luckysevensmvc.TipCalculatorController.java Source code

Java tutorial

Introduction

Here is the source code for com.swcguild.luckysevensmvc.TipCalculatorController.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.swcguild.luckysevensmvc;

import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 *
 * @author Christopher Becker <beckerc@umich.edu>
 */
@Controller
public class TipCalculatorController {

    @RequestMapping(value = "displayTipCalculatorForm", method = RequestMethod.GET)
    public String displayTipCalculatorForm() {
        return "tipCalculatorForm";
    }
    //value is the name of the form from ...Form.jsp
    //getParameter is the name of the input field  

    @RequestMapping(value = "payCheck", method = RequestMethod.POST)
    public String payCheck(HttpServletRequest req, Model model) {

        String billAmountString = req.getParameter("billAmount");
        String perCentTipString = req.getParameter("perCentTip");

        Double billAmountDouble = Double.parseDouble(billAmountString);
        Double perCentTipDouble = Double.parseDouble(perCentTipString);

        Double totalTip = perCentTipDouble / 100 * billAmountDouble;

        Double totalBill = billAmountDouble + totalTip;

        model.addAttribute("totalTip", totalTip);
        model.addAttribute("totalBill", totalBill);
        return "tipCalculatorResult";
    }

}