-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGiftExchangeGUI.java
765 lines (615 loc) · 22.7 KB
/
GiftExchangeGUI.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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
/* Written by
Seth Roffe
*/
package giftexchange;
import java.util.*;
import java.io.File;
import javafx.application.Application;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.scene.text.*;
import javafx.scene.paint.*;
import javafx.stage.Stage;
import javafx.geometry.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Message.*;
public class GiftExchangeGUI {
/** Performs a gift exchange pairing. Inputs include the number of people
participating which must be greater than 2 people and the names of
those people. The outputs are files that are named for the buyer
containing a string that explains who that person is buying for.
*/
public static void main(String[] args) throws FileNotFoundException,AddressException,MessagingException {
Scanner input = new Scanner(System.in);
JFrame inputFrame = new JFrame("Gift Exchange");
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
boolean useEmails = UsingEmails();
if (useEmails) {
EmailMethod();
}
else {
FileMethod();
}
}
public static boolean isValidEmail(String email){
//Check if valid email address
Pattern pattern = Pattern.compile("[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}");
Matcher mat = pattern.matcher(email);
if(mat.matches()){
return true;
}
else{
return false;
}
}
public static String[] getEmailPswd() {
boolean getEmail = true;
String[] tmp = {"",""};
Object[] nextDone = {"OK","Cancel"}; //button labels
do {
JPanel userPanel = new JPanel();
JTextField username = new JTextField(40);
username.setPreferredSize(new Dimension(100,50));
username.setFont(new Font("Arial", Font.PLAIN,30));
JPasswordField passwd = new JPasswordField(40);
passwd.setPreferredSize(new Dimension(100,50));
passwd.setFont(new Font("Arial", Font.PLAIN,30));
JLabel userLabel = new JLabel("Email Username: ");
userLabel.setPreferredSize(new Dimension(400,50));
userLabel.setFont(new Font("Arial",Font.BOLD,24));
JLabel passLabel = new JLabel("Password: ");
passLabel.setPreferredSize(new Dimension(400,50));
passLabel.setFont(new Font("Arial",Font.BOLD,24));
userPanel.setLayout(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();
grid.gridx = 0;
grid.gridy = 0;
grid.anchor = GridBagConstraints.WEST;
userPanel.add(userLabel,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
userPanel.add(username,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
userPanel.add(passLabel,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
userPanel.add(passwd,grid);
int listPressed = JOptionPane.showOptionDialog(null,
userPanel, //message
"Gift Exchange - Host Email", //title
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, //no custom icon
nextDone, //title of buttons
username //default button
);
if (listPressed == JOptionPane.YES_OPTION) {
if (username.getText().length() == 0 || !isValidEmail(username.getText())) {
//if Next was pushed but nothing was entered
JLabel errMessage = new JLabel("You need to input a valid email address");
errMessage.setFont(new Font("Arial",Font.BOLD,32));
JOptionPane.showMessageDialog(null,
errMessage,
"Invalid Host email",
JOptionPane.WARNING_MESSAGE
);
getEmail = true;
}
else if ( passwd.getPassword().length == 0 ) {
JLabel errMessage = new JLabel("You need to input a password.");
errMessage.setFont(new Font("Arial",Font.BOLD,32));
JOptionPane.showMessageDialog(null,
errMessage,
"No password inputted",
JOptionPane.WARNING_MESSAGE
);
getEmail = true;
}
else {
getEmail = false;
String user = username.getText();
String pass = new String(passwd.getPassword());
String[] userpass = {user,pass};
return userpass;
}
}
else if (listPressed == JOptionPane.NO_OPTION){
//cancel == cancel the program
System.exit(0);
}
else if (listPressed == JOptionPane.CLOSED_OPTION) {
System.exit(0);
}
} while (getEmail);
return tmp;
}
public static void EmailPeople(String[] userpass, ArrayList<String> names, ArrayList<String> emails, ArrayList<Integer> exchange) throws FileNotFoundException,AddressException, MessagingException{
try {
String pathToJar = GiftExchangeGUI.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String path = new File(pathToJar).getParentFile().getPath();
for (int i = 0; i < names.size(); i++) {
if (emails.get(i) == "") {
//write files for people who don't have emails
PrintWriter output = new PrintWriter(path+"/"+names.get(i)+".txt");
//Map number in shuffled integer array to original names array.
output.print("You are buying for "+names.get(exchange.get(i))+".");
output.close();
}
else {
//email the people
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");//"465"); // smtp port
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userpass[0], userpass[1]);
}
});
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userpass[0]));
msg.setSubject("Gift Exchange Person Output");
msg.setRecipient(RecipientType.TO, new InternetAddress(emails.get(i)));
//add atleast simple body
MimeBodyPart body = new MimeBodyPart();
body.setText("You are buying for "+names.get(exchange.get(i))+".");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(body);
msg.setContent(multipart);
Transport.send(msg);
}
}
}
catch (IOException ioe) {
System.out.println("There was an IO exception.");
}
catch (Exception e) {
e.printStackTrace();
System.out.println("There was an error");
}
}
public static boolean UsingEmails() {
String[] yesNo = {"Yes","No"};
JPanel userPanel = new JPanel();
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("ARIAL",Font.PLAIN,36)));
JLabel label = new JLabel("Would you like to automate the emailing?");
label.setPreferredSize(new Dimension(800,50));
label.setFont(new Font("Arial",Font.BOLD,24));
JLabel notice = new JLabel("Note: Only for gmail host email");
notice.setPreferredSize(new Dimension(400,50));
notice.setFont(new Font("Arial",Font.BOLD,24));
userPanel.setLayout(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();
grid.gridx = 0;
grid.gridy = 0;
grid.anchor = GridBagConstraints.WEST;
userPanel.add(label,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
userPanel.add(notice,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
int pressed = JOptionPane.showOptionDialog(null,
userPanel, //message
"Gift Exchange - Host Email", //title
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //no custom icon
yesNo, //title of buttons
JOptionPane.YES_OPTION //default button
);
if (pressed == JOptionPane.YES_OPTION) {
return true;
}
else if (pressed == JOptionPane.NO_OPTION) {
return false;
}
else if (pressed == JOptionPane.CLOSED_OPTION) {
System.exit(0);
}
return true;
}
public static void EmailMethod() throws FileNotFoundException,AddressException,MessagingException {
//get the list of people's names and store it into an arraylist of strings
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> emails = new ArrayList<String>();
//enlarge buttons
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("ARIAL",Font.PLAIN,36)));
boolean inputNames = true;
int count = 1;
String[] userpass = getEmailPswd();
String username = userpass[0];
String password = userpass[1];
// Get the list of people
do {
JLabel emailLabel = new JLabel("Person " + (count) + "'s email is (No email will make a file for the person):");
emailLabel.setPreferredSize(new Dimension(100,100));
emailLabel.setFont(new Font("Arial",Font.BOLD,34));
JLabel messageLabel = new JLabel("Person "+(count)+" is:");
messageLabel.setPreferredSize(new Dimension(100,100));
messageLabel.setFont(new Font("Arial",Font.BOLD,36));
JPanel peoplePanel = new JPanel();
Object[] nextDone = {"Next","Done"}; //button labels
JTextField textField = new JTextField(40); //input
textField.setPreferredSize(new Dimension(100,50));
textField.setFont(new Font("Arial",Font.PLAIN,30));
JTextField emailField = new JTextField(40); //email input
emailField.setPreferredSize(new Dimension(100,50));
emailField.setFont(new Font("Arial",Font.PLAIN,30));
//panel.add(textField);
JTextArea currentList = new JTextArea(20,10); //Where the names are listed
currentList.setFont(new Font("Arial",Font.PLAIN,24));
JLabel currentLabel = new JLabel("The current list is: ");
currentLabel.setPreferredSize(new Dimension(400,50));
currentLabel.setFont(new Font("Arial",Font.BOLD,24));
for (int i = 0; i < names.size(); i++) {
currentList.append(names.get(i)+"\t" + emails.get(i)+"\n");
}
currentList.setEditable(false); //make the list read-only
JScrollPane scrollPeoplePane = new JScrollPane(currentList);
scrollPeoplePane.setPreferredSize(new Dimension(100,250));
peoplePanel.setLayout(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();
grid.gridx = 0;
grid.gridy = 0;
grid.anchor = GridBagConstraints.WEST;
peoplePanel.add(currentLabel,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
peoplePanel.add(currentList,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
peoplePanel.add(messageLabel,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
peoplePanel.add(textField,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
peoplePanel.add(emailLabel,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
peoplePanel.add(emailField,grid);
int optionPressed = JOptionPane.showOptionDialog(null,
peoplePanel, //message
"Gift Exchange", //title
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //no custom icon
nextDone, //title of buttons
textField //default button
);
// Add the person to the list whilst checking for errors
String person;
String email;
if (optionPressed == JOptionPane.YES_OPTION) {
person = textField.getText();
email = emailField.getText();
if (person.length() == 0) {
//if Next was pushed but nothing was entered
JLabel errMessage = new JLabel("You need to input a person\'s name");
errMessage.setFont(new Font("Arial",Font.BOLD,32));
JOptionPane.showMessageDialog(null,
errMessage,
"No Person inputted",
JOptionPane.WARNING_MESSAGE
);
}
else if ( !isValidEmail(email) && email.length() != 0 ) {
JLabel errMessage = new JLabel("That is not a valid email address");
errMessage.setFont(new Font("Arial", Font.BOLD, 32));
JOptionPane.showMessageDialog(null,
errMessage,
"Invalid email",
JOptionPane.WARNING_MESSAGE
);
}
else {
count++;
names.add(person);
if (email.length() == 0) {
emails.add("");
}
else {
emails.add(email);
}
inputNames = true;
}
}
else if (optionPressed == JOptionPane.NO_OPTION){
// Done pushed - If nothing entered on last person, ignore that entry
person = textField.getText();
email = emailField.getText();
if (person.length() == 0) {
inputNames = false;
}
else {
names.add(person);
if (email.length() == 0) {
emails.add("");
}
else {
emails.add(email);
}
inputNames = false;
}
}
else if (optionPressed == JOptionPane.CLOSED_OPTION) {
//exiting by closing the window
System.exit(0);
}
// Check the names in the list
if (!inputNames) {
Object[] confirmList = {"Yes","No"};
JPanel listPanel = new JPanel();
listPanel.setPreferredSize(new Dimension(400,400));
JLabel listLabel = new JLabel("The names in the list are: ");
listLabel.setPreferredSize(new Dimension(400,50));
listLabel.setFont(new Font("Arial",Font.BOLD,24));
JLabel correctLabel = new JLabel("Is this correct?");
correctLabel.setPreferredSize(new Dimension(400,50));
correctLabel.setFont(new Font("Arial",Font.BOLD,24));
JTextArea listOfPeople = new JTextArea(25,25); //Where the names are listed
listOfPeople.setFont(new Font("Arial",Font.PLAIN,24));
for (int i = 0; i < names.size(); i++) {
listOfPeople.append(names.get(i)+" - "+ emails.get(i)+"\n");
}
listOfPeople.setEditable(false); //make the list read-only
JScrollPane scrollPane = new JScrollPane(listOfPeople);
scrollPane.setPreferredSize(new Dimension(100,250));
listPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
//format the window
listPanel.add(listLabel,gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy++;
listPanel.add(scrollPane,gbc);
gbc.fill= GridBagConstraints.HORIZONTAL;
gbc.gridy++;
listPanel.add(correctLabel,gbc);
int listPressed = JOptionPane.showOptionDialog(null,
listPanel, //message
"Gift Exchange", //title
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, //no custom icon
confirmList, //title of buttons
confirmList[0] //default button
);
if (listPressed == JOptionPane.YES_OPTION) {
//if yes was pushed, just continue on
}
else if (listPressed == JOptionPane.NO_OPTION){
//start from scratch if no was pushed
inputNames = true;
names.clear();
emails.clear();
count = 1;
}
else if (listPressed == JOptionPane.CLOSED_OPTION) {
System.exit(0);
}
}
} while (inputNames);
//make an arrayList of integers to map people to
ArrayList<Integer> exchange = new ArrayList<Integer>();
for (int i = 0; i<names.size(); i++) {
exchange.add(i);
}
//Don't want people to get themselves
boolean getSelf = true;
do {
getSelf = false;
//shuffles the array of integers
Collections.shuffle(exchange);
for (int i = 0; i<names.size(); i++) {
if (exchange.get(i) == i) {
getSelf = true;
}
}
} while (getSelf);
JLabel doneLabel = new JLabel("Done. Files emailed. If there was no email a file was created containing who that person is buying for.");
doneLabel.setFont(new Font("Arial",Font.PLAIN,34));
JOptionPane.showMessageDialog(null,
doneLabel,
"Done Matching",
JOptionPane.INFORMATION_MESSAGE
);
//write files and email people
EmailPeople(userpass,names,emails,exchange);
}
public static void FileMethod() throws FileNotFoundException {
Scanner input = new Scanner(System.in);
JFrame inputFrame = new JFrame("Gift Exchange");
inputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//get the list of people's names and store it into an arraylist of strings
ArrayList<String> names = new ArrayList<String>();
//enlarge buttons
UIManager.put("OptionPane.buttonFont", new FontUIResource(new Font("ARIAL",Font.PLAIN,36)));
boolean inputNames = true;
int count = 1;
// Get the list of people
do {
JLabel messageLabel = new JLabel("Person "+(count)+" is:");
messageLabel.setPreferredSize(new Dimension(100,100));
messageLabel.setFont(new Font("Arial",Font.BOLD,36));
JPanel peoplePanel = new JPanel();
//panel.add(messageLabel);
Object[] nextDone = {"Next","Done"}; //button labels
JTextField textField = new JTextField(30); //input
textField.setPreferredSize(new Dimension(100,50));
textField.setFont(new Font("Arial",Font.PLAIN,30));
//panel.add(textField);
JTextArea currentList = new JTextArea(20,10); //Where the names are listed
currentList.setFont(new Font("Arial",Font.PLAIN,24));
JLabel currentLabel = new JLabel("The current list is: ");
currentLabel.setPreferredSize(new Dimension(400,50));
currentLabel.setFont(new Font("Arial",Font.BOLD,24));
for (int i = 0; i < names.size(); i++) {
currentList.append(names.get(i)+"\n");
}
currentList.setEditable(false); //make the list read-only
JScrollPane scrollPeoplePane = new JScrollPane(currentList);
scrollPeoplePane.setPreferredSize(new Dimension(100,250));
peoplePanel.setLayout(new GridBagLayout());
GridBagConstraints grid = new GridBagConstraints();
grid.gridx = 0;
grid.gridy = 0;
grid.anchor = GridBagConstraints.WEST;
peoplePanel.add(currentLabel,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
peoplePanel.add(currentList,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
peoplePanel.add(messageLabel,grid);
grid.fill = GridBagConstraints.HORIZONTAL;
grid.gridy++;
peoplePanel.add(textField,grid);
int optionPressed = JOptionPane.showOptionDialog(null,
peoplePanel, //message
"Gift Exchange", //title
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, //no custom icon
nextDone, //title of buttons
textField //default button
);
// Add the person to the list whilst checking for errors
String person;
if (optionPressed == JOptionPane.YES_OPTION) {
person = textField.getText();
if (person.length() == 0) {
//if Next was pushed but nothing was entered
JLabel errMessage = new JLabel("You need to input a person\'s name");
errMessage.setFont(new Font("Arial",Font.BOLD,32));
JOptionPane.showMessageDialog(null,
errMessage,
"No Person inputted",
JOptionPane.WARNING_MESSAGE
);
}
else {
count++;
names.add(person);
inputNames = true;
}
}
else if (optionPressed == JOptionPane.NO_OPTION){
// Done pushed - If nothing entered on last person, ignore that entry
person = textField.getText();
if (person.length() == 0) {
inputNames = false;
}
else {
names.add(person);
inputNames = false;
}
}
else if (optionPressed == JOptionPane.CLOSED_OPTION) {
//exiting by closing the window
System.exit(0);
}
// Check the names in the list
if (!inputNames) {
Object[] confirmList = {"Yes","No"};
JPanel listPanel = new JPanel();
listPanel.setPreferredSize(new Dimension(400,400));
JLabel listLabel = new JLabel("The names in the list are: ");
listLabel.setPreferredSize(new Dimension(400,50));
listLabel.setFont(new Font("Arial",Font.BOLD,24));
JLabel correctLabel = new JLabel("Is this correct?");
correctLabel.setPreferredSize(new Dimension(400,50));
correctLabel.setFont(new Font("Arial",Font.BOLD,24));
JTextArea listOfPeople = new JTextArea(25,25); //Where the names are listed
listOfPeople.setFont(new Font("Arial",Font.PLAIN,24));
for (int i = 0; i < names.size(); i++) {
listOfPeople.append(names.get(i)+"\n");
}
listOfPeople.setEditable(false); //make the list read-only
JScrollPane scrollPane = new JScrollPane(listOfPeople);
scrollPane.setPreferredSize(new Dimension(100,250));
listPanel.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
//format the window
listPanel.add(listLabel,gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy++;
listPanel.add(scrollPane,gbc);
gbc.fill= GridBagConstraints.HORIZONTAL;
gbc.gridy++;
listPanel.add(correctLabel,gbc);
int listPressed = JOptionPane.showOptionDialog(null,
listPanel, //message
"Gift Exchange", //title
JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE,
null, //no custom icon
confirmList, //title of buttons
confirmList[0] //default button
);
if (listPressed == JOptionPane.YES_OPTION) {
//if yes was pushed, just continue on
}
else if (listPressed == JOptionPane.NO_OPTION){
//start from scratch if no was pushed
inputNames = true;
names.clear();
count = 1;
}
else if (listPressed == JOptionPane.CLOSED_OPTION) {
System.exit(0);
}
}
} while (inputNames);
//make an arrayList of integers to map people to
ArrayList<Integer> exchange = new ArrayList<Integer>();
for (int i = 0; i<names.size(); i++) {
exchange.add(i);
}
//Don't want people to get themselves
boolean getSelf = true;
do {
getSelf = false;
//shuffles the array of integers
Collections.shuffle(exchange);
for (int i = 0; i<names.size(); i++) {
if (exchange.get(i) == i) {
getSelf = true;
}
}
} while (getSelf);
JLabel doneLabel = new JLabel("Done. File names are the buyer\n File contains who buyer is buying for.");
doneLabel.setFont(new Font("Arial",Font.PLAIN,34));
JOptionPane.showMessageDialog(null,
doneLabel,
"Done Matching",
JOptionPane.INFORMATION_MESSAGE
);
//write files
try {
String pathToJar = GiftExchangeGUI.class.getProtectionDomain().getCodeSource().getLocation().getPath();
String path = new File(pathToJar).getParentFile().getPath();
for (int i = 0; i<names.size(); i++) {
PrintWriter output = new PrintWriter(path+"/"+names.get(i)+".txt");
//Map number in shuffled integer array to original names array.
output.print("You are buying for "+names.get(exchange.get(i))+".");
output.close();
}
}
catch (IOException ioe) {
System.out.println("There was an IO exception.");
}
}
}