USER
USER
STUDENTS(Class)
package User;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
// GUI components
private JFrame frame;
private JTable borrowedBooksTable; // Table for borrowed books
private DefaultTableModel borrowedBooksModel; // Model for the borrowed
books table
private JTable availableBooksTable;
private DefaultTableModel availableBooksModel; // Model for available books
table
private JButton borrowButton;
private JButton returnButton;
// Header Section
JPanel headerPanel = new JPanel();
headerPanel.setBackground(Color.DARK_GRAY);
headerPanel.setPreferredSize(new Dimension(frame.getWidth(), 80));
// Footer Section
JPanel footerPanel = new JPanel();
footerPanel.setBackground(Color.DARK_GRAY);
footerPanel.setPreferredSize(new Dimension(frame.getWidth(), 80));
JLabel footerLabel = new JLabel("Manage your books efficiently and effectively
with ease!", JLabel.CENTER);
footerLabel.setFont(new Font("Arial", Font.PLAIN, 14));
footerLabel.setForeground(Color.WHITE);
footerPanel.add(footerLabel);
frame.add(footerPanel, BorderLayout.SOUTH);
// Create JTable for borrowed books with columns "Book Title" and "Quantity"
borrowedBooksModel = new DefaultTableModel(new String[]{"Book Title",
"Quantity"}, 0) {
@Override
public boolean isCellEditable(int row, int column) {
return false; // Disable cell editing
}
};
borrowedBooksTable = new JTable(borrowedBooksModel);
JScrollPane borrowedScrollPane = new JScrollPane(borrowedBooksTable);
borrowedScrollPane.setPreferredSize(new Dimension(300, 350));
leftPanel.add(borrowedScrollPane, BorderLayout.CENTER);
// Search Panel
JPanel searchPanel = new JPanel();
searchPanel.setLayout(new FlowLayout());
JLabel searchLabel = new JLabel("Search Book: ");
JTextField searchField = new JTextField(15);
JButton searchButton = new JButton("Search");
JButton resetButton = new JButton("Reset");
searchPanel.add(searchLabel);
searchPanel.add(searchField);
searchPanel.add(searchButton);
searchPanel.add(resetButton);
rightPanel.add(searchPanel, BorderLayout.NORTH);
// Buttons Panel
JPanel buttonsPanel = new JPanel();
borrowButton = new JButton("Borrow Book");
borrowButton.setBackground(Color.decode("#FFA500"));
borrowButton.addActionListener(this);
buttonsPanel.add(borrowButton);
rightPanel.add(buttonsPanel, BorderLayout.SOUTH);
frame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == borrowButton) {
int selectedRow = availableBooksTable.getSelectedRow();
if (selectedRow != -1) {
String selectedBook = availableBooksTable.getValueAt(selectedRow,
0).toString();
int availableQuantity = availableBooks.get(selectedBook);
String input = JOptionPane.showInputDialog(frame, "Enter the quantity to
borrow (Available: " + availableQuantity + "):");
if (input != null && !input.isEmpty()) {
int quantity = Integer.parseInt(input);
if (quantity > 0 && quantity <= availableQuantity) {
borrowBook(selectedBook, quantity);
} else {
JOptionPane.showMessageDialog(frame, "Invalid quantity entered.");
}
}
} else {
JOptionPane.showMessageDialog(frame, "Please select a book to
borrow.");
}
} else if (e.getSource() == returnButton) {
int selectedRow = borrowedBooksTable.getSelectedRow();
if (selectedRow != -1) {
String selectedBook = borrowedBooksTable.getValueAt(selectedRow,
0).toString();
int borrowedQuantity = borrowedBooksCount.get(selectedBook);
String input = JOptionPane.showInputDialog(frame, "Enter the quantity to
return (Borrowed: " + borrowedQuantity + "):");
if (input != null && !input.isEmpty()) {
int quantity = Integer.parseInt(input);
if (quantity > 0 && quantity <= borrowedQuantity) {
returnBook(selectedBook, quantity);
} else {
JOptionPane.showMessageDialog(frame, "Invalid quantity entered.");
}
}
} else {
JOptionPane.showMessageDialog(frame, "Please select a book to return.");
}
}
}
private void borrowBook(String bookName, int quantity) {
availableBooks.put(bookName, availableBooks.get(bookName) - quantity);
borrowedBooksCount.put(bookName,
borrowedBooksCount.getOrDefault(bookName, 0) + quantity);
JOptionPane.showMessageDialog(frame, "You have successfully borrowed " +
quantity + " copies of: " + bookName);
updateBorrowedBooksArea();
updateBookTable();
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public Admin() {
// Frame setup
adminFrame = new JFrame("Library Admin Panel");
adminFrame.setSize(900, 600);
adminFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
adminFrame.setResizable(false);
adminFrame.setLayout(new BorderLayout(10, 10));
// Footer panel
footerPanel = new JPanel();
footerPanel.setPreferredSize(new Dimension(900, 50));
footerPanel.setBackground(Color.LIGHT_GRAY);
JLabel footerLabel = new JLabel("Library Management System © 2024",
JLabel.CENTER);
footerLabel.setFont(new Font("Arial", Font.PLAIN, 14));
footerPanel.add(footerLabel);
centerPanel.add(inputPanel, BorderLayout.NORTH);
centerPanel.add(scrollPane, BorderLayout.CENTER);
// Show frame
adminFrame.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == addBookButton) {
String bookName = bookField.getText().trim();
if (!bookName.isEmpty()) {
addBook(bookName);
updateCatalog();
bookField.setText("");
} else {
JOptionPane.showMessageDialog(adminFrame, "Please enter a book
name.");
}
} else if (e.getSource() == removeBookButton) {
String bookName = bookField.getText().trim();
if (!bookName.isEmpty()) {
removeBook(bookName);
updateCatalog();
bookField.setText("");
} else {
JOptionPane.showMessageDialog(adminFrame, "Please enter a book
name.");
}
}
}
package User;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
// GUI Components
private JFrame frame;
private JTextField bookField;
private JTextArea borrowedBooksArea;
private JButton borrowButton, returnButton;
private JPanel headerPanel, footerPanel;
// Header Panel
headerPanel = new JPanel();
headerPanel.setPreferredSize(new Dimension(900, 50));
headerPanel.setBackground(Color.LIGHT_GRAY);
JLabel headerLabel = new JLabel("Library Management - Faculty Portal",
JLabel.CENTER);
headerLabel.setFont(new Font("Arial", Font.BOLD, 24));
headerPanel.add(headerLabel);
// Footer Panel
footerPanel = new JPanel();
footerPanel.setPreferredSize(new Dimension(900, 50));
footerPanel.setBackground(Color.LIGHT_GRAY);
JLabel footerLabel = new JLabel("Library Management System © 2024",
JLabel.CENTER);
footerLabel.setFont(new Font("Arial", Font.PLAIN, 14));
footerPanel.add(footerLabel);
// Input Panel
JPanel inputPanel = new JPanel(new FlowLayout());
bookField = new JTextField(20);
borrowButton = new JButton("Borrow Book");
returnButton = new JButton("Return Book");
if (e.getSource() == borrowButton) {
if (!bookName.isEmpty()) {
borrowBook(bookName);
updateBorrowedBooksArea();
bookField.setText(""); // Clear input field
} else {
JOptionPane.showMessageDialog(frame, "Please enter a book name.");
}
} else if (e.getSource() == returnButton) {
if (!bookName.isEmpty()) {
returnBook(bookName);
updateBorrowedBooksArea();
bookField.setText(""); // Clear input field
} else {
JOptionPane.showMessageDialog(frame, "Please enter a book name.");
}
}
}