JavaWeb实战:报价计算系统(layui+tomcat+cookie实现)


JavaWeb实战:报价计算系统(layui+tomcat+cookie实现)

系统概述:

该系统是文物物流公司的一个小功能模块,用于帮助用户计算运费。点击查看实际效果

系统文档:

添加展品:

在表单内添加展品信息,完毕后点击添加展品,即可在展品列表中显示相应的展品信息,也可以对已经添加的展品进行删除,完成之后点击下一步。

添加行程:

使用方法与添加行程类似,添加完毕之后点击先一步。

生成报价:

在下拉选项栏中选择服务公司,选择完毕点击立即查询即可生成报价。

开发中使用的技术:

layui

tomcat

json

cookie

实现思路:

我个人的想法是,先这个把这个系统划分生两大部分:第一部分:展品、行程列表的管理,第二部分:报价计算。

第一部分、展品、行程列表的管理:

展品和行程可以看作是两个类,那么展品和行程列表就可以看成是两个ArrayList,展品和行程列表的管理就可以看作是Array List的增加、删除和查询整个链表。

明白这个道理后,就是具体的操作实现了。所以我宏观的思路是前端进行信息的提交,后端进行详细的信息操作。

添加:前端发送添加展品/行程的信息,后端得到表单数据,生成cookie;

删除:前端发送删除展品的信息,后端接受信息并读取cookie生成链表删除删除相应对象,更新cookie。

显示列表:后端读取cookie,将cookie转换为json发送至客户端,前端根据后端json生成列表。

第二部分、报价计算:

该部分比较简单,得到展品和行程的ArrayList根据相应公式计算的到总价。

代码实现:

servlet类(实现行程和展品信息的处理和给前端发送数据)

