-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryBean.java
More file actions
161 lines (125 loc) · 4.51 KB
/
QueryBean.java
File metadata and controls
161 lines (125 loc) · 4.51 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package beans;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
public class QueryBean implements Serializable {
private static final String[] AVAILABLE_X = new String[] {"-2", "-1.5", "-1", "-0.5", "0", "0.5", "1", "1.5", "2"};
private static final String[] AVAILABLE_R = new String[] {"1", "1.5", "2", "2.5", "3"};
private static final double AVAILABLE_Y_MIN = -3;
private static final double AVAILABLE_Y_MAX = 5;
private HistoryBean historyBean;
private String r, y, alternativeR;
private final Map<String, Boolean> x = new HashMap<>();
private String errorMessage;
public QueryBean() {}
public Object rAction(String r) {
if (Objects.equals(this.r, r)) {
this.r = null;
this.alternativeR = null;
} else {
this.r = r;
this.alternativeR = null;
}
return null;
}
public Object mainAction() {
if (Stream.of(AVAILABLE_R).noneMatch(r::equals)) {
if (alternativeR.equals("")){
errorMessage = "потому что ты не выбрал R";
return null;
}
else
{this.r=alternativeR;}
}
Boolean result = getResult();
if (y == null || y.isEmpty()) {
errorMessage = "потому что ты не ввёл Y";
return null;
}
try {
double y = Double.parseDouble(this.y);
if (y <= AVAILABLE_Y_MIN || y >= AVAILABLE_Y_MAX) {
errorMessage = "потому что Y не входит в (" + AVAILABLE_Y_MIN + ", " + AVAILABLE_Y_MAX + ")";
return null;
}
} catch (NumberFormatException e) {
errorMessage = "потому что Y не число";
return null;
}
if (!x.containsValue(true)) {
errorMessage = "потому что ты не выбрал X";
return null;
}
if (x.values().parallelStream().filter(v -> v).count() > 1) {
errorMessage = "потому что ты выбрал слишком много X";
return null;
}
if (result != null) {
if (!result){
errorMessage = "потому что меня уронили:'(";
}
try {
historyBean.addQuery(new HistoryBean.Query(r, y, x.entrySet().parallelStream()
.filter(Map.Entry::getValue).map(Map.Entry::getKey).findAny().orElse(null), result));
}
catch (SQLException ignored){}
}
return null;
}
private Boolean getResult() {
Double x = this.x.entrySet().parallelStream()
.filter(Map.Entry::getValue).map(Map.Entry::getKey)
.map(Double::parseDouble).findAny().orElse(null);
try {
Double r = this.r == null ? null : Double.parseDouble(this.r);
Double y = this.y == null ? null : Double.parseDouble(this.y);
if (x != null && y != null && r != null) {
return x >= 0 && y >= 0 && x <= r && y <= r || //переделать
x <= 0 && y >= 0 && x * x + y * y <= r * r ||
x >= 0 && y <= 0 && y >= 2 * x - r;
}
} catch (NumberFormatException e) {
e.printStackTrace();
}
return null;
}
public void setHistoryBean(HistoryBean historyBean) {
this.historyBean = historyBean;
}
public String getR() {
return r;
}
public void setR(String r) {
this.r = r;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
public Map<String, Boolean> getX() {
return x;
}
public String getErrorMessage() {
return errorMessage;
}
public boolean getHasErrorMessage() {
return errorMessage != null;
}
public String[] getAvailableX() {
return AVAILABLE_X;
}
public String[] getAvailableR() {
return AVAILABLE_R;
}
public String getAlternativeR() {
return alternativeR;
}
public void setAlternativeR(String alternativeR) {
this.alternativeR = alternativeR;
}
}