-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
182 lines (150 loc) · 4.01 KB
/
App.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
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.Calendar;
import java.lang.Thread;
public class App
{
private static Meal[] Menu;
private static App instance = null;
protected App()
{
Menu = new Meal[5];
try
{
Document doc = Jsoup.connect("http://butlercatering.se/einstein").get();
getMenu(doc);
}
catch (Exception e)
{
if (e instanceof java.net.UnknownHostException) {
System.out.println("Error: Unknown host "+e.getMessage());
} else {
System.out.println(e.toString());
}
System.exit(0);
}
}
public static App getInstance()
{
if(instance == null)
{
instance = new App();
}
return instance;
}
public static Meal[] getMenu()
{
return Menu;
}
public static Meal getTodaysMenu()
{
return Menu[getDay()];
}
public static void printTodaysMenu()
{
Meal men = Menu[getDay()];
System.out.print(men.getMenu());
}
public static void printWeekMenu()
{
for(int i = 0; i < Menu.length; i++)
{
if(i == getDay())
{
System.out.print("=>");
}
System.out.println(Menu[i].getMenu());
System.out.println("---------------------------------\n\n");
}
}
private static void getMenu(Document d)
{
Elements es = d.getElementsByClass("field-day");
String dayM = "";
for(int i = 0; i < es.size() ; i++)
{
Element e2 = es.get(i).getElementsByClass("field-label").get(0);
Element fish = e2.nextElementSibling();
Element meat = fish.nextElementSibling();
Menu[i] = new Meal(e2.text(),meat.text(),fish.text());
}
}
private static int getDay()
{
Calendar c = Calendar.getInstance();
if(c.get(Calendar.HOUR_OF_DAY) >= 13)
{
return c.get(Calendar.DAY_OF_WEEK)-1;
}
else
{
return c.get(Calendar.DAY_OF_WEEK)-2;
}
}
private static int weekdayToNr(String s)
{
switch (s.toLowerCase())
{
case "måndag":
return 1;
case "tisdag":
return 2;
case "onsdag":
return 3;
case "torsdag":
return 4;
case "fredag":
return 5;
default:
return -1;
}
}
private static String getShortMeal(String s)
{
s = s.replaceAll("[^a-zäöåA-ZÄÖÅ0-9\\s]", " ");
String[] ss = s.split(" ");
String res = "";
int i = 0;
while(i < ss.length-1 )
{
if (ss[i].equals("med") || ss[i].equals("serveras"))
{
i = ss.length;
}
else
{
res = res + ss[i] + " " ;
i++;
}
}
return res;
}
public static class Meal
{
public String Day;
public String Meat;
public String Fish;
public Meal(String d, String m, String f )
{
Day = d;
Meat = m;
Fish = f;
}
public String getMenu()
{
return Day +"\n\nFisk:\n" + Fish +"\n\nKött:\n"+ Meat + "\n\n";
}
public String getShortFish()
{
return getShortMeal(Fish);
}
public String getShortMeat()
{
return getShortMeal(Meat);
}
}
}