-
Notifications
You must be signed in to change notification settings - Fork 7
/
Booking.java
232 lines (178 loc) · 6.16 KB
/
Booking.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package model;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Booking {
public static ArrayList<Profile> rgCustomers = new ArrayList<Profile>();
private int id;
// Format 4/20/2018
private Date date;
// Format HH:mm:ss
private Date startTime;
private Date endTime;
// unused in local memory
private int shop_id;
private int user_id;
// Connection to a persons booking
private Profile profile;
private Service service;
public Booking(String[] bookingData){
// String id, String date, String startTime, String endTime, String person_id, String service_id
// Convert everything in here
this.id = Integer.parseInt(bookingData[0]);
DateFormat formatterDate = new SimpleDateFormat("dd/mm/yy");
DateFormat formatterTime = new SimpleDateFormat("HH:mm:ss");
try {
this.date = formatterDate.parse(bookingData[1]);
this.startTime = formatterTime.parse(bookingData[2]);
this.endTime = formatterTime.parse(bookingData[3]);
} catch (ParseException e) {
e.printStackTrace();
}
boolean found = false;
// Try to find this persons ID
for(int i=0; i<rgCustomers.size(); i++){
if(bookingData[4].equals(rgCustomers.get(i).getID())){
System.out.println("FOUND A CUSTOMER MATCH!!!" + bookingData[4] + " @ " + rgCustomers.get(i).getID());
this.profile = rgCustomers.get(i);
found = true;
break;
}
}
if(!found){
// Open a URL connection and retrieve the data instead
String data = Connection.URL_GET_CUSTOMER + "?id=" + bookingData[4];
System.out.println("Trying to find customer--->" + data);
try {
URL url = new URL(data);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("User-Agent", "Mozilla/5.0");
// Read the JSON output here
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// Try reading it in JSON format
try {
JSONObject json = new JSONObject(response.toString());
System.out.println(json.getString("query_result"));
String query_response = json.getString("query_result");
if (query_response.equals("FAILED_CUSTOMER")) {
} else if (query_response.equals("SUCCESSFUL_CUSTOMER")) {
// Read up the JSON values
List<String> list = new ArrayList<String>();
JSONArray array = json.getJSONArray("customer");
String[] values = new String[7];
for(int i = 0 ; i < array.length() ; i++){
// Now create the profile
values[0] = array.getJSONObject(i).getString("id");
values[1] = array.getJSONObject(i).getString("first_name");
values[2] = array.getJSONObject(i).getString("last_name");
values[3] = array.getJSONObject(i).getString("age");
values[4] = array.getJSONObject(i).getString("phone_number");
values[5] = array.getJSONObject(i).getString("profile_picture");
values[6] = array.getJSONObject(i).getString("account_id");
}
Profile profile = new Profile(values);
rgCustomers.add(profile);
this.profile = profile;
found = true;
} else {
System.out.println("Not enough arguments were entered.. try filling both fields");
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if(!found){
try {
throw new Exception("Couldn't find a customer");
} catch (Exception e1) {
e1.printStackTrace();
}
}
found = false;
// Link a service
for(int i=0; i<User.getInstance().services.size(); i++){
if(User.getInstance().services.get(i).getId() == Integer.parseInt(bookingData[5])){
this.service = User.getInstance().services.get(i);
found = true;
break;
}
}
if(!found){
try {
throw new Exception("Unable to find service!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
public int getID() {
return this.id;
}
public Service getService(){
return this.service;
}
public Profile getProfile(){
return this.profile;
}
/**
* Copy constructor
*/
public Booking(Booking source) {
id = source.id;
date = source.date;
startTime = source.startTime;
endTime = source.endTime;
profile = source.profile;
service = source.service;
}
@SuppressWarnings("deprecation")
public String getDay(){
String[] days = {"Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
return days[date.getDay()];
}
public String getDate() {
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yy");
return sdf.format(date);
}
public String getStartTime() {
SimpleDateFormat hh = new SimpleDateFormat("HH:mm");
return hh.format(startTime);
}
public String getEndTime() {
SimpleDateFormat hh = new SimpleDateFormat("HH:mm");
return hh.format(endTime);
}
@Override
public String toString(){
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yy");
SimpleDateFormat hh = new SimpleDateFormat("HH:mm:ss");
return "ID: " + this.id + " date: " + sdf.format(date) + " start: " + hh.format(startTime) + " end: " + hh.format(endTime);
}
public String getPrice() {
return Double.toString(this.service.getPrice());
}
}