這是我的代碼。我需要幫助的所有的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);
}
你能給我一些建議或任何可以幫助我的例子嗎?我會感謝任何和所有的幫助。
創建方法'私人無效的validate(HttpServletRequest的請求,HttpServletResponse的響應)'之上或之下的'doGet'(無所謂),並把所有的的if-else內部陳述。此外'currencyValue.isEmpty()'等同於'currencyValue.equals(「」)' – ChristofferPass
您可以在一個方法打動你標籤,它總是重複,所以只需添加變量 – sForSujit