-
Notifications
You must be signed in to change notification settings - Fork 1
/
library_management.java
672 lines (642 loc) · 17.9 KB
/
library_management.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
import java.util.*;
import java.io.*;
import java.sql.*;
import java.util.concurrent.*;
import com.mysql.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.*;
interface OnCallBack
{
void onCallBack(String s);
}
public class library_management
{
public static Map<String,String> result = new HashMap<>();
public static String Address,Subject,Message;
public static void main(String [] args)
{
Subject = "Book is Available";
Message = "The book you had requested for is available now";
JFrame frame = new JFrame("LIBRARY");
frame.add( new server(args));
//frame.getContentPane().setBackground(Color.DARK_GRAY);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //to close when exit button is pressed
frame.setExtendedState(JFrame.MAXIMIZED_BOTH); //set screen with given size
frame.setVisible(true);
}
}
class server extends JComponent
implements MouseListener,ActionListener
{
String [] args;
user<Integer> uid1 = null;
user<String> uid2 = null;
library<Double> obj1 = null;
Student student = null;
Books books = null;
Fine fine = null;
JsoupGoogleSearch search = null;
HyperlinkDemo link = null;
IssueRequest request = null;
SwingEmailSender ses = null;
OnCallBack ocb = null;
ThreadPool tp = null;
JButton newUser,addInfo,buy,returnBook,Trends,yourBooks,issueRequest;
JPanel panel,panel1,panel2,panel3,panel4,panel5,panel6,panel7,panel8,panel9,panel10,panel11,panel13,img_pan;
JTable table;
Connection con = null;
JScrollPane scrollPane;
public server(String [] args)
{
this.args = args;
try
{
panel = new JPanel();
panel.setBounds(0,0,250,1000);
panel.setBackground(Color.BLACK);
panel1 = new JPanel();
panel1.setBounds(0, 0, 250, 50);
panel1.setBackground(new Color(165,70,30));
panel2 = new JPanel();
panel2.setBounds(250, 0, 1440, 5);
panel2.setBackground(Color.BLACK);
panel3 = new JPanel();
panel3.setBounds(0, 60, 250, 50);
panel3.setBackground(new Color(165,70,30));
panel4 = new JPanel();
panel4.setBounds(950,0,600,350);
panel4.setBackground(Color.LIGHT_GRAY);
panel5 = new JPanel();
panel5.setBounds(0,120,250,50);
panel5.setBackground(new Color(165,70,30));
panel7 = new JPanel();
panel7.setBounds(0,180,250,50);
panel7.setBackground(new Color(165,70,30));
panel9 = new JPanel();
panel9.setBounds(0,240,250,50);
panel9.setBackground(new Color(165,70,30));
panel11 = new JPanel();
panel11.setBounds(0,300,250,50);
panel11.setBackground(new Color(165,70,30));
panel13 = new JPanel();
panel13.setBounds(0,360,250,50);
panel13.setBackground(new Color(165,70,30));
img_pan = new JPanel();
img_pan.setBounds(0,0,1920,1080);
newUser = new JButton("New User");
//newUser.setBounds(0, 0, 100, 30);
addInfo = new JButton("Add Information");
//addInfo.setBounds(0,60,200,30);
buy = new JButton("Issue a Book");
//buy.setBounds(0,100,100,30);
returnBook = new JButton("Return a book");
//returnBook.setBounds(0, 400, 100, 30);
Trends = new JButton("Current Trends");
//Trends.setBounds(0,);
yourBooks = new JButton("Your Books");
issueRequest = new JButton("Issue Request");
panel1.add(newUser);
panel3.add(addInfo);
panel5.add(buy);
panel7.add(returnBook);
panel9.add(Trends);
panel11.add(yourBooks);
panel13.add(issueRequest);
add(panel1);
add(panel2);
add(panel3);
add(panel4);
//add(panel10);
add(panel5);
add(panel7);
add(panel9);
add(panel11);
add(panel13);
add(panel);
panel4.setVisible(false);
newUser.addActionListener(this);
addInfo.addActionListener(this);
buy.addActionListener(this);
returnBook.addActionListener(this);
Trends.addActionListener(this);
yourBooks.addActionListener(this);
issueRequest.addActionListener(this);
addMouseListener(this);
try {
BufferedImage myPicture = ImageIO.read(new File("resized_background.jpg"));
JLabel picLabel = new JLabel(new ImageIcon(myPicture));
picLabel.setBounds(0, 0, 1440, 1080);
img_pan.add(picLabel);
}
catch(Exception E) {
E.printStackTrace();
}
add(img_pan);
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/assignments","root","password");
obj1 = new library<Double>(con);
//Scanner in = new Scanner(System.in);
uid1 = new user<Integer>(con);
uid2 = new user<String>(con);
ocb = new thread();
tp = new ThreadPool(ocb);
student = new Student(con);
books = new Books(con);
fine = new Fine(con);
search = new JsoupGoogleSearch();
request = new IssueRequest(con);
ses = new SwingEmailSender();
}
catch(Exception e)
{
System.out.println(e);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == newUser)
{
String n = JOptionPane.showInputDialog(null, "User Name : ");
String t = JOptionPane.showInputDialog(null, "Id type?\n1.Integer\n2.String\n\n");
if(t.equals("Integer"))
{
String t1 = JOptionPane.showInputDialog(null,"Id : ");
Integer id = Integer.parseInt(t1);
try
{
uid1.new_user(n, id, tp, t);
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else if(t.equals("String"))
{
String t1 = JOptionPane.showInputDialog(null,"Id : ");
try
{
uid2.new_user(n, t1, tp, t);
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
else
{
JOptionPane.showMessageDialog(null, "Invalid Input");
}
}
else if(e.getSource() == buy)
{
int count = 0;
try
{
ResultSet cnt = this.obj1.display(con);
while(cnt.next())
{
count++;
}
}
catch (SQLException e2)
{
// TODO Auto-generated catch block
e2.printStackTrace();
}
//JFrame tFrame = new JFrame("BOOKS");
String column[] = {"Name","Author","Price","Stock"};
String data[][] = new String[count][4];
int i = 0;
try
{
ResultSet rs = this.obj1.display(this.con);
while(rs.next())
{
data[i][0] = rs.getString(1);
data[i][1] = rs.getString(2);
data[i][2] = rs.getString(3);
data[i][3] = rs.getString(4);
i++;
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
JTable table = new JTable(data,column);
table.setBounds(30,40,700,1000);
table.setBackground(Color.LIGHT_GRAY);
JScrollPane sp = new JScrollPane(table);
sp.setBounds(850, 0, 400, 1000);
//tFrame.getContentPane().add(sp);
//tFrame.setSize(700, 700);
//tFrame.setVisible(true);
//tFrame.setLocation(800, 40);
//tFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
panel4.add(sp);
panel4.setVisible(true);
String name = JOptionPane.showInputDialog(null,"Enter Book name : ");
String stud_id = JOptionPane.showInputDialog(null, "Enter student id : ");
String stud_name = JOptionPane.showInputDialog(null, "Enter student name : ");
try
{
if(student.insert(stud_id, stud_name, con))
{
String ans = this.obj1.buy(name, this.con);
JOptionPane.showMessageDialog(null,ans);
String check = "Book not available";
if(ans.equalsIgnoreCase(check))
{
student.delete(stud_id, con);
}
else
{
System.out.println(1);
String msg = books.insert(name, stud_id, con);
String msg2 = "The return date is : "+msg;
JOptionPane.showMessageDialog(null, msg2);
}
}
else
{
String ans = "You have maximum number of books issued, cannot issue another";
JOptionPane.showMessageDialog(null,ans);
}
}
catch(Exception ex)
{
}
panel4.remove(sp);
panel4.setVisible(false);
}
else if(e.getSource()==addInfo)
{
try
{
String n = JOptionPane.showInputDialog(null, "User Name : ");
String t = JOptionPane.showInputDialog(null, "Id type?\n1.Integer\n2.String\n\n");
if(t.equals("Integer"))
{
String t1 = JOptionPane.showInputDialog(null,"Id : ");
Integer id = Integer.parseInt(t1);
if(uid1.exists(n,id,t))
{
String b = JOptionPane.showInputDialog(null, "Enter book Name : ");
String a = JOptionPane.showInputDialog(null, "Enter author : ");
String n1 = JOptionPane.showInputDialog(null, "Enter price : ");
String n2 = JOptionPane.showInputDialog(null, "Enter stock : ");
String c = JOptionPane.showInputDialog(null, "Enter category : ");
Integer p=0;
Double s=0.0;
if(n2!=null)
{
p = Integer.parseInt(n2);
}
if(n1!=null)
{
s = Double.parseDouble(n1);
}
obj1.accept(b,a,s,p,c);
}
}
else if(t.equals("String"))
{
String t1 = JOptionPane.showInputDialog(null,"Id : ");
if(uid2.exists(n,t1,t))
{
String b = JOptionPane.showInputDialog(null, "Enter book Name : ");
String a = JOptionPane.showInputDialog(null, "Enter author : ");
String n1 = JOptionPane.showInputDialog(null, "Enter price : ");
String n2 = JOptionPane.showInputDialog(null, "Enter stock : ");
String c = JOptionPane.showInputDialog(null, "Enter category : ");
Integer p=0;
Double s=0.0;
if(n2!=null)
{
p = Integer.parseInt(n2);
}
if(n1!=null)
{
s = Double.parseDouble(n1);
}
obj1.accept(b,a,s,p,c);
}
}
else
{
JOptionPane.showMessageDialog(null, "Invalid Input");
}
}
catch(SQLException s1)
{
s1.printStackTrace();
}
}
else if(e.getSource() == returnBook)
{
String id = JOptionPane.showInputDialog(null,"Enter your id : ");
String b = JOptionPane.showInputDialog(null,"Enter Book name : ");
try
{
Statement stmt = con.createStatement();
// String s = "select category from library where name = '"+b+"';";
// ResultSet rs = stmt.executeQuery(s);
// try
// {
// if(rs.next())
// {
// library_management.result = search.getLinks(rs.getString(1));
// library_management.category = rs.getString(1);
// }
// }
// catch (IOException e1)
// {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
Double f = fine.fine(id, b, con);
if(f!=0)
{
String s1 = "You have crossed the return date. Please pay fine of "+f+" rupees";
JOptionPane.showMessageDialog(null, s1);
}
JOptionPane.showMessageDialog(null, "Book returned successfully");
obj1.update(b);
books.delete(b, id, con);
request.sendEmail(b, con);
if(library_management.Address.length()!=0)
{
ses.send();
}
library_management.Address = "";
// link = new HyperlinkDemo();
// link.launchStage(args);
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
else if(e.getSource() == Trends)
{
String s = JOptionPane.showInputDialog(null,"Enter category : ");
try
{
library_management.result = search.getLinks(s);
}
catch (IOException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
HyperlinkDemo link = new HyperlinkDemo();
link.launchStage(args);
}
else if(e.getSource() == yourBooks)
{
String id = JOptionPane.showInputDialog(null,"Enter id : ");
try
{
Statement stmt = con.createStatement();
String s = "select * from books where stud_id = '"+id+"';";
ResultSet rs = stmt.executeQuery(s);
String ans = "";
while(rs.next())
{
ans = ans + "Book : "+rs.getString(1)+" return date : "+rs.getString(3)+"\n";
}
if(ans.length()!=0)
{
JOptionPane.showMessageDialog(null, ans);
}
else
{
JOptionPane.showMessageDialog(null, "No Books Issued");
}
}
catch (SQLException e1)
{
e1.printStackTrace();
}
}
else if(e.getSource() == issueRequest)
{
String id = JOptionPane.showInputDialog(null,"Enter id : ");
String b = JOptionPane.showInputDialog(null,"Enter book name : ");
try
{
request.issue(con, id, b);
}
catch (SQLException e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
class library<T>
{
Connection con = null;
public library(Connection con) throws SQLException
{
this.con = con;
Statement stmt = con.createStatement();
stmt.executeUpdate("create table if not exists library(name varchar(20),author varchar(20),price float,stock int);");
}
public void accept(String s1, String s2, Double d, Integer i,String c) throws SQLException
{
// TODO Auto-generated method stub
Statement pre = con.createStatement();
String s3 = "select price,stock from library where name = '"+s1+"';";
ResultSet rs = pre.executeQuery(s3);
if(rs.next())
{
Statement stmt = con.createStatement();
String s = "update library set stock = "+(i+rs.getInt(2))+" where name = '"+s1+"';";
stmt.executeUpdate(s);
}
else
{
Statement stmt = con.createStatement();
String s = "insert into library values('"+s1+"','"+s2+"',"+d+","+i+",'"+c+"');";
stmt.executeUpdate(s);
}
}
public String buy(String b,Connection con)throws SQLException
{
// TODO Auto-generated method stub
Statement stmt = con.createStatement();
String s1 = "select price,stock from library where name = '"+b+"';";
ResultSet rs = stmt.executeQuery(s1);
//System.out.println(1);
if(rs.next())
{
double price = rs.getDouble(1);
int copies = rs.getInt(2) - 1;
String s2 = "update library set stock = "+copies+" where name = '"+b+"';";
Statement stmt2 = con.createStatement();
stmt2.executeUpdate(s2);
// String s3 = "delete from library where stock = 0;";
// Statement stmt3 = con.createStatement();
// stmt3.executeUpdate(s3);
return ("Book has been issued");
}
else
{
return ("Book not available");
}
}
public ResultSet display(Connection con) throws SQLException
{
// TODO Auto-generated method stub
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select * from library;");
return rs;
/*int cnt = 0;
while(rs.next())
{
System.out.println(rs.getString(1)+"\t"+rs.getString(2)+"\t"+rs.getDouble(3)+"\t"+rs.getInt(4));
cnt++;
}
if(cnt==0)
return false;
return true;*/
}
public void update(String b)throws SQLException
{
Statement stmt1 = con.createStatement();
String s = "update library set stock = stock + 1 where name = '"+b+"';";
stmt1.executeUpdate(s);
}
}
class user<T>
{
Connection con = null;
public user(Connection con) throws SQLException
{
this.con = con;
Statement stmt = con.createStatement();
stmt.executeUpdate("Create table if not exists userInt(uname varchar(20),uid int);");
Statement stmt1 = con.createStatement();
stmt1.executeUpdate("Create table if not exists userStr(uname varchar(20),uid varchar(20));");
}
public void new_user(String s, T t,ThreadPool tp,String type) throws SQLException
{
if(type.equals("Integer"))
{
Statement pre = con.createStatement();
String s3 = "select * from userInt where uname = '"+s+"';";
ResultSet rs = pre.executeQuery(s3);
if(rs.next())
{
JOptionPane.showMessageDialog(null,"Username already exists!!");
}
else
{
Statement stmt = con.createStatement();
String s1 = "insert into userInt values('"+s+"', "+t+");";
stmt.executeUpdate(s1);
}
}
else
{
Statement pre = con.createStatement();
String s3 = "select * from userStr where uname = '"+s+"';";
ResultSet rs = pre.executeQuery(s3);
if(rs.next())
{
JOptionPane.showMessageDialog(null,"Username already exists!!");
}
else
{
Statement stmt = con.createStatement();
String s1 = "insert into userStr values('"+s+"', '"+t+"');";
stmt.executeUpdate(s1);
}
}
}
public boolean exists(String s, T t, String type)throws SQLException
{
boolean ans = false;
if(type.equals("Integer"))
{
Statement stmt = con.createStatement();
String s1 = "select * from userInt where uname = '"+s+"';";
ResultSet rs = stmt.executeQuery(s1);
if(rs.next())
{
ans=true;
}
}
else
{
Statement stmt = con.createStatement();
String s1 = "select * from userStr where uname = '"+s+"';";
ResultSet rs = stmt.executeQuery(s1);
if(rs.next())
{
ans=true;
}
}
return ans;
}
}
class thread extends Thread implements OnCallBack
{
public void run() {
//System.out.println(Thread.currentThread().getName() + " added to the system");
}
public void onCallBack(String s) {
// TODO Auto-generated method stub
System.out.println("\n" + s + " added to the system");
}
}
class ThreadPool
{
private OnCallBack ocb;
ExecutorService executor;
public ThreadPool(OnCallBack ocb)
{
executor = Executors.newFixedThreadPool(2);
this.ocb = ocb;
}
public void addToPool(String s)
{
Thread user = new thread();
user.setName(s);
executor.execute(user);
ocb.onCallBack(s);
}
public void end()
{
executor.shutdown();
}
}