This repository has been archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSigns.java
197 lines (151 loc) · 4.42 KB
/
Signs.java
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
public class Signs {
private static Calendar c = Calendar.getInstance();
private static final String HOROSCOPE_REST_API = "http://widgets.fabulously40.com/horoscope.xml";
private static Sign sign;
public static Sign getSign() {
return sign;
}
public static void setSign(Sign sign) {
Signs.sign = sign;
}
public static List<Sign> star_Signs = new ArrayList<Sign>();
public static void initStarSigns() {
Sign sign ;
sign = new Sign(
"Aquarius",
new Date(c.YEAR, 1, 20),
new Date(c.YEAR, 2, 18)
);
star_Signs.add(sign);
sign = new Sign(
"Pisces",
new Date(c.YEAR, 2, 19),
new Date(c.YEAR, 3, 19)
);
star_Signs.add(sign);
sign = new Sign(
"Aries",
new Date(c.YEAR, 3, 20),
new Date(c.YEAR, 4, 19)
);
star_Signs.add(sign);
sign = new Sign(
"Taurus",
new Date(c.YEAR, 4, 20),
new Date(c.YEAR, 5, 19)
);
star_Signs.add(sign);
sign = new Sign(
"Gemini",
new Date(c.YEAR, 5, 21),
new Date(c.YEAR, 6, 20)
);
star_Signs.add(sign);
sign = new Sign(
"Cancer",
new Date(c.YEAR, 6, 21),
new Date(c.YEAR, 7, 21)
);
star_Signs.add(sign);
sign = new Sign(
"Leo",
new Date(c.YEAR, 7, 22),
new Date(c.YEAR, 8, 22)
);
star_Signs.add(sign);
sign = new Sign(
"Virgo",
new Date(c.YEAR, 8, 23),
new Date(c.YEAR, 9, 22)
);
star_Signs.add(sign);
sign = new Sign(
"Libra",
new Date(c.YEAR, 9, 23),
new Date(c.YEAR, 10, 22)
);
star_Signs.add(sign);
sign = new Sign(
"Scorpio",
new Date(c.YEAR, 10, 23),
new Date(c.YEAR, 11, 21)
);
star_Signs.add(sign);
sign = new Sign(
"Sagittarius",
new Date(c.YEAR, 11, 22),
new Date(c.YEAR, 12, 20)
);
star_Signs.add(sign);
sign = new Sign(
"Capricorn",
new Date(c.YEAR, 12, 21),
new Date(c.YEAR, 1, 19)
);
star_Signs.add(sign);
}
public static String getHoroscope(Date birthDay) {
return getStarSign(birthDay).getHoroscope();
}
public static String getHoroscope(int month, int day) {
Date birthDay = new Date(c.YEAR, month, day);
Sign s = getStarSign(birthDay);
sign = s;
return s.getName() + ": " + getWSHoroscope(s.getName().toLowerCase());
}
private static Sign getStarSign(Date birthDay) {
for (Sign s : star_Signs) {
if (birthDay.after(s.getDateFrom()) &&
birthDay.before(s.getDateTo())) {
return (Sign) s;
}
}
return null;
}
private static String getWSHoroscope(String sign) {
try {
URL apiEndPoint = new URL(HOROSCOPE_REST_API + "?sign=" + sign);
URLConnection connection = apiEndPoint.openConnection();
BufferedReader in = new BufferedReader(
new InputStreamReader(
connection.getInputStream()));
StringBuilder builder = new StringBuilder();
String inputLine;
while ((inputLine = in.readLine()) != null) {
builder.append(inputLine);
builder.append("\n");
}
in.close();
Document doc = DocumentHelper.parseText(builder.toString());
Node node = doc.node(0);
Element el = (Element) node;
return el.attributeValue("horoscope");
} catch (MalformedURLException e) {
JOptionPane.showMessageDialog(null, "Url of the API is not correct.");
throw new RuntimeException("Url of the API is not correct.");
} catch (IOException e) {
JOptionPane.showMessageDialog(null, "Cannot communicate with the server.");
throw new RuntimeException("Cannot communicate with the server.");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Horoscope not found";
}
}