forked from rchatley/JavaWebApp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryProcessor.java
More file actions
81 lines (68 loc) · 2.79 KB
/
Copy pathQueryProcessor.java
File metadata and controls
81 lines (68 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package com.develogical;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static java.lang.Integer.parseInt;
public class QueryProcessor {
public String process(String query) {
if (query.toLowerCase().contains("shakespeare")) {
return "William Shakespeare (26 April 1564 - 23 April 1616) was an " +
"English poet, playwright, and actor, widely regarded as the greatest " +
"writer in the English language and the world's pre-eminent dramatist.";
}
if(query.toLowerCase().contains("which year was Theresa May first elected as the Prime Minister of Great Britain") ){
return "2016";
}
if(query.toLowerCase().contains("minus") ){
ArrayList<String> numbers = new ArrayList<>(Arrays.asList(query.split(":")));
ArrayList<String> numbers2 = new ArrayList<>(Arrays.asList(numbers.get(1).split(" ")));
List<Integer> numbers3 = numbers2.stream().filter(x->isInteger(x.trim())).map(i-> parseInt(i.trim())).collect(Collectors.toList());
return Integer.toString(numbers3.get(0)-numbers3.get(1));
}
if(query.toLowerCase().contains("following numbers") ){
if(query.toLowerCase().contains("largest")){
ArrayList<String> numbers = new ArrayList<>(Arrays.asList(query.split(":")));
ArrayList<String> numbers2 = new ArrayList<>(Arrays.asList(numbers.get(2).split(",")));
return numbers2.stream().map(x->parseInt(x.trim())).max(Integer::compare).toString();
}
if(query.toLowerCase().contains("square and a cube")){
String partAfterDot = query.substring(query.indexOf(':') + 2);
ArrayList<String> numbers = new ArrayList<>(Arrays.asList(query.split(":")));
ArrayList<String> numbers2 = new ArrayList<>(Arrays.asList(numbers.get(2).split(",")));
return numbers2.stream().map(x->parseInt(x.trim())).filter(i-> (isSquare(i)&&isCube(i))).collect(Collectors.toList()).toString();
}
}
return "";
}
static Boolean isSquare(int n)
{
for (int i = 0; i < n / 2 + 2; i++)
{
if (i * i == n)
{
return true;
}
}
return false;
}
static Boolean isCube(int n)
{
for (int i = 0; i < n / 2 + 2; i++)
{
if (i * i *i == n)
{
return true;
}
}
return false;
}
public static boolean isInteger(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException nfe) {
return false;
}
}
}