· 9 years ago · Apr 06, 2017, 03:06 AM
1
2import org.eclipse.swt.widgets.Display;
3import org.eclipse.swt.widgets.Shell;
4import org.eclipse.swt.widgets.Text;
5import org.eclipse.swt.SWT;
6import org.eclipse.swt.widgets.Label;
7import org.eclipse.swt.widgets.Button;
8import org.eclipse.swt.events.SelectionAdapter;
9import org.eclipse.swt.events.SelectionEvent;
10import org.eclipse.swt.widgets.Combo;
11import org.eclipse.swt.widgets.List;
12
13import java.util.*;
14import java.util.regex.Pattern;
15import java.util.regex.Matcher;
16import java.net.*;
17import java.text.*;
18import java.lang.*;
19import java.io.*;
20import java.sql.*;
21import java.awt.*;
22
23
24public class ProjectPart2 {
25
26 public int custID;
27 public String selectedBook;
28 public String bookTitle4String;
29 public String bookYear;
30 public String bookLanguage;
31 public String bookWeight;
32 public String custClub;
33 public double minimumPrice;
34 protected Shell shell;
35 private Text loginText;
36 private Text bookTitle;
37 private Text lowestPrice;
38 private Text quantity;
39 private Text totalText;
40 private static Connection myConnection;
41 private static String url;
42 private Text promptText;
43 private Label customerInfo;
44 private Combo catCombo;
45 private List bookList;
46 private static Pattern bookInfo = Pattern.compile("Title: (.+) Year: (.+) Language: (.+) Weight: (.+)");
47 public DateFormat df = new SimpleDateFormat("yyyy-mm-dd-HH.mm.ss");
48 public Calendar calobj = Calendar.getInstance();
49 public int quantityI;
50
51
52 /**
53 * Launch the application.
54 * @param args
55 */
56 public static void main(String[] args) {
57 try
58 {
59 //Register the driver with the driver manager.
60 Class.forName("com.ibm.db2.jcc.DB2Driver").newInstance();
61 }
62 catch (ClassNotFoundException e)
63 {
64 e.printStackTrace();
65 }
66 catch (InstantiationException e)
67 {
68 e.printStackTrace();
69 }
70 catch (IllegalAccessException e)
71 {
72 e.printStackTrace();
73 }
74
75 //Give the url for the db
76 url = "jdbc:db2:c3421m";
77
78 try
79 {
80 myConnection = DriverManager.getConnection(url);
81 }
82 catch (SQLException e)
83 {
84 System.out.println("SQL: database connection error.\n");
85 }
86
87
88 try {
89 ProjectPart2 window = new ProjectPart2();
90 window.open();
91 } catch (Exception e) {
92 e.printStackTrace();
93 }
94
95 }
96
97 /**
98 * Open the window.
99 */
100 public void open() {
101 Display display = Display.getDefault();
102 createContents();
103 shell.open();
104 shell.layout();
105 while (!shell.isDisposed()) {
106 if (!display.readAndDispatch()) {
107 display.sleep();
108 }
109 }
110 }
111
112 /**
113 * Create contents of the window.
114 */
115 protected void createContents() {
116 shell = new Shell();
117 shell.setSize(700, 550);
118 shell.setText("SWT Application");
119
120 loginText = new Text(shell, SWT.BORDER);
121 loginText.setBounds(28, 53, 150, 24);
122
123 Label lblUserId = new Label(shell, SWT.NONE);
124 lblUserId.setBounds(28, 27, 69, 21);
125 lblUserId.setText("Customer ID");
126
127 Button loginButton = new Button(shell, SWT.NONE);
128 loginButton.addSelectionListener(new SelectionAdapter() {
129 @Override
130 public void widgetSelected(SelectionEvent e) {
131 find_customer(Integer.parseInt(loginText.getText()));
132 }
133 });
134 loginButton.setBounds(184, 53, 85, 24);
135 loginButton.setText("Login");
136
137
138 catCombo = new Combo(shell, SWT.NONE);
139 catCombo.setBounds(28, 128, 150, 24);
140
141 Label lblCategory = new Label(shell, SWT.NONE);
142 lblCategory.setBounds(28, 101, 69, 21);
143 lblCategory.setText("Category");
144
145 bookTitle = new Text(shell, SWT.BORDER);
146 bookTitle.setBounds(226, 128, 273, 24);
147
148 Label lblBookTitle = new Label(shell, SWT.NONE);
149 lblBookTitle.setBounds(226, 101, 69, 21);
150 lblBookTitle.setText("Book Title");
151
152 bookList = new List(shell, SWT.BORDER);
153 bookList.addSelectionListener(new SelectionAdapter() {
154 @Override
155 public void widgetSelected(SelectionEvent e) {
156 selectedBook = bookList.getItem(bookList.getFocusIndex());
157 Matcher m = bookInfo.matcher(selectedBook);
158 bookTitle4String = m.group(0);
159 bookYear = m.group(1);
160 bookLanguage = m.group(2);
161 bookWeight = m.group(3);
162 min_price();
163 }
164 });
165 bookList.setBounds(28, 188, 555, 125);
166
167 Button btnSearch = new Button(shell, SWT.NONE);
168 btnSearch.addSelectionListener(new SelectionAdapter() {
169 @Override
170 public void widgetSelected(SelectionEvent e) {
171 find_book();
172 }
173 });
174 btnSearch.setBounds(518, 127, 85, 26);
175 btnSearch.setText("Search");
176
177 Label lblListOfBooks = new Label(shell, SWT.NONE);
178 lblListOfBooks.setBounds(28, 161, 85, 21);
179 lblListOfBooks.setText("List of Books");
180
181 lowestPrice = new Text(shell, SWT.BORDER);
182 lowestPrice.setBounds(589, 232, 85, 24);
183 lowestPrice.setEditable(false);
184
185 Label lblLowestPrice = new Label(shell, SWT.NONE);
186 lblLowestPrice.setBounds(586, 205, 88, 21);
187 lblLowestPrice.setText("Lowest Price");
188
189 quantity = new Text(shell, SWT.BORDER);
190 quantity.setBounds(28, 376, 86, 24);
191
192
193 Label lblWhatIsThe = new Label(shell, SWT.NONE);
194 lblWhatIsThe.setBounds(27, 349, 356, 21);
195 lblWhatIsThe.setText("Quantity");
196
197 totalText = new Text(shell, SWT.BORDER);
198 totalText.setBounds(28, 443, 85, 24);
199 totalText.setEditable(false);
200
201 Label lblTotalPrice = new Label(shell, SWT.NONE);
202 lblTotalPrice.setBounds(28, 416, 69, 21);
203 lblTotalPrice.setText("Total Price");
204
205 Button purchaseBtn = new Button(shell, SWT.NONE);
206 purchaseBtn.setBounds(159, 442, 85, 26);
207 purchaseBtn.setText("Purchase!");
208 purchaseBtn.addSelectionListener(new SelectionAdapter() {
209 @Override
210 public void widgetSelected(SelectionEvent e) {
211 insert_purchase();
212 }
213 });
214
215 Label lblUserPrompt = new Label(shell, SWT.NONE);
216 lblUserPrompt.setBounds(315, 10, 85, 21);
217 lblUserPrompt.setText("User Prompt");
218
219 promptText = new Text(shell, SWT.BORDER);
220 promptText.setBounds(315, 37, 359, 40);
221 promptText.setEditable(false);
222 promptText.setText("Enter your customer id and press login to login");
223
224 customerInfo = new Label(shell, SWT.NONE);
225 customerInfo.setBounds(381, 402, 293, 94);
226 customerInfo.setText("");
227
228 Label lblCustomerInfo = new Label(shell, SWT.NONE);
229 lblCustomerInfo.setBounds(381, 376, 118, 21);
230 lblCustomerInfo.setText("Customer Info");
231
232 Button quantityButton = new Button(shell, SWT.NONE);
233 quantityButton.setBounds(127, 376, 85, 26);
234 quantityButton.setText("Select");
235 quantityButton.addSelectionListener(new SelectionAdapter() {
236 @Override
237 public void widgetSelected(SelectionEvent e) {
238 quantityI = Integer.parseInt(quantity.getText());
239 double price = Double.parseDouble(lowestPrice.getText());
240 String totalSPrice = Double.toString(quantityI * price);
241 totalText.setText(totalSPrice);
242 promptText.setText("Review the price and press the purchase button to purchase your books");
243
244 }
245 });
246
247
248 }
249
250 private void setText(String string) {
251 // TODO Auto-generated method stub
252
253 }
254
255 public boolean find_customer(int input) {
256 String queryText = ""; // The SQL text.
257 PreparedStatement querySt = null; // The query handle.
258 ResultSet answers = null; // A cursor.
259 String output;
260
261 boolean inDB = false; // Return.
262
263 queryText =
264 "SELECT cid, name, city "
265 + "FROM yrb_customer "
266 + "WHERE cid = ? ";
267
268 // Prepare the query.
269 try {
270 querySt = myConnection.prepareStatement(queryText);
271 } catch(SQLException e) {
272 System.out.println("SQL#1 failed in prepare");
273 System.out.println(e.toString());
274 System.exit(0);
275 }
276
277 // Execute the query.
278 try {
279 querySt.setInt(1, input);
280 answers = querySt.executeQuery();
281 } catch(SQLException e) {
282 System.out.println("SQL#1 failed in execute");
283 System.out.println(e.toString());
284 System.exit(0);
285 }
286
287 // Any answer?
288 try {
289 if (answers.next()) {
290 inDB = true;
291 output = "cid: " + answers.getString("cid") + "\nName: " + answers.getString("name") + "\nCity: " + answers.getString("city");
292 customerInfo.setText(output);
293 } else {
294 inDB = false;
295 promptText.setText("There is no customer with that id \n Please enter your customer id and try again");;
296 }
297 } catch(SQLException e) {
298 System.out.println("SQL#1 failed in cursor.");
299 System.out.println(e.toString());
300 System.exit(0);
301 }
302
303 // Close the cursor.
304 try {
305 answers.close();
306 } catch(SQLException e) {
307 System.out.print("SQL#1 failed closing cursor.\n");
308 System.out.println(e.toString());
309 System.exit(0);
310 }
311
312 // We're done with the handle.
313 try {
314 querySt.close();
315 } catch(SQLException e) {
316 System.out.print("SQL#1 failed closing the handle.\n");
317 System.out.println(e.toString());
318 System.exit(0);
319 }
320
321 return inDB; }
322
323
324 public boolean fetch_categories() {
325 String queryText = ""; // The SQL text.
326 PreparedStatement querySt = null; // The query handle.
327 ResultSet answers = null; // A cursor.
328 String output;
329
330 boolean inDB = false; // Return.
331
332 queryText =
333 "SELECT cat "
334 + "FROM yrb_category ";
335
336 // Prepare the query.
337 try {
338 querySt = myConnection.prepareStatement(queryText);
339 } catch(SQLException e) {
340 System.out.println("SQL#1 failed in prepare");
341 System.out.println(e.toString());
342 System.exit(0);
343 }
344
345 // Execute the query.
346 try {
347 answers = querySt.executeQuery();
348 } catch(SQLException e) {
349 System.out.println("SQL#1 failed in execute");
350 System.out.println(e.toString());
351 System.exit(0);
352 }
353
354 // Any answer?
355 try {
356 if (answers.next()) {
357 inDB = true;
358 catCombo.add(answers.getString("cat"));
359 while(answers.next())
360 {
361 catCombo.add(answers.getString("cat"));
362 }
363 promptText.setText("Select a category and enter the title of a book to search for");
364 } else {
365 inDB = false;
366 promptText.setText("There are no categories to display.");
367 }
368 } catch(SQLException e) {
369 System.out.println("SQL#1 failed in cursor.");
370 System.out.println(e.toString());
371 System.exit(0);
372 }
373
374 // Close the cursor.
375 try {
376 answers.close();
377 } catch(SQLException e) {
378 System.out.print("SQL#1 failed closing cursor.\n");
379 System.out.println(e.toString());
380 System.exit(0);
381 }
382
383 // We're done with the handle.
384 try {
385 querySt.close();
386 } catch(SQLException e) {
387 System.out.print("SQL#1 failed closing the handle.\n");
388 System.out.println(e.toString());
389 System.exit(0);
390 }
391
392 return inDB; }
393
394
395 public boolean find_book() {
396 String queryText = ""; // The SQL text.
397 PreparedStatement querySt = null; // The query handle.
398 ResultSet answers = null; // A cursor.
399 String output;
400
401 boolean inDB = false; // Return.
402
403 queryText =
404 "SELECT title, year, language, weight "
405 + "FROM yrb_book "
406 + "WHERE cat = ? AND title = ? ";
407
408 // Prepare the query.
409 try {
410 querySt = myConnection.prepareStatement(queryText);
411 } catch(SQLException e) {
412 System.out.println("SQL#1 failed in prepare");
413 System.out.println(e.toString());
414 System.exit(0);
415 }
416
417 // Execute the query.
418 try {
419 querySt.setString(1, catCombo.getText());
420 querySt.setString(2, bookTitle.getText());
421 answers = querySt.executeQuery();
422 } catch(SQLException e) {
423 System.out.println("SQL#1 failed in execute");
424 System.out.println(e.toString());
425 System.exit(0);
426 }
427
428 // Any answer?
429 try {
430 if (answers.next()) {
431 inDB = true;
432 output = "Title: " + answers.getString("title") + " Year: " + answers.getString("year") + " Language: " + answers.getString("language") + " Weight: " + answers.getString("weight");
433 bookList.add(output);
434 while(answers.next())
435 {
436 output = "Title: " + answers.getString("title") + " Year: " + answers.getString("year") + " Language: " + answers.getString("language") + " Weight: " + answers.getString("weight");
437 bookList.add(output);
438 }
439 } else {
440 inDB = false;
441 promptText.setText("There are no books in that category with that title, Please enter a new query and search again.");;
442 }
443 } catch(SQLException e) {
444 System.out.println("SQL#1 failed in cursor.");
445 System.out.println(e.toString());
446 System.exit(0);
447 }
448
449 // Close the cursor.
450 try {
451 answers.close();
452 } catch(SQLException e) {
453 System.out.print("SQL#1 failed closing cursor.\n");
454 System.out.println(e.toString());
455 System.exit(0);
456 }
457
458 // We're done with the handle.
459 try {
460 querySt.close();
461 } catch(SQLException e) {
462 System.out.print("SQL#1 failed closing the handle.\n");
463 System.out.println(e.toString());
464 System.exit(0);
465 }
466
467 return inDB; }
468
469
470 public boolean min_price() {
471
472 String queryText = ""; // The SQL text.
473 PreparedStatement querySt = null; // The query handle.
474 ResultSet answers = null; // A cursor.
475 String output;
476
477 boolean inDB = false; // Return.
478
479 queryText =
480 "SELECT min(price), "
481 + "FROM (SELECT * FROM yrb_offer AS O, (SELECT club FROM yrb_member m WHERE m.cid = ?) AS c "
482 + "WHERE O.title = ? AND O.year = ? AND O.club = c.club ) ";
483
484 // Prepare the query.
485 try {
486 querySt = myConnection.prepareStatement(queryText);
487 } catch(SQLException e) {
488 System.out.println("SQL#1 failed in prepare");
489 System.out.println(e.toString());
490 System.exit(0);
491 }
492
493 // Execute the query.
494 try {
495 querySt.setInt(1, custID);
496 querySt.setString(2, bookTitle4String);
497 querySt.setString(3, bookYear);
498 answers = querySt.executeQuery();
499 } catch(SQLException e) {
500 System.out.println("SQL#1 failed in execute");
501 System.out.println(e.toString());
502 System.exit(0);
503 }
504
505 // Any answer?
506 try {
507 if (answers.next()) {
508 inDB = true;
509 output = answers.getString("1");
510 lowestPrice.setText(output);
511 minimumPrice = Double.parseDouble(output);
512 promptText.setText("Enter the amount of books you want to buy in the quantity text field\n"
513 + "and then press the select button to calculate total");
514 } else {
515 inDB = false;
516 promptText.setText("Something has gone terribly wrong");
517 }
518 } catch(SQLException e) {
519 System.out.println("SQL#1 failed in cursor.");
520 System.out.println(e.toString());
521 System.exit(0);
522 }
523
524 // Close the cursor.
525 try {
526 answers.close();
527 } catch(SQLException e) {
528 System.out.print("SQL#1 failed closing cursor.\n");
529 System.out.println(e.toString());
530 System.exit(0);
531 }
532
533 // We're done with the handle.
534 try {
535 querySt.close();
536 } catch(SQLException e) {
537 System.out.print("SQL#1 failed closing the handle.\n");
538 System.out.println(e.toString());
539 System.exit(0);
540 }
541
542 return inDB; }
543
544
545 public boolean insert_purchase() {
546 String queryText = ""; // The SQL text.
547 PreparedStatement querySt = null; // The query handle.
548 int answers = 0; // A int.
549 String output;
550
551 boolean inDB = false; // Return.
552
553 queryText =
554 "INSERT INTO yrb_purchase (cid, club, title, year, when, quantity) "
555 + "VALUES (?, ?, ?, ?, ?, ?)";
556
557 // Prepare the query.
558 try {
559 querySt = myConnection.prepareStatement(queryText);
560 } catch(SQLException e) {
561 System.out.println("SQL#1 failed in prepare");
562 System.out.println(e.toString());
563 System.exit(0);
564 }
565
566 // Execute the query.
567 try {
568 querySt.setInt(1, custID);
569 querySt.setString(2, custClub);
570 querySt.setString(3, bookTitle4String);
571 querySt.setString(4, bookYear);
572 querySt.setString(5, df.format(calobj.getTime()));
573 querySt.setInt(6, quantityI);
574 answers = querySt.executeUpdate();
575 } catch(SQLException e) {
576 System.out.println("SQL#1 failed in execute");
577 System.out.println(e.toString());
578 System.exit(0);
579 }
580
581 // Any answer?
582 if (answers == 1) {
583 inDB = true;
584 promptText.setText("YOU HAVE SUCCESSFULLY PURCHASED THE BOOKS");
585 } else {
586 inDB = false;
587 promptText.setText("SOMETHING WENT WRONG PURCHASING THE BOOKS");;
588 }
589 // We're done with the handle.
590 try {
591 querySt.close();
592 } catch(SQLException e) {
593 System.out.print("SQL#1 failed closing the handle.\n");
594 System.out.println(e.toString());
595 System.exit(0);
596 }
597
598 return inDB; }
599
600
601 public boolean find_club() {
602 String queryText = ""; // The SQL text.
603 PreparedStatement querySt = null; // The query handle.
604 ResultSet answers = null; // A cursor.
605 String output;
606
607 boolean inDB = false; // Return.
608
609 queryText =
610 "SELECT min(price), "
611 + "FROM (SELECT club FROM yrb_offer AS O, (SELECT club AS cub FROM yrb_member m WHERE m.cid = ?) AS c "
612 + "WHERE O.title = ? AND O.year = ? AND O.club = c.cub AND price = ? ) ";
613
614 // Prepare the query.
615 try {
616 querySt = myConnection.prepareStatement(queryText);
617 } catch(SQLException e) {
618 System.out.println("SQL#1 failed in prepare");
619 System.out.println(e.toString());
620 System.exit(0);
621 }
622
623 // Execute the query.
624 try {
625 querySt.setInt(1, custID);
626 querySt.setString(2, bookTitle4String);
627 querySt.setString(3, bookYear);
628 querySt.setDouble(4, minimumPrice);
629 answers = querySt.executeQuery();
630 } catch(SQLException e) {
631 System.out.println("SQL#1 failed in execute");
632 System.out.println(e.toString());
633 System.exit(0);
634 }
635
636 // Any answer?
637 try {
638 if (answers.next()) {
639 inDB = true;
640 output = "cid: " + answers.getString("cid") + "\nName: " + answers.getString("name") + "\nCity: " + answers.getString("city");
641 customerInfo.setText(output);
642 } else {
643 inDB = false;
644 promptText.setText("There is no customer with that id \n Please enter your customer id and try again");;
645 }
646 } catch(SQLException e) {
647 System.out.println("SQL#1 failed in cursor.");
648 System.out.println(e.toString());
649 System.exit(0);
650 }
651
652 // Close the cursor.
653 try {
654 answers.close();
655 } catch(SQLException e) {
656 System.out.print("SQL#1 failed closing cursor.\n");
657 System.out.println(e.toString());
658 System.exit(0);
659 }
660
661 // We're done with the handle.
662 try {
663 querySt.close();
664 } catch(SQLException e) {
665 System.out.print("SQL#1 failed closing the handle.\n");
666 System.out.println(e.toString());
667 System.exit(0);
668 }
669
670 return inDB; }
671
672
673 public void run()
674 {
675
676 //Everything is set up so we lay out the main procedure here
677
678 promptText.setText("Hello!");
679
680 //*******"Login" take in the customer id, if it does not exist prompt the user and try again
681 //*******If the customer exists login and display customer id, name and city
682
683 //*******Also if the customer exists populate drop down with all categories
684
685 //*******After the customer chooses a category the customer can enter a title of the book
686 //*******Here we return the title, year language and wieght
687 //*******There may be more than one book here so we put these in a list and let the user select
688 //*******Which book they want
689
690 //*******If no books appear the user can search again
691
692 //*******After the user selects a book we output the minimum price
693 //*******The user might belong to different clubs with different prices
694
695 //*******The user then enters the number of books they want to buy and we calculate
696 //*******The total price
697
698 //We ask the user if they want to buy the books and then they purchase
699 //When they purchase the books we need to insert a tuple into the purchase table
700 }
701}