利用javabean实现一个超级简单的在线计算器

package javaBean;
import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 
 * 功能:实现javaBean中的一个计算类。
 */
public class Calculate {
	private String firstName;//第一个参数
	private String secondName;//第二个参数
	private BigDecimal result;//计算结果
	private String operation;//运算符
	
	//构造方法
	public Calculate(String fn,String sn,String op){
		this.firstName = fn;
		this.operation = op;
		this.secondName = sn;
	}
	
	/*
	 * set和get方法
	 */
	public String getFirstName() {
		return firstName;
	}
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}
	public String getSecondName() {
		return secondName;
	}
	public void setSecondName(String secondName) {
		this.secondName = secondName;
	}
	public BigDecimal getResult() {
		this.result = calculate();
		return result;
	}
	public String getOperation() {
		return operation;
	}
	public void setOperation(String operation) {
		this.operation = operation;
	}
	
	//计算结果的方法
	public BigDecimal calculate(){
		 if(this.operation.endsWith("+")){
			 return (new BigDecimal(this.firstName)).add(new BigDecimal(this.secondName));
		 }
		 else if(this.operation.endsWith("-")){
			 return (new BigDecimal(this.firstName)).subtract(new BigDecimal(this.secondName));
		 }
		 else if(this.operation.endsWith("*")){
			 return (new BigDecimal(this.firstName)).multiply(new BigDecimal(this.secondName));
		 }
		 else return new BigDecimal(this.firstName).divide(new BigDecimal(this.secondName),5, RoundingMode.HALF_UP);
	}
}

package severlet;

import java.io.IOException;
import java.io.PrintWriter;
import javaBean.Calculate;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DealWith extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;utf-8");
		
		//获取jsp中计算器的每一个参数
		String firstValue = request.getParameter("firstNumber");
		String secondValue = request.getParameter("secondNumber");
		String operatorValue = request.getParameter("operator");
		
		//把参数封装到一个Calculate对象中
		Calculate c  = new Calculate(firstValue,secondValue,operatorValue);
		
		//把该对象保存到request域对象中
		request.setAttribute("calculate",c);
		
		//使用转发技术跳转到计算页面
		request.getRequestDispatcher("/result.jsp").forward(request,response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		this.doGet(request, response);
	}
}

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <head>
    <title>calculator.html</title>  
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
  	<table>
  		<tr>
          <td>计算结果是:</td>
          <td><input type="text"></td>
        </tr>
  	</table>
    <form action="${pageContext.request.contextPath}/DealWith" method="post">
        <table border="1">
        <tr>简单的计算器</tr>
        <tr>
          <td>第一个参数:</td>
          <td><input  type="text" name="firstNumber"></td>
        </tr>
        <tr>
          <td>运算符:</td>
          <td>
          <select name = "operator">
          	<option>+</option>
          	<option>-</option>
          	<option>*</option>
          	<option>/</option>
          </select>
          </td>
        </tr>
        <tr>
          <td>第二个参数:</td>
          <td><input type="text" name="secondNumber"></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><input type="submit" value="计算"></td>
        </tr>
      </table>
    </form>
  </body>
</html>
<%@ page language="java" import="java.util.*" import="javaBean.Calculate" pageEncoding="utf-8"%>
<html>
  <head>
    <title>calculator.html</title>  
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
  <% 
  Calculate c = (Calculate)request.getAttribute("calculate");
  String s1 = c.getFirstName();
  String s2 = c.getOperation();
  String s3 = c.getSecondName();
  String s4 = c.getResult()+"";
  String s = s1 + s2 +s3 +"="+s4;
  %>
  	<table>
  		<tr>
          <td>计算结果是:</td>
          <td><input type="text" value = "<%=s%>"></td>
        </tr>
  	</table> 
        <form action="${pageContext.request.contextPath}/DealWith" method="post">
        <table border="1">
        <tr>简单的计算器</tr>
        <tr>
          <td>第一个参数:</td>
          <td><input  type="text" name="firstNumber"></td>
        </tr>
        <tr>
          <td>运算符:</td>
          <td>
          <select name = "operator">
          	<option>+</option>
          	<option>-</option>
          	<option>*</option>
          	<option>/</option>
          </select>
          </td>
        </tr>
        <tr>
          <td>第二个参数:</td>
          <td><input type="text" name="secondNumber"></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><input type="submit" value="计算"></td>
        </tr>
      </table>
    </form>
  </body>
</html>

目录结构
在这里插入图片描述
在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值