-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryManagementSystem.java
More file actions
230 lines (194 loc) · 7.57 KB
/
LibraryManagementSystem.java
File metadata and controls
230 lines (194 loc) · 7.57 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
230
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
class Book {
private String title;
private String author;
private int serialNumber;
public Book(String title, String author, int serialNumber) {
this.title = title;
this.author = author;
this.serialNumber = serialNumber;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
public int getSerialNumber() {
return serialNumber;
}
@Override
public String toString() {
return "Serial Number: " + serialNumber + "\nTitle: " + title + "\nAuthor: " + author + "\n";
}
}
class LibrarySystem {
private ArrayList<Book> books;
public LibrarySystem() {
this.books = new ArrayList<>();
books.add(new Book("The Catcher in the Rye", "J.D. Salinger", 1));
books.add(new Book("To Kill a Mockingbird", "Harper Lee", 2));
books.add(new Book("1984", "George Orwell", 3));
books.add(new Book("Pride and Prejudice", "Jane Austen", 4));
books.add(new Book("The Great Gatsby", "F. Scott Fitzgerald", 5));
}
public void addBook(String title, String author, int serialNumber) {
Book newBook = new Book(title, author, serialNumber);
books.add(newBook);
}
public String searchBookByTitle(String title) {
for (Book book : books) {
if (book.getTitle().equalsIgnoreCase(title)) {
return book.toString();
}
}
return "Book not found with title: " + title;
}
public String deleteBook(int serialNumber) {
for (Book book : books) {
if (book.getSerialNumber() == serialNumber) {
books.remove(book);
return "Book deleted successfully!";
}
}
return "Book not found with serial number: " + serialNumber;
}
public String displayBooks() {
if (books.isEmpty()) {
return "No books in the library.";
} else {
StringBuilder sb = new StringBuilder("Library Books:\n");
for (Book book : books) {
sb.append(book).append("\n");
}
return sb.toString();
}
}
}
public class LibraryManagementSystem {
private LibrarySystem librarySystem;
private JFrame frame;
private JTextArea displayArea;
private JTextField titleField, authorField, serialNumberField;
public LibraryManagementSystem() {
librarySystem = new LibrarySystem();
frame = new JFrame("Library System");
frame.setSize(500, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout(10, 10));
// Setting a background color
frame.getContentPane().setBackground(new Color(245, 245, 245));
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBackground(new Color(245, 245, 245));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.WEST;
// Title Field
panel.add(new JLabel("Title: "), gbc);
titleField = new JTextField(15);
titleField.setPreferredSize(new Dimension(200, 30));
titleField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
gbc.gridx = 1;
panel.add(titleField, gbc);
// Author Field
gbc.gridx = 0;
gbc.gridy = 1;
panel.add(new JLabel("Author: "), gbc);
authorField = new JTextField(15);
authorField.setPreferredSize(new Dimension(200, 30));
authorField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
gbc.gridx = 1;
panel.add(authorField, gbc);
// Serial Number Field
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(new JLabel("Serial Number: "), gbc);
serialNumberField = new JTextField(15);
serialNumberField.setPreferredSize(new Dimension(200, 30));
serialNumberField.setBorder(BorderFactory.createLineBorder(Color.GRAY, 1));
gbc.gridx = 1;
panel.add(serialNumberField, gbc);
// Buttons
JButton addButton = new JButton("Add Book");
JButton searchButton = new JButton("Search Book");
JButton deleteButton = new JButton("Delete Book");
JButton displayButton = new JButton("Display Books");
// Button styling
Font buttonFont = new Font("Arial", Font.BOLD, 12);
addButton.setFont(buttonFont);
searchButton.setFont(buttonFont);
deleteButton.setFont(buttonFont);
displayButton.setFont(buttonFont);
addButton.setBackground(new Color(64, 158, 255));
addButton.setForeground(Color.WHITE);
searchButton.setBackground(new Color(64, 158, 255));
searchButton.setForeground(Color.WHITE);
deleteButton.setBackground(new Color(255, 97, 97));
deleteButton.setForeground(Color.WHITE);
displayButton.setBackground(new Color(0, 204, 102));
displayButton.setForeground(Color.WHITE);
// Adding buttons to the panel
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(addButton, gbc);
gbc.gridy = 4;
panel.add(searchButton, gbc);
gbc.gridy = 5;
panel.add(deleteButton, gbc);
gbc.gridy = 6;
panel.add(displayButton, gbc);
// Display area for results
displayArea = new JTextArea();
displayArea.setEditable(false);
displayArea.setFont(new Font("Monospaced", Font.PLAIN, 12));
displayArea.setBackground(new Color(240, 240, 240));
JScrollPane scrollPane = new JScrollPane(displayArea);
// Add components to the frame
frame.add(panel, BorderLayout.NORTH);
frame.add(scrollPane, BorderLayout.CENTER);
// Button actions
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String title = titleField.getText();
String author = authorField.getText();
int serialNumber = Integer.parseInt(serialNumberField.getText());
librarySystem.addBook(title, author, serialNumber);
displayArea.setText("Book added successfully!");
}
});
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String title = titleField.getText(); // Get the book title from the title field
String result = librarySystem.searchBookByTitle(title);
displayArea.setText(result);
}
});
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int serialNumber = Integer.parseInt(serialNumberField.getText());
String result = librarySystem.deleteBook(serialNumber);
displayArea.setText(result);
}
});
displayButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String result = librarySystem.displayBooks();
displayArea.setText(result);
}
});
// Show the frame
frame.setVisible(true);
}
public static void main(String[] args) {
new LibraryManagementSystem();
}
}