Algorithm project pipeline laying system (javaweb+mvc+idea+degree map+prim)

This project requires the development of a pipeline laying auxiliary system. Taking the west campus of our school as an example, it is located in the Liren College teaching building, student apartments, student cafeteria, first, second, third and fourth teaching Buildings, School of Materials Science, School of Electrical Engineering, School of Science, Laying water pipelines between the School of Arts, School of Economics and Management, School of Silesia and School of Information, designing and implementing algorithms to minimize the distance of laid water pipelines, reading data from text, displaying the best laying plan, and drawing the best plan Simple schematic diagram and other functions.

Since this project is a collaborative project, I only wrote the prim algorithm, so I only upload the part about prim.

operation result

db.properties

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/map?userSSL=ture &useUnicode=true &characterEncoding=utf-8
username=root
password=root

index.jsp

<%@ page import="domain.Buildings" %>
<%@ page import="java.util.List" %>
<%@ page import="com.google.gson.Gson" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>Pipe laying auxiliary system</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <style>
        html{height:100%}
        body{height:100%;margin:0px;padding:0px}
        #container {
            overflow: hidden;
            width: 100%;
            height: 90%;
            margin: 0;
            font-family: "Microsoft Yahei";
        }
    </style>
    <script src="//api.map.baidu.com/api?type=webgl & amp;v=1.0 & amp;ak=40ZSZGnM6HRUM06RKMsnWOcyUsmTed3Z"></script>
</head>
<body>
<div id="container"></div>
<div id="r-result">
    <form action="/PrimeServlet" method="get">
        <p>Starting point:<input type="text" name="point"></p>
        <input type="submit" value="submit">
    </form>
    <input type="button" onclick="start();" value="kruskal" />
    <input type="button" onclick="startprime();" value="prim" />
<%-- <a class="btn float-buttons waves-effect waves-button waves-float" id="a" href="/PrimeServlet">--%>
<%-- primeBuildings--%>
<%-- </a>--%>
</div>
</body>
</html>
<script>
    var map = new BMapGL.Map('container'); // Create Map instance
    map.centerAndZoom(new BMapGL.Point(119.533143,39.912459), 18); // Initialize the map, set the center point coordinates and map level
    map.enableScrollWheelZoom(true); // Enable mouse wheel zoom

    var BuildingsList = [];
    <% List<Buildings> buildingList = (List<Buildings>) request.getAttribute("Buildings"); %>
    <% if (buildingList != null) { %>
    BuildingsList = <%= new Gson().toJson(buildingList) %>;
    <% } %>

    var route=[];
    <% List<String> routes = (List<String>) request.getAttribute("route"); %>
    <% if (routes != null) { %>
    route = <%= new Gson().toJson(routes) %>;
    <% } %>

    var path = [];
    for (var i1 = 0; i1 < route.length; i1 + + ) {
        var point2 = {
            'lng': parseFloat(route[i1].split(",")[0]),
            'lat': parseFloat(route[i1].split(",")[1])
        };
        path.push(point2);
    }

    function change(){
        var points = [];
        var labels = [];
        for (var i = 0; i < BuildingsList.length; i + + ) {
            var building = BuildingsList[i];
            var address1 = building.address;
            var name = building.name;
            var address=address1.split(",");
            var point = new BMapGL.Point(parseFloat(address[0]),parseFloat(address[1]));
            points.push(point);
            var label = new BMapGL.Label(name, {
                position: point,
                offset: new BMapGL.Size(0, 0)
            });
            labels.push(label);
        }

        for (var j = 0; j < labels.length; j + + ) {
            map.addOverlay(labels[j]);
        }
    }
    function start(){
        change();

        for (var ij=0;ij<path.length;) {
            var coors = [
                {'lat': parseFloat(path[ij].lat), 'lng': parseFloat(path[ij].lng)},
                {'lat': parseFloat(path[ij + 1].lat), 'lng': parseFloat(path[ij + 1].lng)}
            ];

            var polyline = new BMapGL.Polyline(coors);

            map.addOverlay(polyline);

            ij + = 2;
        }
    }

    var addresses=[];
    var point=[];
    <% int []point=(int[]) request.getAttribute("point"); %>
    <% if (point != null) { %>
    point = <%= new Gson().toJson(point) %>;
    <% } %>

    function startprime(){

        var ans=[];

        for(var i=0;i<point.length;i + + ){
            for(var j=0;j<BuildingsList.length;j + + ){
                var abstract_name=BuildingsList[j].abstract_name;
                if(point[i]==abstract_name) {
                    var address = BuildingsList[j].address;

                    var an = BuildingsList[j].abstract_name;
                    break;
                }
            }
            ans.push(an);
            addresses.push(address);
        }
        var lng=[];
        varlat=[];
        for (var i=0;i<addresses.length;i + + ) {
            const a = addresses[i].split(",");
            lng[i] = a[0];
            lat[i] = a[1];
        }

        for (var i=0;i<addresses.length;i + + ){
            var point1 = new BMapGL.Point(lng[i], lat[i]);

            var marker = new BMapGL.Marker(point1); // Create a marker
            map.addOverlay(marker); // Add markers to the map
        }
        for (var i=1;i<addresses.length;i + + ){

            // window.alert(lng[i], lat[i]);
            var polyline = new BMapGL.Polyline([
                new BMapGL.Point(lng[i-1], lat[i-1]),
                new BMapGL.Point(lng[i], lat[i])
            ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});
            map.addOverlay(polyline);
        }

    }
