-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddBook.java
More file actions
122 lines (102 loc) · 4.39 KB
/
Copy pathAddBook.java
File metadata and controls
122 lines (102 loc) · 4.39 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
import javax.swing.*;
import java.awt.*;
import java.sql.*;
public class AddBook extends JFrame {
JTextField name;
JTextField author;
JTextArea info;
AddBook() {
setTitle("Add Book");
setSize(520, 360);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
JPanel root = LibraryUI.createContentPanel();
root.setLayout(new BorderLayout(0, 16));
setContentPane(root);
JPanel header = new JPanel();
header.setOpaque(false);
header.setLayout(new BoxLayout(header, BoxLayout.Y_AXIS));
header.add(LibraryUI.createTitle("Add Book"));
header.add(Box.createVerticalStrut(4));
header.add(LibraryUI.createSubtitle("Create a new library record and avoid duplicate entries."));
root.add(header, BorderLayout.NORTH);
JPanel card = LibraryUI.createCardPanel();
card.setLayout(new BoxLayout(card, BoxLayout.Y_AXIS));
root.add(card, BorderLayout.CENTER);
JLabel nameLabel = new JLabel("Book Name");
nameLabel.setFont(LibraryUI.LABEL_FONT);
nameLabel.setForeground(LibraryUI.TEXT);
card.add(nameLabel);
card.add(Box.createVerticalStrut(8));
name = LibraryUI.createTextField(26);
card.add(name);
card.add(Box.createVerticalStrut(14));
JLabel authorLabel = new JLabel("Author");
authorLabel.setFont(LibraryUI.LABEL_FONT);
authorLabel.setForeground(LibraryUI.TEXT);
card.add(authorLabel);
card.add(Box.createVerticalStrut(8));
author = LibraryUI.createTextField(26);
card.add(author);
card.add(Box.createVerticalStrut(16));
JPanel buttonRow = new JPanel(new FlowLayout(FlowLayout.LEFT, 0, 0));
buttonRow.setOpaque(false);
JButton addBtn = LibraryUI.createPrimaryButton("Add Book");
buttonRow.add(addBtn);
card.add(buttonRow);
card.add(Box.createVerticalStrut(16));
info = LibraryUI.createInfoArea(5, 30);
info.setText("Enter a book name and author. If the same book already exists, the app will show it instead of adding another copy.");
card.add(LibraryUI.wrap(info));
addBtn.addActionListener(e -> addBook());
setVisible(true);
}
private void addBook() {
String bookName = name.getText().trim();
String authorName = author.getText().trim();
if (bookName.isEmpty() || authorName.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter both book name and author.");
return;
}
Connection con = DBConnection.getConnection();
if (con == null) {
return;
}
try (
PreparedStatement check = con.prepareStatement(
"SELECT * FROM books WHERE LOWER(name)=LOWER(?) AND LOWER(author)=LOWER(?)"
);
PreparedStatement insert = con.prepareStatement(
"INSERT INTO books(name, author, issued) VALUES(?,?,false)"
)
) {
check.setString(1, bookName);
check.setString(2, authorName);
ResultSet existing = check.executeQuery();
if (existing.next()) {
info.setText(
"This book already exists.\n\n"
+ "ID: " + existing.getInt("id") + "\n"
+ "Name: " + existing.getString("name") + "\n"
+ "Author: " + existing.getString("author") + "\n"
+ "Status: " + (existing.getBoolean("issued") ? "Issued" : "Available")
);
JOptionPane.showMessageDialog(this, "This book already exists in the library.");
return;
}
insert.setString(1, bookName);
insert.setString(2, authorName);
int rows = insert.executeUpdate();
if (rows > 0) {
info.setText("Book added successfully.\n\nName: " + bookName + "\nAuthor: " + authorName + "\nStatus: Available");
name.setText("");
author.setText("");
JOptionPane.showMessageDialog(this, "Book added successfully.");
} else {
info.setText("Book could not be added.");
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(this, "Unable to add book.\n" + ex.getMessage());
}
}
}