package com.bdzp.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.bdzp.Utils.GetWord;
import com.bdzp.daomain.modelContractParams;
import com.bdzp.daomain.modelTrip;
import com.bdzp.daomain.modelVolume;
import com.bdzp.service.PriceService;
import com.bdzp.service.imp.PriceServiceTestImpI;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.net.http.HttpResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@WebServlet(name = "ServletPriceTest")
public class ServletPriceTest extends HttpServlet {
    private static  final long serialVersionUID=1L;
    private PriceService ps=new PriceServiceTestImpI();
    modelContractParams params=new modelContractParams();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");//解决乱码
        response.setContentType("text/html;charset=UTF-8");
        String type =request.getParameter("command");
        if ("addDisplay".equals(type))
        {
            addDisplay(request,response);
        }else if("delDisplay".equals(type)){
            delDisplay(request,response);
        }else if("getDisplayList".equals(type)){
            getDisplayList(request,response);
        }else if("addTrip".equals(type)){
            addTrip(request,response);
        }else if("delTrip".equals(type)){
            delTrip(request,response);
        }else if("getTripList".equals(type)) {
            getTripList(request, response);
        } else if("getPrice".equals(type)){
            getPrice(request,response);
        }else if ("submitParams".equals(type)){
            try {
               params=submitParams(request, response);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }else if ("madeContract".equals(type)){
                downloadFileTest(request, response,params);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
    public void addDisplay(HttpServletRequest request,HttpServletResponse response){
        String name=request.getParameter("displayName");
        int amount=Integer.parseInt(request.getParameter("amount"));
        int height=Integer.parseInt(request.getParameter("height"));
        int width=Integer.parseInt(request.getParameter("width"));
        int length=Integer.parseInt(request.getParameter("length"));
        modelVolume volume=new modelVolume();
        volume.setName(name);
        volume.setAmount(amount);
        volume.setLength(length);
        volume.setWidth(width);
        volume.setHeight(height);
        if (addDisplay(volume,response,request)){
            System.out.println("添加展品成功");
            PrintWriter out= null;
            try {
                out = response.getWriter();
                out.print("Y");
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void delDisplay(HttpServletRequest request,HttpServletResponse response){
        String name=request.getParameter("displayName");
        if (delDisplay(name,response,request)){
            System.out.println("删除展品成功");
            PrintWriter out=null;
            try {
                out=response.getWriter();
                out.print("Y");
                out.flush();
                out.close();
            }catch (IOException e ){
                e.printStackTrace();
            }
        }
    }
    public void getDisplayList(HttpServletRequest request,HttpServletResponse response){
        String json = null;
        try {
            json = "{\"code\":0,\"msg\":\"\",\"count\":0,\"data\":" + JSON.toJSONString(getDisplayArrayList(request, response)) + "}";
        System.out.println(json);
            PrintWriter out=null;
            out = response.getWriter();
            out.print(json);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void addTrip(HttpServletRequest request,HttpServletResponse response){
        String name=request.getParameter("tripName");
        String start=request.getParameter("start");
        String end=request.getParameter("goal");
        int bigCarAmount=Integer.parseInt(request.getParameter("bigCarAmount"));
        int smallCarAmount=Integer.parseInt(request.getParameter("smallCarAmount"));
        int bigCarDistance=Integer.parseInt(request.getParameter("bigCarDistance"));
        int smallCarDistance=Integer.parseInt(request.getParameter("smallCarDistance"));
        int workerAmount=Integer.parseInt(request.getParameter("workerAmount"));
        int workerDays=Integer.parseInt(request.getParameter("workerDays"));
        modelTrip trip=new modelTrip();
        trip.setName(name);
        trip.setStart(start);
        trip.setEnd(end);
        trip.setBigCarAmount(bigCarAmount);
        trip.setBigCarDistance(bigCarDistance);
        trip.setSmallCarAmount(smallCarAmount);
        trip.setSmallCarDistance(smallCarDistance);
        trip.setWorkerAmount(workerAmount);
        trip.setWorkerDays(workerDays);
        if (addTrip(trip,response,request)){
            System.out.println("添加行程成功");
            PrintWriter out= null;
            try {
                out = response.getWriter();
                out.print("Y");
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public void delTrip(HttpServletRequest request,HttpServletResponse response){
        String name=request.getParameter("tripName");
        if (delTrip(name,response,request)){
            System.out.println("删除行程成功");
            PrintWriter out=null;
            try {
                out=response.getWriter();
                out.print("Y");
                out.flush();
                out.close();
            }catch (IOException e ){
                e.printStackTrace();
            }
        }
    }
    public void getTripList(HttpServletRequest request,HttpServletResponse response){
        try {
        String json = "{\"code\":0,\"msg\":\"\",\"count\":0,\"data\":" + JSON.toJSONString(getTripArrayList(request, response)) + "}";
        System.out.println(json);
            PrintWriter out=null;
            out = response.getWriter();
            out.print(json);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public boolean addDisplay(modelVolume model,HttpServletResponse response,HttpServletRequest request) {
        boolean flag=false;
        try {
        //value中储存的使json字符转,将json字符串转换为对象数组
        Cookie cookie=getCookie("displayList",response, request);
        //将json字符串转换为对象数组
        ArrayList displayList=new ArrayList();
            String cookieValue=URLDecoder.decode(cookie.getValue(), "utf-8");
            displayList= (ArrayList) JSON.parseArray(cookieValue,modelVolume.class);
            displayList.add(model);
            JSONArray array=JSONArray.parseArray(JSON.toJSONString(displayList));
            System.out.println(array);
            String encodeCookie = null;
            encodeCookie = URLEncoder.encode(array.toJSONString(),"utf-8");
            response.addCookie(new Cookie("displayList",encodeCookie));
            flag=true;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return flag;
    }
    public boolean delDisplay(String name,HttpServletResponse response,HttpServletRequest request){
        Cookie cookie=getCookie("displayList",response, request);
        try {
            String cookieValue=URLDecoder.decode(cookie.getValue(), "utf-8");
        ArrayList displayList= (ArrayList) JSON.parseArray(cookieValue,modelVolume.class);
        for (modelVolume display:displayList){
            if (display.getName().equals(name)){
                displayList.remove(display);
                JSONArray array=JSONArray.parseArray(JSON.toJSONString(displayList));
                try {
                    String encodeCookie = URLEncoder.encode(array.toJSONString(),"utf-8");
                    response.addCookie(new Cookie("displayList",encodeCookie));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return true;
            }
        }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return false;
    }
    public ArrayListgetDisplayArrayList(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        Cookie cookie=getCookie("displayList",response, request);
        String cookieValue=URLDecoder.decode(cookie.getValue(), "utf-8");
        ArrayList displayList= (ArrayList) JSON.parseArray(cookieValue,modelVolume.class);
        return displayList;
    }
    public boolean addTrip(modelTrip model,HttpServletResponse response,HttpServletRequest request){
        boolean flag=false;
        //value中储存的使json字符转,将json字符串转换为对象数组
        try {
        Cookie cookie=getCookie("tripList",response, request);
        //将json字符串转换为对象数组
        ArrayList tripList=new ArrayList();
            String cookieValue=URLDecoder.decode(cookie.getValue(), "utf-8");
            tripList= (ArrayList) JSON.parseArray(cookieValue,modelTrip.class);
            tripList.add(model);
            JSONArray array=JSONArray.parseArray(JSON.toJSONString(tripList));
            System.out.println(array);
            String encodeCookie = null;
            encodeCookie = URLEncoder.encode(array.toJSONString(),"utf-8");
            response.addCookie(new Cookie("tripList",encodeCookie));
            flag=true;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return flag;
    }
    public boolean delTrip(String name,HttpServletResponse response,HttpServletRequest request){
        Cookie cookie=getCookie("tripList",response, request);
        try {
            String cookieValue=URLDecoder.decode(cookie.getValue(), "utf-8");
            ArrayList tripList= (ArrayList) JSON.parseArray(cookieValue,modelTrip.class);
            for (modelTrip trip:tripList){
                if (trip.getName().equals(name)){
                    tripList.remove(trip);
                    JSONArray array=JSONArray.parseArray(JSON.toJSONString(tripList));
                    try {
                        String encodeCookie = URLEncoder.encode(array.toJSONString(),"utf-8");
                        response.addCookie(new Cookie("tripList",encodeCookie));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                    return true;
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return false;
    }
    public ArrayListgetTripArrayList(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        Cookie cookie=getCookie("tripList",response, request);
        String cookieValue=URLDecoder.decode(cookie.getValue(), "utf-8");
        ArrayList tripList= (ArrayList) JSON.parseArray(cookieValue,modelTrip.class);
        return tripList;
    }
    public Cookie getCookie(String name,HttpServletResponse response,HttpServletRequest request){
        Cookie result=null;
        boolean flag=false;
        if (request.getCookies()!=null){
            for (Cookie cookie:request.getCookies()){
                if (cookie.getName().equals(name)){
                    result=cookie;
                    flag=true;
                }
            }
        }
        if (!flag){
            try {
                String encodeCookie = URLEncoder.encode("[]","utf-8");
                Cookie cookie=new Cookie(name,encodeCookie);
                cookie.setMaxAge(1);
                response.addCookie(cookie);
                result=cookie;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    public void getPrice(HttpServletRequest request,HttpServletResponse response){
        try {
        String company=request.getParameter("company");
        int price =ps.getPrice(company,getDisplayArrayList(request, response),getTripArrayList(request, response)).get(7);
            PrintWriter out = null;
            out = response.getWriter();
            out.print(price);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public modelContractParams submitParams(HttpServletRequest request,HttpServletResponse response) throws ParseException {
        String company =request.getParameter("company");
        String activity=request.getParameter("activity");
        String partyA=request.getParameter("partyA");
        String address=request.getParameter("address");
        String name=request.getParameter("name");
        String phone=request.getParameter("phone");
        String startString=request.getParameter("startDate");
        String endString=request.getParameter("end");
        String start= new String(new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(startString)));
        String end= new String(new SimpleDateFormat("yyyy年MM月dd日").format(new SimpleDateFormat("yyyy-MM-dd").parse(endString)));
        modelContractParams params=new modelContractParams();
        params.setCompany(company);
        params.setActivity(activity);
        params.setAddress(address);
        params.setName(name);
        params.setPartyA(partyA);
        params.setPhone(phone);
        params.setStart(start);
        params.setEnd(end);
        try {
            PrintWriter out=null;
            out=response.getWriter();
            out.print("Y");
            out.flush();
            out.close();
        }catch (IOException e ){
            e.printStackTrace();
        }
        return params;
    }
    public void downloadFileTest(HttpServletRequest request,HttpServletResponse response,modelContractParams params){
        try {
            // 处理中文文件名下载乱码
            request.setCharacterEncoding("UTF-8");
        String path = "";
        String fileName = "";
        String activity=params.getActivity();
        String name ="“"+ activity+"”展品包装运输服务协议书.doc";
        // 投保书
            fileName = new String(name.getBytes("UTF-8"), "iso-8859-1");
        GetWord gw = new GetWord();
        //价格明细表格
        List> priceTableList=new ArrayList>();
        Map tempValue=new HashMap<>();
        List priceList=ps.getPrice(request.getParameter("company"),getDisplayArrayList(request, response),getTripArrayList(request, response));
        tempValue.put("inProductionCost",String.valueOf(priceList.get(0)));
        tempValue.put("packingMaterialCost",String.valueOf(priceList.get(1)));
        tempValue.put("outProductionCost",String.valueOf(priceList.get(2)));
        tempValue.put("bigCarCost",String.valueOf(priceList.get(3)));
        tempValue.put("smallCarCost",String.valueOf(priceList.get(4)));
        tempValue.put("workerCost",String.valueOf(priceList.get(5)));
        tempValue.put("taxes",String.valueOf(priceList.get(6)));
        tempValue.put("sumCost",String.valueOf(priceList.get(7)));
        priceTableList.add(tempValue);
        path = gw.getWordTest(request, response,
                priceTableList,
                ps.getParams(params,getDisplayArrayList(request, response),getTripArrayList(request, response),priceList));
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/msword"); // word格式

            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
            File file = new File(path);
            InputStream inputStream = new FileInputStream(file);
            OutputStream os = response.getOutputStream();
            byte[] b = new byte[10240];
            int length;
            while ((length = inputStream.read(b)) > 0) {
                os.write(b, 0, length);
            }
            inputStream.close();
            os.flush();
            os.close();
            file.delete();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

PriceServicempl类,用于价格计算

public List getPrice(String company, ArrayList displayList, ArrayList tripList) {
    int price=0;
    //得到总体积  保留三位小数
    double sumVolume=0;   //总体积
    int sumDisplayAmount=0;//得到展品总量
    for (modelVolume volume:displayList){
        sumVolume+=volume.getVolume();
        sumDisplayAmount+=volume.getAmount();
    }
    sumVolume=Math.ceil(sumVolume*0.001)*0.001;

    /*报价计算*/
    //包装费用
    int inProductionCost=(int)Math.ceil(sumVolume*2800*0.1)*10; //inProductionCost 内箱制作费
    int packingMaterialCost=0;  //PackingMaterialCost 包装材料费
    if (sumDisplayAmount<=70){
        packingMaterialCost=3000;
    }else {
        packingMaterialCost=5000;
    }
    //((int)(sumVolume/0.5)==sumVolume/0.5?sumVolume/0.5*1800:(sumVolume/0.5+1)*1800);
    int outProductionCost=(int)Math.ceil(sumVolume/0.5)*1800;    //outProductionCost 外箱制作费
    int finallyPackingCost=inProductionCost+outProductionCost+packingMaterialCost;//finallyPackingCost 总包装费
    int bigCarCost=0;//大车总费用
    int smallCarCost=0;//小车总费用
    int workerCost=0;//总人工费
    for (modelTrip trip:tripList){
        bigCarCost+=trip.getBigCarAmount()*trip.getBigCarDistance()*18;
        smallCarCost+=trip.getSmallCarAmount()*trip.getSmallCarDistance()*10;
        workerCost+=trip.getWorkerAmount()*trip.getWorkerDays()*800;
    }
    double x=Math.ceil((finallyPackingCost+bigCarCost+smallCarCost+workerCost)*0.1)*10;
    price=(int)x;
    int taxation=0;//税费
    if (price==3000) {
        price=0;
    } else if ("BeiJing".equals(company)) {
        taxation=(int)Math.ceil(price * 0.06 / 10) * 10;
        price=taxation + price;
    } else if ("HeNan".equals(company)) {
        taxation=(int)Math.ceil(price * 0.03 / 10) * 10;
        price=taxation + price;
    }
    List priceList=new ArrayList();
    priceList.add(inProductionCost);
    priceList.add(packingMaterialCost);
    priceList.add(outProductionCost);
    priceList.add(bigCarCost);
    priceList.add(smallCarCost);
    priceList.add(workerCost);
    priceList.add(taxation);
    priceList.add(price);
    return priceList;
}

前端代码:

``



   
      
      价格计算
      
   
   
   
      
      
      
      
  • 添加展品
  • 添加行程
  • 生成报价
  • 生成合同
行程信息
大车运输
小车运输
人工服务
合计费用

信息录入
-