</script>
PrimeServlet
package servlet;

import domain.Buildings;
import domain.Edata;
import service.Kruskalimp;
import service.Primeimpl1;
import service.Buildingserviceimp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/PrimeServlet")
public class PrimeServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Buildingserviceimp Buildings = new Buildingserviceimp();
        List<Buildings> BuildingsList = new ArrayList<Buildings>();
        BuildingsList = Buildings.getBuildings();
        request.setAttribute("Buildings", BuildingsList);
        String v0=request.getParameter("point");
        int v=Integer.parseInt(v0);
        Primeimpl1 prime = new Primeimpl1();
        int []point=prime.Prim(v);
        request.setAttribute("point",point);
        request.getRequestDispatcher("/index.jsp").forward(request, response);
    }
}
Primeimpl1
package service;

import dao.Filedao;
import dao.Filedaoimp;

import java.io.IOException;

public class Primeimpl1 {
    Filedao Filedaoimp=new Filedaoimp();
    int[][] number;
    {
        try {
            number = Filedaoimp.read();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    int [][]edge=number; //Storage edges in the graph
    int vertexNum=14;
    boolean visited[] = new boolean[vertexNum];//Whether it has been visited
    int []point=new int[vertexNum];//Storage path vertices
    int MAX = Integer.MAX_VALUE;
    int []lowcost=new int[vertexNum];//The shortest distance to the set of points that have been passed
    int total=0;//used to record the sum of weights
    public int[] Prim(int v) throws IOException {
        int i,j,k,n;
        lowcost[v]=0;
        visited[v] = true;
        point[0]=v;
        for(i=0;i<vertexNum;i + + ){
            lowcost[i]=edge[v][i];
        }
        for(n=1;n<vertexNum;n + + ){
            int min=MAX;
            int index=-1;
            for(i=0;i<vertexNum;i + + ){
                if(!visited[i] & amp; & amp;lowcost[i]<min){
                    min=lowcost[i];
                    index=i;
                }
            }
            if(index==-1) System.out.println("error");
            total + =min;
            lowcost[index]=0;
            visited[index]=true;
            point[n]=index;
            //Update lowcost
            for(j=0;j<vertexNum;j + + ) {
                if(edge[index][j]<lowcost[j]) {
                    lowcost[j]=edge[index][j];
                }
            }
        }
// for (i=0;i<point.length;i + + ) {
// System.out.println(point[i]);
// }
// System.out.println(total);
        Filedaoimp.writefile(point,total);
        return point;
    }
}








Buildingservice
package service;

import java.util.List;
import domain.*;

public interface Buildingservice {
    public List<Buildings> getBuildings();
}
Buildingserviceimp
package service;

import java.sql.Connection;
import java.util.List;

import dao.BaseDao;
import dao.Buildingsdao;
import dao.Buildingsdaoimp;
import domain.*;

public class Buildingserviceimp implements Buildingservice {
    private Buildingsdao buildingdao;

    public Buildingserviceimp() {
        buildingdao = new Buildingsdaoimp();
    }
    public List<Buildings>getBuildings(){
        Connection connection = null;
        List<Buildings> buildinglist = null;
        try {
            connection = BaseDao.getConnection();
            buildinglist = buildingdao.getBuilding(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return buildinglist;
    }
}
Buildings
package domain;

public class Buildings {
    String name;
    String address;
    int abstract_name;

    public int getAbstract_name() {
        return abstract_name;
    }

    public void setAbstract_name(int abstract_name) {
        this.abstract_name = abstract_name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
BaseDao
package dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class BaseDao {
    //Static code block, executed when the class is loaded
    static {
        init();
    }

    private static String driver;
    private static String url;
    private static String username;
    private static String password;

    //Initialize connection parameters, obtained from configuration file
    public static void init(){
        Properties properties = new Properties();
        String configFile = "db.properties";
        InputStream inputStream = dao.BaseDao.class.getClassLoader().getResourceAsStream(configFile);

        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

        driver=properties.getProperty("driver");
        url=properties.getProperty("url");
        username=properties.getProperty("username");
        password=properties.getProperty("password");
    }

    /**
     * Get database connection
     * @return
     */
    public static Connection getConnection() {
        Connection connection =null;
        try {
            Class.forName(driver);
            connection= DriverManager.getConnection(url,username,password);
        }catch (Exception e){
            e.printStackTrace();
        }

        return connection;
    }


    /**
     * Write query public class
     */
    public static ResultSet execute(Connection connection, PreparedStatement pstm, ResultSet rs, String sql, Object[] params)throws Exception{
        pstm = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i + + ) {
            pstm.setObject(i + 1,params[i]);
        }
        rs=pstm.executeQuery();
        return rs;
    }

    /**
     *Write addition, deletion and modification of public classes
     */
    public static int execute(Connection connection,PreparedStatement pstm,
                              String sql,Object[] params) throws Exception{
        int updateRows = 0;
        pstm = connection.prepareStatement(sql);
        for(int i = 0; i < params.length; i + + ){
            pstm.setObject(i + 1, params[i]);
        }
        updateRows = pstm.executeUpdate();
        return updateRows;
    }

    /**
     * Release resources
     */
    public static boolean closeResource(Connection connection,PreparedStatement pstm,ResultSet rs){
        boolean flag = true;
        if(rs != null){
            try {
                rs.close();
                rs = null;//GC recycling
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }
        if(pstm != null){
            try {
                pstm.close();
                pstm = null;//GC recycling
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }
        if(connection != null){
            try {
                connection.close();
                connection = null;//GC recycling
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                flag = false;
            }
        }

        return flag;
    }
}
Buildingsdao
package dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import domain.*;

public interface Buildingsdao {
    public List<Buildings> getBuilding(Connection connection) throws Exception;
    public List<String> getabsname(Connection connection) throws SQLException;
}
Buildingsdaoimp
package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import domain.*;

public class Buildingsdaoimp implements Buildingsdao {
    public List<Buildings> getBuilding(Connection connection) throws Exception {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<Buildings> buildingList = new ArrayList<Buildings>();
        if (connection != null) {
            String sql = "select name,address,abstract_name from map";
            pstm = connection.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                Buildings _b = new Buildings();
                _b.setName(rs.getString("name"));
                _b.setAddress(rs.getString("address"));
                _b.setAbstract_name(rs.getInt("abstract_name"));
                buildingList.add(_b);
            }
            BaseDao.closeResource(null, pstm, rs);
        }
        return buildingList;
    }

    public List<String> getabsname(Connection connection) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet rs = null;
        List<String> absname = new ArrayList<String>();
        if (connection != null) {
            String sql = "select abstract_name from map";
            pstm = connection.prepareStatement(sql);
            rs = pstm.executeQuery();
            while (rs.next()) {
                String _b = rs.getString("abstract_name");
                absname.add(_b);
            }
            BaseDao.closeResource(null, pstm, rs);
        }
        return absname;
    }
}
Filedao
public interface Filedao {
    public int[][] read() throws IOException;
    public void writefile(int[]point,int total) throws IOException;
}
Filedaoimp
public class Filedaoimp implements Filedao {
    public int [][] read() throws IOException {
            String pathname = "D:\Documents\Tencent Files\2247594359\FileRecv\input(1).txt";
            File filename = new File(pathname);
            BufferedReader bf = new BufferedReader(new FileReader(filename));
            String textLine = new String();
            String str = new String();
            while((textLine = bf.readLine()) != null) {//Read one line at a time
                str + = textLine + ",";//separate each line read with commas
            }
            String[] numbers = str.split(",");//Save each line into numbers according to commas
            int[][] number = new int[14][14];//Matrix array
            String[] stmp = null;
            for(int i = 0; i < 14; i + + ) {
                stmp = numbers[i].split(" ");//Further separate the numbers in each line
                for(int j = 0; j < 14; j + + ) {
                    number[i][j] = Integer.parseInt(stmp[j]);
                }
            }
            for(int i = 0; i < 14; i + + ) {
                for(int j = 0; j < 14; j + + ) {
                    System.out.print(number[i][j] + " ");
                }
                System.out.print("\\
");
            }
            bf.close();
            return number;
        }

    @Override
    public void writefile(int[]point,int total) throws IOException {
        File outfile = new File("D:\Desktop\output.txt");
        if (outfile.exists()) {
            System.out.println("The file already exists");
            System.exit(1);
        }
        PrintWriter out = new PrintWriter(outfile, "UTF-8");
// out.println("The adjacency matrix of the minimum spanning tree is:");
// for(int i=0;i<matrix.length;i + + ){
// for (int j=0;j<matrix[0].length;j + + ){
// out.print(matrix[i][j]);
// }
// out.print("\\
");
// }
        out.println("The path of the minimum spanning tree is:");
        for(int i=0;i<point.length-1;i + + )
            out.print(point[i] + "->");
        out.println(point[13]);
        out.print("The length of the minimum spanning tree is:" + total);
        out.close();
    }
}