-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrarySystemGUI.java
More file actions
227 lines (196 loc) · 7.95 KB
/
LibrarySystemGUI.java
File metadata and controls
227 lines (196 loc) · 7.95 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
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;
import java.util.HashMap;
import java.util.Map;
class Book {
private String bookID, title, author, genre, availability;
public Book(String bookID, String title, String author, String genre, String availability) {
this.bookID = bookID;
this.title = title;
this.author = author;
this.genre = genre;
setAvailability(availability);
}
public String getBookID() {
return bookID;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public String getGenre() {
return genre;
}
public String getAvailability() {
return availability;
}
public void setTitle(String title) {
if (!title.isEmpty())
this.title = title;
}
public void setAuthor(String author) {
if (!author.isEmpty())
this.author = author;
}
public void setAvailability(String availability) {
if (availability.equalsIgnoreCase("Available") || availability.equalsIgnoreCase("Checked Out")) {
this.availability = availability;
} else {
this.availability = "Available";
}
}
}
public class LibrarySystemGUI {
private static Map<String, Book> bookCatalog = new HashMap<>();
private static DefaultTableModel tableModel;
private static JTable bookTable;
public static void main(String[] args) {
SwingUtilities.invokeLater(LibrarySystemGUI::createGUI);
}
private static void createGUI() {
JFrame frame = new JFrame("Library Management System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(700, 500);
frame.setLayout(new BorderLayout());
// Table
String[] columns = { "Book ID", "Title", "Author", "Genre", "Availability" };
tableModel = new DefaultTableModel(columns, 0);
bookTable = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(bookTable);
frame.add(scrollPane, BorderLayout.CENTER);
// Buttons Panel
JPanel panel = new JPanel();
String[] buttons = { "Add Book", "View Books", "Search Book", "Update Book", "Delete Book", "Exit" };
for (String btn : buttons) {
JButton button = new JButton(btn);
button.addActionListener(e -> handleButtonClick(btn));
panel.add(button);
}
frame.add(panel, BorderLayout.SOUTH);
frame.setVisible(true);
}
private static void handleButtonClick(String action) {
switch (action) {
case "Add Book":
addBook();
break;
case "View Books":
viewAllBooks();
break;
case "Search Book":
searchBook();
break;
case "Update Book":
updateBook();
break;
case "Delete Book":
deleteBook();
break;
case "Exit":
System.exit(0);
break;
}
}
private static void addBook() {
JTextField idField = new JTextField();
JTextField titleField = new JTextField();
JTextField authorField = new JTextField();
JTextField genreField = new JTextField();
String[] statusOptions = { "Available", "Checked Out" };
JComboBox<String> availabilityBox = new JComboBox<>(statusOptions);
Object[] message = {
"Book ID:", idField,
"Title:", titleField,
"Author:", authorField,
"Genre:", genreField,
"Availability:", availabilityBox
};
int option = JOptionPane.showConfirmDialog(null, message, "Add Book", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
String bookID = idField.getText().trim();
String title = titleField.getText().trim();
String author = authorField.getText().trim();
String genre = genreField.getText().trim();
String availability = (String) availabilityBox.getSelectedItem();
if (bookID.isEmpty() || title.isEmpty() || author.isEmpty()) {
JOptionPane.showMessageDialog(null, "Book ID, Title, and Author cannot be empty!", "Error",
JOptionPane.ERROR_MESSAGE);
return;
}
if (bookCatalog.containsKey(bookID)) {
JOptionPane.showMessageDialog(null, "Book ID already exists!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Book book = new Book(bookID, title, author, genre, availability);
bookCatalog.put(bookID, book);
JOptionPane.showMessageDialog(null, "Book added successfully!");
viewAllBooks();
}
}
private static void viewAllBooks() {
tableModel.setRowCount(0);
for (Book book : bookCatalog.values()) {
tableModel.addRow(new Object[] { book.getBookID(), book.getTitle(), book.getAuthor(), book.getGenre(),
book.getAvailability() });
}
}
private static void searchBook() {
String searchKey = JOptionPane.showInputDialog("Enter Book ID or Title:");
if (searchKey == null || searchKey.trim().isEmpty())
return;
for (Book book : bookCatalog.values()) {
if (book.getBookID().equalsIgnoreCase(searchKey) || book.getTitle().equalsIgnoreCase(searchKey)) {
JOptionPane.showMessageDialog(null, "Book Found:\n" +
"Book ID: " + book.getBookID() + "\n" +
"Title: " + book.getTitle() + "\n" +
"Author: " + book.getAuthor() + "\n" +
"Genre: " + book.getGenre() + "\n" +
"Availability: " + book.getAvailability());
return;
}
}
JOptionPane.showMessageDialog(null, "Book not found.");
}
private static void updateBook() {
String bookID = JOptionPane.showInputDialog("Enter Book ID to Update:");
if (bookID == null || !bookCatalog.containsKey(bookID)) {
JOptionPane.showMessageDialog(null, "Book ID not found!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
Book book = bookCatalog.get(bookID);
JTextField titleField = new JTextField(book.getTitle());
JTextField authorField = new JTextField(book.getAuthor());
JTextField genreField = new JTextField(book.getGenre());
String[] statusOptions = { "Available", "Checked Out" };
JComboBox<String> availabilityBox = new JComboBox<>(statusOptions);
availabilityBox.setSelectedItem(book.getAvailability());
Object[] message = {
"Title:", titleField,
"Author:", authorField,
"Genre:", genreField,
"Availability:", availabilityBox
};
int option = JOptionPane.showConfirmDialog(null, message, "Update Book", JOptionPane.OK_CANCEL_OPTION);
if (option == JOptionPane.OK_OPTION) {
book.setTitle(titleField.getText().trim());
book.setAuthor(authorField.getText().trim());
book.setAvailability((String) availabilityBox.getSelectedItem());
JOptionPane.showMessageDialog(null, "Book updated successfully!");
viewAllBooks();
}
}
private static void deleteBook() {
String bookID = JOptionPane.showInputDialog("Enter Book ID to Delete:");
if (bookID == null || !bookCatalog.containsKey(bookID)) {
JOptionPane.showMessageDialog(null, "Book ID not found!", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
bookCatalog.remove(bookID);
JOptionPane.showMessageDialog(null, "Book deleted successfully!");
viewAllBooks();
}
}