如何在doGet()方法的validation()方法中移动if语句

时间:2017-07-27 12:04:08

标签: java refactoring

这是我的代码。我需要帮助移动所有if else in另一个名为validation()的方法,所以我可以优化它,而不是一遍又一遍地使用相同的代码。我该怎么办?我可以在doGet()方法中创建这个新方法,还是必须将它放在/上面?

private static final long serialVersionUID = 1L;

public Currency() {
    super();
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String currencyCode = request.getParameter("currencyCode");
    String currencyValue = request.getParameter("currencyValue");

    double result = 0;

    if (currencyCode.equals("") && currencyValue.equals("")) {
        // end user display
        PrintWriter out = response.getWriter();
        out.print("<html>");
        out.print("<head>");
        out.print("<title>Value Convertor</title>");
        out.print("</head>");
        out.print("<body><br>");
        out.print("<h1>ERROR! Code and Value are not set. Please enter Code and Value you want to exchange.</h1>");
        out.print("</body>");
        out.print("<html>");
        System.out.println(
                "ERROR! currencyCode and currencyValue are not set. Please enter Code and Value you want to exchange.");
    } else if (currencyCode.equals("")) {
        // end user display
        PrintWriter out = response.getWriter();
        out.print("<html>");
        out.print("<head>");
        out.print("<title>Value Convertor</title>");
        out.print("</head>");
        out.print("<body><br>");
        out.print("<h1>ERROR! Code is not set. Please enter Code you want to exchange.</h1>");
        out.print("</body>");
        out.print("<html>");
        System.out.println("ERROR! currencyCode is not set. Please enter Code and Value you want to exchange.");
    } else if (currencyValue.equals("")) {
        // end user display
        PrintWriter out = response.getWriter();
        out.print("<html>");
        out.print("<head>");
        out.print("<title>Value Convertor</title>");
        out.print("</head>");
        out.print("<body><br>");
        out.print("<h1>ERROR! Value is not set. Please enter Value se we can exchange your currency.</h1>");
        out.print("</body>");
        out.print("<html>");
        System.out.println("ERROR! currencyValue is not set. Please enter Code and Value you want to exchange.");
    }

    // DB
    Connection conn = null;

    double exchange = 1;

    try {
        Class.forName("org.postgresql.Driver");

        // String URL = ;
        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/currency", "postgres", "amdcs16");
        Statement st = conn.createStatement();
        ResultSet rs = st
                .executeQuery("SELECT * FROM currency_exchange WHERE currency_code = '" + currencyCode + "'");
        while (rs.next()) {
            // Displaying data of tables
            System.out.println("Your currency is: " + rs.getString("currency_code"));
            System.out.println("The rate of currency is: " + rs.getString("exchange"));
            exchange = rs.getDouble("exchange");
            System.out.println("Exchange: " + exchange);
        }
        st.close();
        rs.close();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } finally {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    double currValue = Double.parseDouble(currencyValue);
    result = exchange * currValue;
    // end user display
    PrintWriter out = response.getWriter();
    out.print("<html>");
    out.print("<head>");
    out.print("<title>Value Convertor</title>");
    out.print("</head>");
    out.print("<body><br>");
    out.print("<h1>The exchange (BGN/" + currencyCode + ") is " + result + "</h1>");
    out.print("</body>");
    out.print("<html>");
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    doGet(request, response);
}

你能给我一些建议或任何可以帮助我的例子吗?我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:1)

提取方法:

private void printOutput(HttpServletResponse response, String msg3) {
    PrintWriter out = response.getWriter();
    out.print("<html>");
    out.print("<head>");
    out.print("<title>Value Convertor</title>");
    out.print("</head>");
    out.print("<body><br>");
    out.print("<h1>" + msg + "</h1>");
    out.print("</body>");
    out.print("<html>");
}

if (currencyCode.equals("") && currencyValue.equals("")) {
    printOutput(response, "ERROR! Code and Value are not set. Please enter Code and Value you want to exchange.");
    System.out.println(
            "ERROR! currencyCode and currencyValue are not set. Please enter Code and Value you want to exchange.");
} else if (currencyCode.equals("")) {
    printOutput(response, "ERROR! Code is not set. Please enter Code you want to exchange.</h1>");
    System.out.println("ERROR! currencyCode is not set. Please enter Code and Value you want to exchange.");
} else if (currencyValue.equals("")) {
    printOutput(response, "ERROR! Value is not set. Please enter Value se we can exchange your currency.</h1>");
    System.out.println("ERROR! currencyValue is not set. Please enter Code and Value you want to exchange.");
}
相关问题