Java if文の冗長化をなくしたい
解決したいこと
if文の条件式と処理の冗長化をなくしたいです。
発生している問題・エラー
String item = request.getParameter("items"); //1,2,3,4のどれかが入る
String sort = request.getParameter("sort"); //asc,descのどれかが入る
String sql = "select * from huga";
if(sort == null){
;
}else if (sort.equals("asc")){
sort = "asc";
if(item.equals("1")){
item = "hoge";
sql = sql + " " + "order by" + " " + item + " " + sort;
}else if(item.equals("2")){
item = "noge";
sql = sql + " " + "order by" + " " + item + " " + sort;
}else if(item.equals("3")){
item = "muge";
sql = sql + " " + "order by" + " " + item + " " + sort;
}else if(item.equals("4")){
item = "mage";
sql = sql + " " + "order by" + " " + item + " " + sort;
}else{
;
}
}else if(sort.equals("desc")){
sort = "desc";
if(item.equals("1")){
item = "hoge";
sql = sql + " " + "order by" + " " + item + " " + sort;
}else if(item.equals("2")){
item = "noge";
sql = sql + " " + "order by" + " " + item + " " + sort;
}else if(item.equals("3")){
item = "muge";
sql = sql + " " + "order by" + " " + item + " " + sort;
}else if(item.equals("4")){
item = "mage";
sql = sql + " " + "order by" + " " + item + " " + sort;
}else{
;
}
}else{
;
}
ResultSet result = state.executeQuery(sql);
0