Hi.
Hi.
What is wrong?
Validator is impermeable:
The getQuery method must return a string built according to the rules specified in the task conditions.
Be sure that the getQuery method works correctly if the passed Map has null values.
I check if params is null if it's going out.
I set up all the possibilities and testing is ok.
I don't know what's validation now?
Suggestions??
Thanks:)
package com.codegym.task.task22.task2208;
import java.util.*;
/*
Build a WHERE query
*/
public class Solution
{
public static void main(String[] args)
{
Map<String, String> dane= new LinkedHashMap<>();
dane.put("name", "Johnson");
dane.put("country", "United States");
dane.put("city", "Los Angeles");
dane.put("age", null);
//dane.put("sex", "men");
//dane.put("nationality", "Polish");
System.out.println(getQuery(dane));
}
public static String getQuery(Map<String, String> params)
{
if (params == null)
{
return "";
}
StringBuilder endStr = new StringBuilder();
int size = 0;
String format = "%s = '%s'";
for (Map.Entry<String, String> pair : params.entrySet())
{
if (pair.getKey() != null && pair.getValue() != null)
{
if(size < params.size() -1)
{
endStr.append(String.format(format, pair.getKey(), pair.getValue()) + " and ");
size++;
}
else if(size == params.size() -1)
{
endStr.append(String.format(format, pair.getKey(), pair.getValue()));
}
}
else if(pair.getValue() == null)
{
endStr.delete(endStr.length()-5,endStr.length());
break;
}
else if (pair.getKey() == null && pair.getValue() == null)
{
endStr.delete(endStr.length()-5,endStr.length());
break;
}
else if (pair.getKey() == null && pair.getValue() != null)
{
endStr.delete(endStr.length()-5,endStr.length());
break;
}
}
return endStr.toString();
}
}