2017-07-27 48 views
0

这是我的代码。我需要帮助的所有的if else朝着称为validation()另一种方法,所以我可以优化它,而不是一遍又一遍使用相同的代码。我该怎么办?我可以在doGet()方法中使用这种新方法吗?还是我必须将它放在/之下?如何验证移动如果else语句()方法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); 
} 

你能给我一些建议或任何可以帮助我的例子吗?我会感谢任何和所有的帮助。

+0

创建方法'私人无效的validate(HttpServletRequest的请求,HttpServletResponse的响应)'之上或之下的'doGet'(无所谓),并把所有的的if-else内部陈述。此外'currencyValue.isEmpty()'等同于'currencyValue.equals(“”)' – ChristofferPass

+0

您可以在一个方法打动你标签,它总是重复,所以只需添加变量 – sForSujit

回答

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."); 
} 
+0

感谢您的回答,我很感激。你可以告诉我,我应该把它放在doGet()方法的内部还是外部? @TimBiegeleisen –

+0

它会没事的,在外面,你只需要调用这些方法 – sForSujit

+0

@VakacTodorov您可以在同一类的辅助方法,其中''的doGet出现()。 –