-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVotingSystem.java
More file actions
659 lines (602 loc) · 21.5 KB
/
Copy pathVotingSystem.java
File metadata and controls
659 lines (602 loc) · 21.5 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
package UserPack;
import EstablishmentPack.AmericanRestaurant;
import EstablishmentPack.AsianRestaurant;
import EstablishmentPack.Cafes.*;
import EstablishmentPack.EuropeanRestaurant;
import EstablishmentPack.Restaurant;
import UserPack.Strategy.middleVoiceStrategy;
import javafx.scene.control.Alert;
import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VotingSystem implements Serializable {
/**
* Array of restaurants and cafes in the system
*/
private Restaurant[] restaurants;
private Cafe[] cafes;
/**
* List of voters registered in the system.
*/
private ArrayList<Voter> voters;
/**
* The host of the system.
*/
private Host host;
/**
* Number of users in the system.
*/
private int kolkoUser;
/**
* Number of restaurants in the system.
*/
private int kolkoRest;
/**
* Number of cafes in the system.
*/
private int kolkoCafes;
/**
* Index of the current user.
*/
private int akyUser;
/**
* List of votes for restaurants.
*/
private ArrayList<ArrayList<Boolean>> votes;
/**
* List of vote counts for restaurants.
*/
private ArrayList<ArrayList<Integer>> votesHod;
/**
* List of votes for cafes.
*/
private ArrayList<ArrayList<Boolean>> votesCafes;
/**
* List of vote counts for cafes.
*/
private ArrayList<ArrayList<Integer>> votesHodCafes;
/**
* Gets the list of votes for cafes.
* This method calculates the middle voice for cafes.
* See {@link UserPack.Strategy.calculateMidleVoiceCafe#calculateMidleVoice(VotingSystem, int)} for details.
*
* @return The list of votes for cafes.
*/
public ArrayList<ArrayList<Boolean>> getVotesCafes() {
return this.votesCafes;
}
/**
* Gets the list of vote counts for cafes.
* This method calculates the middle voice for cafes.
* See {@link UserPack.Strategy.calculateMidleVoiceCafe} for details.
*
* @return The list of vote counts for cafes.
*/
public ArrayList<ArrayList<Integer>> getVotesHodCafes() {
return this.votesHodCafes;
}
/**
* Gets the list of votes for restaurants.
* This method calculates the middle voice for restaurants.
* See {@link UserPack.Strategy.calculateMidleVoiceRest} for details.
*
* @return The list of votes for restaurants.
*/
public ArrayList<ArrayList<Boolean>> getVotes() {
return this.votes;
}
/**
* Gets the list of vote counts for restaurants.
* This method calculates the middle voice for restaurants.
* See {@link UserPack.Strategy.calculateMidleVoiceRest} for details.
*
* @return The list of vote counts for restaurants.
*/
public ArrayList<ArrayList<Integer>> getVotesHod() {
return this.votesHod;
}
/**
* Gets the number of restaurants in the system.
* @return The number of restaurants.
*/
public int getKolkoRest(){
return this.kolkoRest;
}
/**
* Gets the number of cafes in the system.
* @return The number of cafes.
*/
public int getKolkoCafes(){
return this.kolkoCafes;
}
/**
* Gets the index of the current user.
* @return The index of the current user.
*/
public int getAkyUser(){
return this.akyUser;
}
/**
* Gets the host of the system.
* @return The host of the system.
*/
public Host getHost(){
return this.host;
}
/**
* Gets the voter at the specified index.
* @param i The index of the voter.
* @return The voter at the specified index.
*/
public Voter getVoter(int i){
return this.voters.get(i);
}
/**
* Gets the voter corresponding to the current user index.
* @return The voter corresponding to the current user index.
*/
public Voter voterJe(){
return this.voters.get(this.akyUser);
}
/**
* Gets the restaurant at the specified index.
* @param i The index of the restaurant.
* @return The restaurant at the specified index.
*/
public Restaurant getRestaurant(int i){
return this.restaurants[i];
}
/**
* Gets the cafe at the specified index.
* @param i The index of the cafe.
* @return The cafe at the specified index.
*/
public Cafe getCafe(int i){
return this.cafes[i];
}
/**
*Constructor for building a voting system
* It internally calls {@link #creatingObjects()} to initialize the system.
* */
public VotingSystem(){
creatingObjects();
}
/**
* Initializes the objects of the voting system.
* Sets up the necessary data structures and creates users, hosts, restaurants, and cafes.
* {@code for (int i = 0; i < kolkoUser; i++)} Creating users, restaurants, and cafes
* Finally, I have a function to sort cafes and restaurants by their name {@link #sortRestaurantsAndCafesByName()}
* and a function that uses <T> {@link #sortBy(List, Comparator)}
*/
public void creatingObjects(){
kolkoUser=3;
kolkoRest=3;
kolkoCafes=3;
voters=new ArrayList<>();
host=new Host("host","666");
restaurants=new Restaurant[kolkoRest];
cafes=new Cafe[kolkoCafes];
votes = new ArrayList<>();
votesHod = new ArrayList<>();
votesCafes = new ArrayList<>();
votesHodCafes = new ArrayList<>();
for (int i = 0; i < kolkoUser; i++) {
ArrayList<Boolean> userVotesCafe= new ArrayList<>();
ArrayList<Integer> userVotesHodCafe = new ArrayList<>();
ArrayList<Boolean> userVotes = new ArrayList<>();
ArrayList<Integer> userVotesHod = new ArrayList<>();
for (int j = 0; j < kolkoRest; j++) {
userVotes.add(false);
userVotesHod.add(0);
}
for (int j = 0; j < kolkoCafes; j++) {
userVotesCafe.add(false);
userVotesHodCafe.add(0);
}
votesCafes.add(userVotesCafe);
votesHodCafes.add(userVotesHodCafe);
votes.add(userVotes);
votesHod.add(userVotesHod);
}
for (int i=0;i<kolkoUser;i++){
if (i==0){
voters.add(new SimpleVoter("aaa", "111"));
restaurants[i]=new AmericanRestaurant("American Restaurant");
cafes[i]=new CatCafes("Cat Cafes");
}else if(i==1){
voters.add(new SimpleVoter("bbb", "222"));
restaurants[i]=new EuropeanRestaurant("European Restaurant");
cafes[i]=new LamaCafes("Lama Cafes");
} else {
voters.add(new ExpertVoter("ccc", "333"));
restaurants[i]=new AsianRestaurant("Asian Restaurant");
cafes[i]=new ConfectioneryCafes("Confectionery Cafes");
}
}
sortRestaurantsAndCafesByName();
sortBy(voters, Comparator.comparing(Voter::getUsername));
}
/**
* Sorts the restaurants and cafes by name.
* use of lambda expressions
*/
public void sortRestaurantsAndCafesByName() {
Arrays.sort(restaurants, (r1, r2) -> r1.getNameRestaurant().compareToIgnoreCase(r2.getNameRestaurant()));
Arrays.sort(cafes, (c1, c2) -> c1.getNameCafe().compareToIgnoreCase(c2.getNameCafe()));
}
/**
* Sorts the list using the specified comparator.
* This method provides flexibility in sorting lists of different types of
* objects by different comparison criteria.
*
* @param list the list to be sorted
* @param comparator a comparator that determines the order of items in the list
* (use {@link java.util.Comparator} to create comparators)
* @param <T> type of items in the list
*/
public <T> void sortBy(List<T> list, Comparator<? super T> comparator) {
list.sort(comparator);
}
/**
* Registers a new user in the system.
* @param username The username of the new user.
* @param password The password of the new user.
* @param expert Indicates if the new user is an expert or not.
* @return True if registration is successful, false otherwise.
*/
public boolean registrationCrtUsr(String username,String password,boolean expert){
for (int i = 0; i < kolkoUser; i++) {
Voter voter = voters.get(i);
if (voter.getUsername().equals(username)) {
return false;
}
}
kolkoUser++;
if (expert){
voters.add(new ExpertVoter(username, password));
}else {
voters.add(new SimpleVoter(username, password));
}
ArrayList<Boolean> newVotesCafe = new ArrayList<>(kolkoCafes);
ArrayList<Integer> newVotesHodCafe = new ArrayList<>(kolkoCafes);
ArrayList<Boolean> newVotes = new ArrayList<>(kolkoRest);
ArrayList<Integer> newVotesHod = new ArrayList<>(kolkoRest);
for (int i = 0; i < kolkoRest; i++) {
newVotes.add(false);
newVotesHod.add(0);
}
for (int i = 0; i < kolkoCafes; i++) {
newVotesCafe.add(false);
newVotesHodCafe.add(0);
}
votesCafes.add(newVotesCafe);
votesHodCafes.add(newVotesHodCafe);
votes.add(newVotes);
votesHod.add(newVotesHod);
return true;
}
/**
* Checks the host's login credentials.
* @param username The username entered by the host.
* @param password The password entered by the host.
* @return True if the login is successful, false otherwise.
*/
public boolean loginCheckHost(String username,String password){
if (host.login(username, password)) {
this.akyUser = -2;
return true;
}
return false;
}
/**
* Checks the login credentials of a user.
* @param username The username entered by the user.
* @param password The password entered by the user.
* @return True if the login is successful, false otherwise.
*/
public boolean loginCheck(String username,String password){
for (int i = 0; i < voters.size(); i++) {
Voter voter = voters.get(i);
if (voter.login(username, password)) {
this.akyUser = i;
return true;
}
}
return false;
}
/**
* Checks if a given name corresponds to a cafe in the system.
* @param name The name to be checked.
* @return True if the name corresponds to a cafe, false otherwise.
*/
public boolean isCafe(String name){
for (int i = 0; i < getKolkoCafes(); i++) {
if (name.equals(cafes[i].getNameCafe())){
return true;
}
}
return false;
}
/**
* Calculates the rating of a restaurant or cafe based on user votes.
* explicit use of RTTI
* @param hodnota The value of the vote.
* @param akyVoterDalHolos The index of the user who voted.
* @param akyrestoran The index of the restaurant or cafe being voted for.
* @param isCafe Indicates if the entity being voted for is a cafe.
* @return The calculated rating of the restaurant or cafe.
*/
public float hodnoteRest(int hodnota,int akyVoterDalHolos,int akyrestoran, boolean isCafe){
float result;
int kolkoHolosovali=0;
int sumHolosov=0;
if (isCafe){
for (int i = 0; i < kolkoUser; i++) {
if (votesCafes.get(i).get(akyrestoran)) {
if (voters.get(i) instanceof SimpleVoter) {
kolkoHolosovali++;
sumHolosov += votesHodCafes.get(i).get(akyrestoran);
}else {
kolkoHolosovali+=voters.get(i).getKoefHol();
sumHolosov += votesHodCafes.get(i).get(akyrestoran);
}
}
}
}else {
for (int i = 0; i < kolkoUser; i++) {
if (votes.get(i).get(akyrestoran)) {
if (voters.get(i) instanceof SimpleVoter) {
kolkoHolosovali++;
sumHolosov += votesHod.get(i).get(akyrestoran);
}else {
kolkoHolosovali+=voters.get(i).getKoefHol();
sumHolosov += votesHod.get(i).get(akyrestoran);
}
}
}
}
int hodnotaEx;
if (!hasVoted(akyVoterDalHolos, akyrestoran,isCafe)) {
if (voters.get(akyVoterDalHolos) instanceof SimpleVoter) {
if (isCafe)
{
votesHodCafes.get(akyVoterDalHolos).set(akyrestoran, hodnota);
}else
{
votesHod.get(akyVoterDalHolos).set(akyrestoran, hodnota);
}
sumHolosov += hodnota;
kolkoHolosovali++;
vote(akyVoterDalHolos, akyrestoran,isCafe);
}else {
hodnotaEx=hodnota*voters.get(akyVoterDalHolos).getKoefHol();
if (isCafe)
{
votesHodCafes.get(akyVoterDalHolos).set(akyrestoran, hodnotaEx);
}else
{
votesHod.get(akyVoterDalHolos).set(akyrestoran, hodnotaEx);
}
sumHolosov += hodnotaEx;
kolkoHolosovali+=voters.get(akyVoterDalHolos).getKoefHol();
vote(akyVoterDalHolos, akyrestoran,isCafe);
}
}
if (kolkoHolosovali!=0) {
result = sumHolosov / (float)kolkoHolosovali;
}else {
result=hodnota;
}
return result;
}
/**
* Records a user's vote for a restaurant or cafe.
* @param userId The index of the user.
* @param restaurantId The index of the restaurant or cafe.
* @param isCafe Indicates if the entity being voted for is a cafe.
*/
public void vote(int userId, int restaurantId,boolean isCafe) {
if (isCafe){
votesCafes.get(userId).set(restaurantId, true);
}else {
votes.get(userId).set(restaurantId, true);
}
}
/**
* Checks if a user has already voted for a restaurant or cafe.
* @param userId The index of the user.
* @param restaurantId The index of the restaurant or cafe.
* @param isCafe Indicates if the entity being voted for is a cafe.
* @return True if the user has already voted, false otherwise.
*/
public boolean hasVoted(int userId, int restaurantId,boolean isCafe) {
if (isCafe){
return votesCafes.get(userId).get(restaurantId);
}else {
return votes.get(userId).get(restaurantId);
}
}
/**
* Generates statistics on the voting activity.
* @return A string containing the voting statistics.
*/
public String setStatistics(){
String result= "";
int u=0;
for (int i = 0; i < kolkoUser; i++) {
u++;
}
result+="Users: "+u+"\n---------------\n";
int kolkoDalHol=0;
for (int i = 0; i < kolkoRest; i++) {
for (int j = 0; j < kolkoUser; j++) {
if (hasVoted(j,i,false)){
kolkoDalHol++;
}
}
result+=restaurants[i].getNameRestaurant()+":\t"+kolkoDalHol+"\\"+u+"\n";
kolkoDalHol=0;
}
result+="---------------\n";
kolkoDalHol=0;
for (int i = 0; i < kolkoCafes; i++) {
for (int j = 0; j < kolkoUser; j++) {
if (hasVoted(j,i,true)){
kolkoDalHol++;
}
}
result+=cafes[i].getNameCafe()+":\t"+kolkoDalHol+"\\"+u+"\n";
kolkoDalHol=0;
}
return result;
}
/**
* Saves the current state of the system to a file.
* @throws ClassNotFoundException If the class of the loaded object cannot be found.
* @throws IOException If an I/O error occurs while reading from the file.
*/
public void save()throws ClassNotFoundException, IOException {
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("holosovanie.out"));
out.writeObject(this);
out.close();
}
/**
* Loads a previously saved state of the system from a file.
* @throws ClassNotFoundException If the class of the loaded object cannot be found.
* @throws IOException If an I/O error occurs while reading from the file.
*/
public void load() throws ClassNotFoundException, IOException {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("holosovanie.out"));
VotingSystem loadedHolosovanie = (VotingSystem) in.readObject();
in.close();
restaurants = loadedHolosovanie.restaurants;
cafes = loadedHolosovanie.cafes;
voters = loadedHolosovanie.voters;
host = loadedHolosovanie.host;
kolkoUser = loadedHolosovanie.kolkoUser;
kolkoRest = loadedHolosovanie.kolkoRest;
kolkoCafes = loadedHolosovanie.kolkoCafes;
akyUser = loadedHolosovanie.akyUser;
votes = loadedHolosovanie.votes;
votesHod = loadedHolosovanie.votesHod;
votesCafes = loadedHolosovanie.votesCafes;
votesHodCafes = loadedHolosovanie.votesHodCafes;
} catch (IOException | ClassNotFoundException e) {
Alert a = new Alert(Alert.AlertType.ERROR);
System.out.println(e.getMessage());
a.setTitle("Error in the file ");
a.setContentText("Problems with reading from a file.");
a.showAndWait();
e.printStackTrace();
}
}
/**
* Resets all votes and ratings to their initial state.
*/
public void obnulyt(){
for (int i = 0; i < votes.size(); i++) {
ArrayList<Boolean> row = votes.get(i);
for (int j = 0; j < row.size(); j++) {
row.set(j, false);
}
}
for (int i = 0; i < votesHod.size(); i++) {
ArrayList<Integer> row = votesHod.get(i);
for (int j = 0; j < row.size(); j++) {
row.set(j, 0);
}
}
for (int i = 0; i < votesCafes.size(); i++) {
ArrayList<Boolean> row = votesCafes.get(i);
for (int j = 0; j < row.size(); j++) {
row.set(j, false);
}
}
for (int i = 0; i < votesHodCafes.size(); i++) {
ArrayList<Integer> row = votesHodCafes.get(i);
for (int j = 0; j < row.size(); j++) {
row.set(j, 0);
}
}
for (int i = 0; i < kolkoRest; i++) {
restaurants[i].setHodnotaRew(0);
}
for (int i = 0; i < kolkoCafes; i++) {
cafes[i].setHodnotaRew(0);
}
for (int i = 0; i < kolkoUser; i++) {
getVoter(i).getMiddleVoiceAgr().setMiddleVoiceRest(0);
getVoter(i).getMiddleVoiceAgr().setMiddleVoiceCafe(0);
}
}
/**
* Sets the calculation strategy for the average voting.
* @param strategy The strategy to be used for calculation.
* @param u The number of users.
*/
public void setcalculatVoice(middleVoiceStrategy strategy,int u){
strategy.calculateMidleVoice(this,u);
}
/**
* Calculates the average voting figures for restaurants.
* @return The calculated average voting figure for restaurants.
*/
public float calculateMidleVoicefiguresRestResultObserver(){
float sumAllRest=0;
int numberOfVotesRest = 0;
for (int i = 0; i < kolkoUser; i++) {
if (voters.get(i).getMiddleVoiceAgr().getMiddleVoiceRest()!=0){
numberOfVotesRest++;
}
sumAllRest+=voters.get(i).getMiddleVoiceAgr().getMiddleVoiceRest();
}
float res;
if (numberOfVotesRest==0){
res=0;
}else {
res = sumAllRest/numberOfVotesRest;
res=Math.round(res*10)/10.0f;
}
return res;
}
/**
* Calculates the average voting figures for cafes.
* @return The calculated average voting figure for cafes.
*/
public float calculateMidleVoicefiguresCafeResultObserver(){
float sumAllCafe=0;
int numberOfVotesCafe = 0;
for (int i = 0; i < kolkoUser; i++) {
if (voters.get(i).getMiddleVoiceAgr().getMiddleVoiceCafe()!=0){
numberOfVotesCafe++;
}
sumAllCafe+=voters.get(i).getMiddleVoiceAgr().getMiddleVoiceCafe();
}
float res;
if (numberOfVotesCafe==0){
res=0;
}else {
res = sumAllCafe/numberOfVotesCafe;
res=Math.round(res*10)/10.0f;
}
return res;
}
/**
* Checks if the password contains at least one Latin letters.
* @param password The password to be checked.
* @throws checkingForSymbols if the password not contains at least one Latin symbols.
*/
public void CheckRightPaseord(String password) throws checkingForSymbols {
String regex = ".*[a-zA-Z].*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(password);
if (!matcher.matches()) {
throw new checkingForSymbols();
}
}
}