-
Notifications
You must be signed in to change notification settings - Fork 0
/
Onlineshop_Server.~ava
386 lines (367 loc) · 14.8 KB
/
Onlineshop_Server.~ava
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
*
* Description
*
* @version 1.0 from 5/12/2020
* @author JUSTUS E. STAMM && LASER OEZMEN
*/
public class Onlineshop_Server extends Server {
private Userbank userbank;
private Lager myitems;
private Bestellungen myorders;
private List<User> onlineusers;
private InterfaceForServer mygui;
private List<String> commands;
public Onlineshop_Server(InterfaceForServer pgui){
super(80);
userbank = new Userbank();
myitems = new Lager();
myorders = new Bestellungen();
onlineusers = new List<User>();
this.mygui = pgui;
commands = new List<String>();
commands.append("?LOGIN:username:password -> !LOGGEDIN / !LOGINERROR");
commands.append("?BASKET -> !BASKET:!ARTIKEL:artikelnummer,name,beschreibung,preis,hersteller:!ARTIKEL:");
commands.append("?SEARCH:keyword -> !RESULTS:!ARTIKEL:artikelnummer,name,beschreibung,preis,hersteller:!ARTIKEL:");
commands.append("?ADDTOBASKET:artikelnummer -> !ADDTOBASKET:SUCCESS/FAILURE:artikelnummer");
commands.append("?REMOVEFROMBASKET:artikelnummer -> !REMOVEFROMBASKET:SUCCESS/FAILURE:artikelnummer");
commands.append("?NEWACCOUNT:username:password:email -> !NEWACCOUNT:SUCCESS/FAILURE:username");
commands.append("?LOGOUT&BUY -> !LOGOUT&BUY:SUCCESS/FAILURE");
commands.append("?CLEARBASKET -> !CLEARBASKET");
commands.append("gibberish -> !ERR");
commands.append("?HISTORY -> !HISTORY:!BESTELLUNG:artikelnummer,name,beschreibung,preis,hersteller,bestellungsnummer:!BESTELLUNG:artikelnummer,name,beschreibung,preis,hersteller,bestellungsnummer");
commands.append("?NOTIFICATIONS -> !NOTIFICATION:text:!NOTIFICATION");
commands.append("?DELETENOTIFICATIONS -> !DELETENOTIFICATIONS");
commands.append("?WISHLIST -> !WISHLIST:!ARTIKEL:artikelnummer,name,beschreibung,preis,hersteller:!ARTIKEL");
commands.append("?ADDTOWISHLIST:artikelnummer -> !ADDTOWISHLIST:SUCCESS/FAILURE:artikelnummer");
commands.append("?REMOVEFROMWISHLIST:artikelnummer -> !REMOVEFROMWISHLIST:SUCCESS/FAILURE:artikelnummer");
commands.append("?CLEARWISHLIST -> !CLEARWISHLIST");
commands.append("?RECOMMEND:topn -> !RECOMMEND:!ARTIKEL:artikelnummer,name,beschreibung,preis,hersteller:!ARTIKEL:");
commands.append("?ARTIKEL:artikelnummer -> !ARTIKEL:artikelnummer,name,beschreibung,preis,hersteller");
commands.append("?ALLARTIKEL ->!ALLARTIKEL:!ARTIKEL:artikelnummer,name,beschreibung,preis,hersteller:!ARTIKEL:artikelnummer,name,beschreibung,preis,hersteller");
commands.append("?COMMANDS -> the server will send all the commands to the client");
commands.append("?QUIT");
}
public static void main(String[] args){
new Onlineshop_Server(null);
}
public void processNewConnection(String pClientIP, int pClientPort){
User temp = new User(pClientIP,pClientPort);
onlineusers.append(temp);
this.send(pClientIP,pClientPort,"Hallo Kunde. Sie sind jetzt mit dem Onlineshop verbunden. Bitte melden sie sich nun an oder erstellen einen neuen Account.");
}
public void processMessage(String pClientIP, int pClientPort, String pMessage){
String[] input = pMessage.split(":");
User temp = this.getuserbyIpandPort(pClientIP,pClientPort);
switch(input[0]){
case "?LOGIN" :
{
if (temp.getMyaccount()==null) {
Account foo = this.userbank.searchbyname(input[1]);
if (foo!=null) {
this.removeuserswithaccount(foo);
foo.clearbasket();
if (foo.getPasswort().equals(input[2])) {
temp.setMyaccount(foo);
this.send(pClientIP,pClientPort,"!LOGGEDIN");
} else {
this.send(pClientIP,pClientPort,"!LOGINERROR");
} // end of if-else
} else {
this.send(pClientIP,pClientPort,"!LOGINERROR");
} // end of if-else
} else {
this.send(pClientIP,pClientPort,"!LOGGEDIN");
} // end of if-else
}
break;
case "?BASKET" :
{
if (temp.getMyaccount()!=null) {
String back = "!BASKET"+this.Artikelformat(temp.getMyaccount().getBasket());
this.send(pClientIP,pClientPort,back);
} else {
this.send(pClientIP,pClientPort,"FAILURE");
} // end of if-else
}
break;
case "?SEARCH" :
{
String back = "!RESULTS" + this.Artikelformat(this.myitems.searchitem(input[1]));
this.send(pClientIP,pClientPort,back);
}
break;
case "?ADDTOBASKET" :
{
if (temp.getMyaccount()!=null) {
Artikel partikel = myitems.searchitemtbynumber(Integer.parseInt(input[1]));
temp.getMyaccount().addtobasket(partikel);
if (partikel!=null) {
this.send(pClientIP,pClientPort,"!ADDTOBASKET:SUCCESS:"+input[1]);
} else {
this.send(pClientIP,pClientPort,"!ADDTOBASKET:FALURE:"+input[1]);
} // end of if-else
} else {
this.send(pClientIP,pClientPort,"!ADDTOBASKET:FALURE:"+input[1]);
} // end of if-else
}
break;
case "?REMOVEFROMBASKET" :
{
if (temp.getMyaccount()!=null) {
temp.getMyaccount().removefrombasket(Integer.parseInt(input[1]));
this.send(pClientIP,pClientPort,"!REMOVEFROMBASKET:SUCCESS:"+input[1]);
} else {
this.send(pClientIP,pClientPort,"!REMOVEFROMBASKET:FAILURE:"+input[1]);
} // end of if-else
}
break;
case "?NEWACCOUNT" :
{
if (temp.getMyaccount()==null) {
userbank.adduser(input[1],input[2],input[3]);
Account foo = this.userbank.searchbyname(input[1]);
if (foo!=null) {
this.removeuserswithaccount(foo);
foo.clearbasket();
if (foo.getPasswort().equals(input[2])) {
temp.setMyaccount(foo);
this.send(pClientIP,pClientPort,"!NEWACCOUNT:SUCCESS:"+input[1]);
} else {
this.send(pClientIP,pClientPort,"!NEWACCOUNT:FAILURE:"+input[1]);
} // end of if-else
} else {
this.send(pClientIP,pClientPort,"!NEWACCOUNT:FAILURE:"+input[1]);
} // end of if-else
} else {
this.send(pClientIP,pClientPort,"!NEWACCOUNT:FAILURE:"+input[1]);
} // end of if-else
}
break;
case "?LOGOUT&BUY" :
{
if (temp.getMyaccount()!=null) {
List<Artikel> plis = temp.getMyaccount().getBasket();
plis.toFirst();
while (plis.hasAccess()) {
if(plis.getContent()!=null) this.myorders.bestellen(plis.getContent(),temp.getMyaccount());
plis.next();
} // end of while
temp.getMyaccount().clearbasket();
this.send(pClientIP,pClientPort,"!LOGOUT&BUY:SUCCESS");
this.closeConnection(pClientIP,pClientPort);
} else {
this.send(pClientIP,pClientPort,"!LOGOUT&BUY:SUCCESS");
this.closeConnection(pClientIP,pClientPort);
} // end of if-else
}
break;
case "?CLEARBASKET" :
if (temp.getMyaccount()!=null) {
temp.getMyaccount().clearbasket();
this.send(pClientIP,pClientPort,"!CLEARBASKET");
} // end of if
break;
case "?HISTORY" :
{
if (temp.getMyaccount()!=null) {
String back = "HISTORY:"+this.Bestellformat(this.myorders.allordersfromaccount(temp.getMyaccount()));
this.send(pClientIP,pClientPort,back);
} else {
this.send(pClientIP,pClientPort,"FAILURE");
} // end of if-else
}
break;
case "?NOTIFICATIONS" :
{
if(temp.getMyaccount()!=null){
String back = this.Notificationformat(temp.getMyaccount().getNotifications());
this.send(pClientIP,pClientPort,back);
}
}
break;
case "?DELETENOTIFICATIONS" :
if (temp.getMyaccount()!=null) {
temp.getMyaccount().deletenotifications();
this.send(pClientIP,pClientPort,"!DELETENOTIFICATIONS");
} else {
this.send(pClientIP,pClientPort,"FAILURE");
} // end of if-else
break;
case "?WISHLIST" :
{
if (temp.getMyaccount()!=null) {
String back = "!WISHLIST:" + this.Artikelformat(temp.getMyaccount().getWishlist());
this.send(pClientIP,pClientPort,back);
} else {
this.send(pClientIP,pClientPort,"FAILURE");
} // end of if-else
}
break;
case "?ADDTOWISHLIST" :
{
if (temp.getMyaccount()!=null) {
Artikel partikel = myitems.searchitemtbynumber(Integer.parseInt(input[1]));
temp.getMyaccount().addtowishlist(partikel);
if (partikel!=null) {
this.send(pClientIP,pClientPort,"!ADDTOWISHLIST:SUCCESS:"+input[1]);
} else {
this.send(pClientIP,pClientPort,"!ADDTOWISHLIST:FAILURE:"+input[1]);
} // end of if-else
} else {
this.send(pClientIP,pClientPort,"FAILURE");
} // end of if-else
}
break;
case "?REMOVEFROMWISHLIST" :
{
if (temp.getMyaccount()!=null) {
temp.getMyaccount().removefromwishlist(Integer.parseInt(input[1]));
this.send(pClientIP,pClientPort,"!REMOVEFROMWISHLIST:SUCCESS:"+input[1]);
} else {
this.send(pClientIP,pClientPort,"FAILURE");
} // end of if-else
}
break;
case "?CLEARWISHLIST" :
if (temp.getMyaccount()!=null) {
this.send(pClientIP,pClientPort,"!CLEARWISHLIST");
temp.getMyaccount().clearwishlist();
} else {
this.send(pClientIP,pClientPort,"FAILURE");
} // end of if-else
break;
case "?RECOMMEND" :
{
List<Artikel> recommended = this.myitems.nmostpopular(Integer.parseInt(input[1]));
String back ="!RECOMMENDED:" +this.Artikelformat(recommended);
this.send(pClientIP,pClientPort,back);
}
break;
case "?ARTIKEL" :
{
Artikel item = this.myitems.searchitemtbynumber(Integer.parseInt(input[1]));
List<Artikel> val = new List<Artikel>();
val.append(item);
String back = this.Artikelformat(val);
this.send(pClientIP,pClientPort,back);
}
break;
case "?ALLARTIKEL" :
{
String allartikel = "!ALLARTIKEL:"+this.Artikelformat(this.myitems.getMyitems());
this.send(pClientIP,pClientPort,allartikel);
}
break;
case "?COMMANDS" :
{
String sendback = this.getallcommands();
this.send(pClientIP,pClientPort,sendback);
}
break;
case "?QUIT" :
{
if (temp.getMyaccount()!=null) {
temp.getMyaccount().clearbasket();
} // end of if
this.closeConnection(pClientIP,pClientPort);
}
break;
default:
this.send(pClientIP,pClientPort,"-ERR");
break;
}
}
public void processClosingConnection(String pClientIP, int pClientPort){
this.onlineusers.toFirst();
while (onlineusers.hasAccess()) {
if (onlineusers.getContent().getIp().equals(pClientIP)&&onlineusers.getContent().getPort()==pClientPort) {
this.onlineusers.remove();
} else {
this.onlineusers.next();
} // end of if-elsef
} // end of while
this.send(pClientIP,pClientPort,"CONNECTION_TERMINATED");
}
public User getuserbyIpandPort(String ip, int port){
this.onlineusers.toFirst();
while (onlineusers.hasAccess()) {
if (onlineusers.getContent().getIp().equals(ip)&&onlineusers.getContent().getPort()==port) {
return onlineusers.getContent();
} // end of if
onlineusers.next();
} // end of while
return null;
}
public void removeuserswithaccount(Account pacc){
this.onlineusers.toFirst();
while (this.onlineusers.hasAccess()) {
if (this.onlineusers.getContent().getMyaccount()==pacc) {
onlineusers.remove();
} else {
onlineusers.next();
} // end of if-else
} // end of while
}
public String Artikelformat (List<Artikel> plis){
plis.toFirst();
String basket = "";
while (plis.hasAccess()) {
basket = basket + "!ARTIKEL:"+plis.getContent().getArtikelnummer() + "," + plis.getContent().getName()+","+plis.getContent().getBeschreibung()+","+plis.getContent().getPreis()+","+plis.getContent().getHersteller()+":";
plis.next();
} // end of while
return basket;
}
public String Bestellformat (List<Bestellung> plis){
plis.toFirst();
String basket = "";
while (plis.hasAccess()) {
basket = basket + "!BESTELLUNG:"+ plis.getContent().getArtikel().getArtikelnummer() + "," + plis.getContent().getArtikel().getName()+","+plis.getContent().getArtikel().getBeschreibung()+","+plis.getContent().getArtikel().getPreis()+","+plis.getContent().getArtikel().getHersteller()+":"+plis.getContent().getBestellungsnummer()+":";
plis.next();
} // end of while
return basket;
}
public String Notificationformat(List<String> plis){
plis.toFirst();
String notifications = "";
while (plis.hasAccess()) {
notifications = notifications + "!NOTIFICATION:"+ plis.getContent() + ":";
plis.next();
} // end of while
return notifications;
}
public void addproduct(String name, String beschreibung, String hersteller, int preis){
this.myitems.additem(name,preis,beschreibung,hersteller);
}
public void notifyall(){
}
public String getallcommands(){
this.commands.toFirst();
String commandsstring = "";
while (this.commands.hasAccess()) {
commandsstring = commandsstring + commands.getContent() + "\n";
this.commands.next();
} // end of while
return commandsstring;
}
public String allaccounts(){
String pstring = "username,passwort,email"+"\n";
List<Account> temp = this.userbank.getUsers();
temp.toFirst();
while (temp.hasAccess()) {
pstring=pstring + temp.getContent().getName()+","+temp.getContent().getPasswort()+","+temp.getContent().getEmail()+"\n";
temp.next();
} // end of while
return pstring;
}
public String allarticles(){
String pstring = "artikelnummer,name,beschreibung,preis,hersteller"+"\n";
List<Artikel> plis = this.myitems.getMyitems();
plis.toFirst();
while (plis.hasAccess()) {
pstring = pstring+ plis.getContent().getArtikelnummer() + "," + plis.getContent().getName()+","+plis.getContent().getBeschreibung()+","+plis.getContent().getPreis()+","+plis.getContent().getHersteller()+"\n";
plis.next();
} // end of while
return pstring;
}
} // end of Onlineshop_Server