-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibraryManagementSystem.java
237 lines (201 loc) · 7.87 KB
/
LibraryManagementSystem.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class LibraryManagementSystem extends JFrame {
private ArrayList<Book> bookList = new ArrayList<>();
private JTable bookTable;
private JTextField titleField, authorField, isbnField, yearField, searchField;
private JComboBox<String> genreCombo;
private JCheckBox availabilityCheck;
public LibraryManagementSystem() {
// Setting up the main window
setTitle("Library Management System");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLayout(new BorderLayout());
// Menu bar 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 addBookButton = new JButton("Add Book");
JButton removeBookButton = new JButton("Remove Book");
JButton searchBookButton = new JButton("Search");
toolBar.add(addBookButton);
toolBar.add(removeBookButton);
toolBar.add(searchBookButton);
add(toolBar, BorderLayout.NORTH);
// TabbedPane setup
JTabbedPane tabbedPane = new JTabbedPane();
// Book Details Panel
JPanel bookDetailsPanel = new JPanel(new GridLayout(6, 2));
titleField = new JTextField();
authorField = new JTextField();
isbnField = new JTextField();
yearField = new JTextField();
genreCombo = new JComboBox<>(new String[] { "Fiction", "Non-fiction", "Science", "History", "Others" });
availabilityCheck = new JCheckBox("Available");
bookDetailsPanel.add(new JLabel("Title:"));
bookDetailsPanel.add(titleField);
bookDetailsPanel.add(new JLabel("Author:"));
bookDetailsPanel.add(authorField);
bookDetailsPanel.add(new JLabel("ISBN:"));
bookDetailsPanel.add(isbnField);
bookDetailsPanel.add(new JLabel("Publication Year:"));
bookDetailsPanel.add(yearField);
bookDetailsPanel.add(new JLabel("Genre:"));
bookDetailsPanel.add(genreCombo);
bookDetailsPanel.add(new JLabel("Availability:"));
bookDetailsPanel.add(availabilityCheck);
JButton addBook = new JButton("Add Book");
JButton updateBook = new JButton("Update Book");
bookDetailsPanel.add(addBook);
bookDetailsPanel.add(updateBook);
tabbedPane.add("Book Details", bookDetailsPanel);
// Book List Panel
JPanel bookListPanel = new JPanel(new BorderLayout());
bookTable = new JTable(new BookTableModel(bookList));
JScrollPane scrollPane = new JScrollPane(bookTable);
bookListPanel.add(scrollPane, BorderLayout.CENTER);
// Search Bar setup
JPanel searchPanel = new JPanel(new FlowLayout());
searchField = new JTextField(20);
JButton searchButton = new JButton("Search");
searchPanel.add(new JLabel("Search by Title:"));
searchPanel.add(searchField);
searchPanel.add(searchButton);
bookListPanel.add(searchPanel, BorderLayout.NORTH);
tabbedPane.add("Book List", bookListPanel);
add(tabbedPane, BorderLayout.CENTER);
// Event Handling
addBook.addActionListener(e -> addBook());
searchButton.addActionListener(e -> searchBook());
removeBookButton.addActionListener(e -> removeBook());
setVisible(true);
}
private void addBook() {
String title = titleField.getText();
String author = authorField.getText();
String isbn = isbnField.getText();
String year = yearField.getText();
String genre = (String) genreCombo.getSelectedItem();
boolean available = availabilityCheck.isSelected();
if (title.isEmpty() || author.isEmpty() || isbn.isEmpty() || year.isEmpty()) {
JOptionPane.showMessageDialog(this, "All fields must be filled", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
bookList.add(new Book(title, author, isbn, year, genre, available));
bookTable.setModel(new BookTableModel(bookList));
JOptionPane.showMessageDialog(this, "Book added successfully!", "Success", JOptionPane.INFORMATION_MESSAGE);
clearFields();
}
private void searchBook() {
String searchQuery = searchField.getText();
ArrayList<Book> resultList = new ArrayList<>();
for (Book book : bookList) {
if (book.getTitle().toLowerCase().contains(searchQuery.toLowerCase())) {
resultList.add(book);
}
}
bookTable.setModel(new BookTableModel(resultList));
}
private void removeBook() {
int selectedRow = bookTable.getSelectedRow();
if (selectedRow >= 0) {
int confirm = JOptionPane.showConfirmDialog(this, "Are you sure you want to delete this book?", "Confirm Deletion", JOptionPane.YES_NO_OPTION);
if (confirm == JOptionPane.YES_OPTION) {
bookList.remove(selectedRow);
bookTable.setModel(new BookTableModel(bookList));
}
} else {
JOptionPane.showMessageDialog(this, "No book selected", "Error", JOptionPane.ERROR_MESSAGE);
}
}
private void clearFields() {
titleField.setText("");
authorField.setText("");
isbnField.setText("");
yearField.setText("");
genreCombo.setSelectedIndex(0);
availabilityCheck.setSelected(false);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(LibraryManagementSystem::new);
}
}
class Book {
private String title, author, isbn, year, genre;
private boolean available;
public Book(String title, String author, String isbn, String year, String genre, boolean available) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.year = year;
this.genre = genre;
this.available = available;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getIsbn() {
return isbn;
}
public String getYear() {
return year;
}
public String getGenre() {
return genre;
}
public boolean isAvailable() {
return available;
}
}
class BookTableModel extends javax.swing.table.AbstractTableModel {
private final String[] columns = {"Title", "Author", "ISBN", "Year", "Genre", "Availability"};
private ArrayList<Book> bookList;
public BookTableModel(ArrayList<Book> bookList) {
this.bookList = bookList;
}
@Override
public int getRowCount() {
return bookList.size();
}
@Override
public int getColumnCount() {
return columns.length;
}
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Book book = bookList.get(rowIndex);
switch (columnIndex) {
case 0:
return book.getTitle();
case 1:
return book.getAuthor();
case 2:
return book.getIsbn();
case 3:
return book.getYear();
case 4:
return book.getGenre();
case 5:
return book.isAvailable() ? "Yes" : "No";
default:
return null;
}
}
@Override
public String getColumnName(int column) {
return columns[column];
}
}