Java 如何将validation()方法中的if-else语句从doGet()方法移动
这是我的密码。我需要帮助将所有if-else移到另一个名为validation的方法中,这样我就可以优化它,而不必反复使用相同的代码。我该怎么办?我可以把这个新方法放在doGet方法里面,还是必须把它放在doGet方法的上面/下面Java 如何将validation()方法中的if-else语句从doGet()方法移动,java,refactoring,Java,Refactoring,这是我的密码。我需要帮助将所有if-else移到另一个名为validation的方法中,这样我就可以优化它,而不必反复使用相同的代码。我该怎么办?我可以把这个新方法放在doGet方法里面,还是必须把它放在doGet方法的上面/下面 private static final long serialVersionUID = 1L; public Currency() { super(); } @Override protected void doGet(HttpServletReques
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);
}
你能给我一些建议或任何能帮助我的例子吗?我将感谢所有帮助。提取一个方法:
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.");
}
创建一个方法private void validateHttpServletRequest,在doGet之上或之下的HttpServletResponse响应无关紧要,并将所有if-else语句放入其中。此外,currencyValue.isEmpty与currencyValue.equals等效。您可以用一种方法移动标记,它总是重复,因此只需添加变量即可获得答案,非常感谢。你能告诉我应该把它放在什么地方吗,里面还是外面@TimBiegeleisenIt会很好的,在外面,你只需要打电话给他们methods@VakacTodorov可以将helper方法包含在doGet出现的同一类中。