Помогите найти ошибку, что не нравиться валидатору (
Рекомендация ментора: Убедись, что метод getQuery формирует правильную строку.
package com.javarush.task.task22.task2208;
import java.util.*;
/*
Формируем WHERE
*/
public class Solution {
public static void main(String[] args) {
Map<String,String> testMap = new HashMap<>();
testMap.put("name", "Ivanov, ");
testMap.put("country", "Ukraine, ");
testMap.put("city", "Kiev, ");
testMap.put("age", null);
System.out.println(getQuery(testMap));
}
public static String getQuery(Map<String, String> params) {
String firstK = null, secondK = null, thirdK = null, fouthK =null ;
StringBuilder builder = new StringBuilder();
for(Map.Entry<String,String> value : params.entrySet()){
if(value.getValue() != null && value.getKey().replaceAll("\\W","").equals("name")
&& !(value.getValue().replaceAll("\\W","").equals("null"))
)
{
firstK = value.getKey().replaceAll("\\W","")+" = \'"+ value.getValue().replaceAll("\\W","")+"\'";
}
else if (value.getValue() != null && value.getKey().replaceAll("\\W","").equals("country")
&& !(value.getValue().replaceAll("\\W","").equals("null"))
)
{
secondK = value.getKey().replaceAll("\\W","")+" = \'"+ value.getValue().replaceAll("\\W","")+"\'";
}
else if (value.getValue() != null && value.getKey().replaceAll("\\W","").equals("city")
&& !(value.getValue().replaceAll("\\W","").equals("null"))
)
{
thirdK = value.getKey().replaceAll("\\W","")+" = \'"+ value.getValue().replaceAll("\\W","")+"\'";
}
else if(value.getValue() != null && value.getKey().replaceAll("\\W","").equals("age")
&& !(value.getValue().replaceAll("\\W","").equals("null"))
)
{
fouthK = value.getKey().replaceAll("\\W","")+" = \'"+ value.getValue().replaceAll("\\W","")+"\'";
}
}
if(firstK != null) {
builder.append(firstK);
if (secondK != null) builder.append(" and " + secondK);
if (thirdK != null) builder.append(" and " + thirdK);
if (fouthK != null) builder.append(" and " + fouthK);
}
else {
if (secondK != null)
{
builder.append(secondK);
if (thirdK != null) builder.append(" and " + thirdK);
if (fouthK != null) builder.append(" and " + fouthK);
}
else {
if(thirdK != null){
builder.append(thirdK);
if(fouthK != null) builder.append(" and " + fouthK);
}
else {
if ( fouthK != null) builder.append(fouthK);
}
}
}
String returned = builder.toString();
return returned;
}
}