-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryManagementSystem.java
More file actions
229 lines (190 loc) · 7.9 KB
/
LibraryManagementSystem.java
File metadata and controls
229 lines (190 loc) · 7.9 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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
public class LibraryManagementSystem extends JFrame {
// Data structures for storing book information
private ArrayList<HashMap<String, String>> books;
private DefaultTableModel tableModel;
// GUI Components
private JTextField titleField, authorField, isbnField, yearField, searchField;
private JComboBox<String> genreBox;
private JCheckBox availabilityBox;
private JTable bookTable;
public LibraryManagementSystem() {
books = new ArrayList<>();
initializeUI();
}
private void initializeUI() {
setTitle("Library Management System");
setSize(800, 600);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
// MenuBar setup
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("File");
JMenu editMenu = new JMenu("Edit");
JMenu helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(editMenu);
menuBar.add(helpMenu);
setJMenuBar(menuBar);
// Toolbar setup
JToolBar toolBar = new JToolBar();
JButton addButton = new JButton("Add Book");
JButton removeButton = new JButton("Remove Book");
JButton searchButton = new JButton("Search");
toolBar.add(addButton);
toolBar.add(removeButton);
toolBar.add(searchButton);
add(toolBar, BorderLayout.NORTH);
// TabbedPane setup
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("Book Details", createBookDetailsPanel());
tabbedPane.add("Book List", createBookListPanel());
add(tabbedPane, BorderLayout.CENTER);
// Event Handlers
addButton.addActionListener(e -> addBook());
removeButton.addActionListener(e -> removeBook());
searchButton.addActionListener(e -> searchBook());
setVisible(true);
}
// Book Details Panel
private JPanel createBookDetailsPanel() {
JPanel bookDetailsPanel = new JPanel(new GridLayout(6, 2));
bookDetailsPanel.add(new JLabel("Title:"));
titleField = new JTextField();
bookDetailsPanel.add(titleField);
bookDetailsPanel.add(new JLabel("Author:"));
authorField = new JTextField();
bookDetailsPanel.add(authorField);
bookDetailsPanel.add(new JLabel("ISBN:"));
isbnField = new JTextField();
bookDetailsPanel.add(isbnField);
bookDetailsPanel.add(new JLabel("Publication Year:"));
yearField = new JTextField();
bookDetailsPanel.add(yearField);
bookDetailsPanel.add(new JLabel("Genre:"));
genreBox = new JComboBox<>(new String[]{"Fiction", "Non-Fiction", "Science", "Biography", "Fantasy"});
bookDetailsPanel.add(genreBox);
bookDetailsPanel.add(new JLabel("Available:"));
availabilityBox = new JCheckBox();
bookDetailsPanel.add(availabilityBox);
JButton addBookButton = new JButton("Add Book");
JButton updateBookButton = new JButton("Update Book");
bookDetailsPanel.add(addBookButton);
bookDetailsPanel.add(updateBookButton);
// Event Listeners for buttons
addBookButton.addActionListener(e -> addBook());
updateBookButton.addActionListener(e -> updateBook());
return bookDetailsPanel;
}
// Book List Panel
private JPanel createBookListPanel() {
JPanel bookListPanel = new JPanel(new BorderLayout());
// Table for displaying books
String[] columnNames = {"Title", "Author", "ISBN", "Genre", "Available"};
tableModel = new DefaultTableModel(columnNames, 0);
bookTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(bookTable);
bookListPanel.add(scrollPane, BorderLayout.CENTER);
// Search bar and button
JPanel searchPanel = new JPanel();
searchField = new JTextField(20);
JButton searchButton = new JButton("Search");
searchPanel.add(searchField);
searchPanel.add(searchButton);
bookListPanel.add(searchPanel, BorderLayout.NORTH);
searchButton.addActionListener(e -> searchBook());
return bookListPanel;
}
// Add Book method
private void addBook() {
String title = titleField.getText();
String author = authorField.getText();
String isbn = isbnField.getText();
String year = yearField.getText();
String genre = genreBox.getSelectedItem().toString();
String available = availabilityBox.isSelected() ? "Yes" : "No";
// Add book to data structure
HashMap<String, String> book = new HashMap<>();
book.put("Title", title);
book.put("Author", author);
book.put("ISBN", isbn);
book.put("Year", year);
book.put("Genre", genre);
book.put("Available", available);
books.add(book);
// Add book to table
tableModel.addRow(new Object[]{title, author, isbn, genre, available});
// Clear fields after adding
titleField.setText("");
authorField.setText("");
isbnField.setText("");
yearField.setText("");
availabilityBox.setSelected(false);
}
// Update Book method
private void updateBook() {
int selectedRow = bookTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select a book to update.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
// Update data structure
String title = titleField.getText();
String author = authorField.getText();
String isbn = isbnField.getText();
String year = yearField.getText();
String genre = genreBox.getSelectedItem().toString();
String available = availabilityBox.isSelected() ? "Yes" : "No";
HashMap<String, String> book = books.get(selectedRow);
book.put("Title", title);
book.put("Author", author);
book.put("ISBN", isbn);
book.put("Year", year);
book.put("Genre", genre);
book.put("Available", available);
// Update table
tableModel.setValueAt(title, selectedRow, 0);
tableModel.setValueAt(author, selectedRow, 1);
tableModel.setValueAt(isbn, selectedRow, 2);
tableModel.setValueAt(genre, selectedRow, 3);
tableModel.setValueAt(available, selectedRow, 4);
}
// Remove Book method
private void removeBook() {
int selectedRow = bookTable.getSelectedRow();
if (selectedRow == -1) {
JOptionPane.showMessageDialog(this, "Please select a book to remove.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to remove this book?", "Confirm", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
// Remove from data structure
books.remove(selectedRow);
// Remove from table
tableModel.removeRow(selectedRow);
}
}
// Search Book method
private void searchBook() {
String searchTerm = searchField.getText().toLowerCase();
if (searchTerm.isEmpty()) {
JOptionPane.showMessageDialog(this, "Please enter a search term.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
for (int i = 0; i < books.size(); i++) {
HashMap<String, String> book = books.get(i);
if (book.get("Title").toLowerCase().contains(searchTerm) || book.get("Author").toLowerCase().contains(searchTerm)) {
bookTable.setRowSelectionInterval(i, i);
break;
}
}
}
public static void main(String args[] ) {
SwingUtilities.invokeLater(LibraryManagementSystem::new);
}
}