0% found this document useful (0 votes)
4 views

USER

The document contains Java classes for a Library Management System, including functionalities for Students, Admin, and Faculty. The Students class allows users to borrow and return books, while the Admin class manages the book catalog, and the Faculty class provides a portal for faculty members to borrow and return books. Each class includes a graphical user interface (GUI) built using Swing components for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

USER

The document contains Java classes for a Library Management System, including functionalities for Students, Admin, and Faculty. The Students class allows users to borrow and return books, while the Admin class manages the book catalog, and the Faculty class provides a portal for faculty members to borrow and return books. Each class includes a graphical user interface (GUI) built using Swing components for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

USER(Package)

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.*;

public class Students implements ActionListener {

private String username;


private String password;

// Maps for borrowed books and their counts


private Map<String, Integer> borrowedBooksCount;

// 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;

// Map of books with their quantities


private final Map<String, Integer> availableBooks;

public Students(String username, String password) {


this.username = username;
this.password = password;
this.borrowedBooksCount = new HashMap<>();
this.availableBooks = new LinkedHashMap<>(Map.of(
"Java Programming", 5,
"Data Structures and Algorithms", 4,
"Database Systems", 3,
"Operating Systems", 2,
"Software Engineering", 6
));
initializeGUI();
}

private void initializeGUI() {


// Frame settings
frame = new JFrame("Student Portal");
frame.setSize(900, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());

// Header Section
JPanel headerPanel = new JPanel();
headerPanel.setBackground(Color.DARK_GRAY);
headerPanel.setPreferredSize(new Dimension(frame.getWidth(), 80));

JLabel headerLabel = new JLabel("Library Management System - Student Portal",


JLabel.CENTER);
headerLabel.setFont(new Font("Arial", Font.BOLD, 20));
headerLabel.setForeground(Color.WHITE);
headerLabel.setAlignmentX(JLabel.SOUTH);
headerPanel.add(headerLabel);
frame.add(headerPanel, BorderLayout.NORTH);

// 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);

// Left Panel - Borrowed Books Section


JPanel leftPanel = new JPanel();
leftPanel.setLayout(new BorderLayout());

JLabel borrowedBooksLabel = new JLabel("Your Borrowed Books");


borrowedBooksLabel.setFont(new Font("Arial", Font.BOLD, 20));
borrowedBooksLabel.setAlignmentY(JLabel.CENTER_ALIGNMENT);
leftPanel.add(borrowedBooksLabel, BorderLayout.NORTH);

// 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);

// Add Return Button Below Borrowed Books Table


returnButton = new JButton("Return Book");
returnButton.addActionListener(this);
JPanel returnButtonPanel = new JPanel(new FlowLayout());
returnButtonPanel.add(returnButton);
leftPanel.add(returnButtonPanel, BorderLayout.SOUTH);

// Right Panel - Available Books and Actions


JPanel rightPanel = new JPanel();
rightPanel.setLayout(new BorderLayout());

// 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);

// Create read-only JTable for available books with quantities


availableBooksModel = new DefaultTableModel(new String[][]{}, new String[]
{"Available Books", "Quantity"}) {
@Override
public boolean isCellEditable(int row, int column) {
return false; // Disable cell editing
}
};
updateBookTable();

availableBooksTable = new JTable(availableBooksModel);


JScrollPane tableScrollPane = new JScrollPane(availableBooksTable);
tableScrollPane.setPreferredSize(new Dimension(400, 400));
rightPanel.add(tableScrollPane, BorderLayout.CENTER);

// 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);

// Search Button Action


searchButton.addActionListener(e -> {
String query = searchField.getText().trim().toLowerCase();
if (!query.isEmpty()) {
filterBooks(query);
}
});

// Reset Button Action


resetButton.addActionListener(e -> updateBookTable());

// Add the left and right panels to the frame


JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel,
rightPanel);
splitPane.setResizeWeight(0.5);
splitPane.setEnabled(false);
frame.add(splitPane, BorderLayout.CENTER);

frame.setVisible(true);
}

private void updateBookTable() {


availableBooksModel.setRowCount(0); // Clear the table
for (Map.Entry<String, Integer> entry : availableBooks.entrySet()) {
availableBooksModel.addRow(new String[]{entry.getKey(),
String.valueOf(entry.getValue())});
}
}

private void filterBooks(String query) {


availableBooksModel.setRowCount(0); // Clear the table
for (Map.Entry<String, Integer> entry : availableBooks.entrySet()) {
if (entry.getKey().toLowerCase().contains(query)) {
availableBooksModel.addRow(new String[]{entry.getKey(),
String.valueOf(entry.getValue())});
}
}
}

@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();
}

private void returnBook(String bookName, int quantity) {


availableBooks.put(bookName, availableBooks.get(bookName) + quantity);
borrowedBooksCount.put(bookName, borrowedBooksCount.get(bookName) -
quantity);
if (borrowedBooksCount.get(bookName) == 0) {
borrowedBooksCount.remove(bookName);
}
JOptionPane.showMessageDialog(frame, "You have successfully returned " +
quantity + " copies of: " + bookName);
updateBorrowedBooksArea();
updateBookTable();
}

private void updateBorrowedBooksArea() {


borrowedBooksModel.setRowCount(0); // Clear existing rows
for (Map.Entry<String, Integer> entry : borrowedBooksCount.entrySet()) {
borrowedBooksModel.addRow(new Object[]{entry.getKey(),
entry.getValue()});
}
}
//para rani sa testing
public static void main(String[] args) {
new Students("student1", "password1");
}
}
ADMIN (Class)
package User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Admin implements ActionListener {


private JFrame adminFrame;
private JPanel topPanel, footerPanel, leftPanel, centerPanel;
private JTextField bookField;
private JButton addBookButton, removeBookButton;
private JTextArea catalogArea;
private JScrollPane scrollPane;

private ArrayList<String> bookCatalog = new 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));

// Set frame icon


ImageIcon icon = new
ImageIcon("C:/Users/jenni/Documents/NetBeansProjects/LibraryManagentSystem/
src/LmsLogo.png");
adminFrame.setIconImage(icon.getImage());

// Top panel (Header)


topPanel = new JPanel();
topPanel.setPreferredSize(new Dimension(900, 50));
topPanel.setBackground(Color.LIGHT_GRAY);
JLabel titleLabel = new JLabel("Library Management - Admin Panel",
JLabel.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 24));
topPanel.add(titleLabel);

// 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);

// Left panel (optional, for additional features or navigation)


leftPanel = new JPanel();
leftPanel.setPreferredSize(new Dimension(150, 600));
leftPanel.setBackground(Color.DARK_GRAY);

// Center panel for main actions


centerPanel = new JPanel(new BorderLayout(10, 10));
JPanel inputPanel = new JPanel(new FlowLayout());
catalogArea = new JTextArea(20, 50);
catalogArea.setEditable(false);
catalogArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
scrollPane = new JScrollPane(catalogArea);

// Input fields and buttons


bookField = new JTextField(20);
addBookButton = new JButton("Add Book");
removeBookButton = new JButton("Remove Book");

inputPanel.add(new JLabel("Book Name:"));


inputPanel.add(bookField);
inputPanel.add(addBookButton);
inputPanel.add(removeBookButton);

centerPanel.add(inputPanel, BorderLayout.NORTH);
centerPanel.add(scrollPane, BorderLayout.CENTER);

// Add components to frame


adminFrame.add(topPanel, BorderLayout.NORTH);
adminFrame.add(footerPanel, BorderLayout.SOUTH);
adminFrame.add(leftPanel, BorderLayout.WEST);
adminFrame.add(centerPanel, BorderLayout.CENTER);

// Add action listeners to buttons


addBookButton.addActionListener(this);
removeBookButton.addActionListener(this);

// 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.");
}
}
}

private void addBook(String bookName) {


bookCatalog.add(bookName);
JOptionPane.showMessageDialog(adminFrame, "Book added: " + bookName);
}

private void removeBook(String bookName) {


if (bookCatalog.contains(bookName)) {
bookCatalog.remove(bookName);
JOptionPane.showMessageDialog(adminFrame, "Book removed: " +
bookName);
} else {
JOptionPane.showMessageDialog(adminFrame, "Book not found in the
catalog.");
}
}

private void updateCatalog() {


catalogArea.setText(String.join("\n", bookCatalog));
}
// Para rani sa testing
public static void main(String[] args) {
new Admin();
}
}
FACULTY (Class)

package User;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class Faculty implements ActionListener {


private String username;
private String password;
private ArrayList<String> borrowedBooks;

// GUI Components
private JFrame frame;
private JTextField bookField;
private JTextArea borrowedBooksArea;
private JButton borrowButton, returnButton;
private JPanel headerPanel, footerPanel;

public Faculty(String username, String password) {


this.username = username;
this.password = password;
this.borrowedBooks = new ArrayList<>();
initializeGUI();
}

// Method to initialize the GUI


private void initializeGUI() {
// Frame settings
frame = new JFrame("Faculty Portal");
frame.setSize(900, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout(10, 10));
frame.setResizable(false);

// 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");

inputPanel.add(new JLabel("Book Name:"));


inputPanel.add(bookField);
inputPanel.add(borrowButton);
inputPanel.add(returnButton);

// Display area for borrowed books


borrowedBooksArea = new JTextArea(15, 40);
borrowedBooksArea.setEditable(false);
borrowedBooksArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
JScrollPane scrollPane = new JScrollPane(borrowedBooksArea);

JPanel centerPanel = new JPanel(new BorderLayout(10, 10));


centerPanel.add(inputPanel, BorderLayout.NORTH);
centerPanel.add(scrollPane, BorderLayout.CENTER);

// Add components to the frame


frame.add(headerPanel, BorderLayout.NORTH);
frame.add(footerPanel, BorderLayout.SOUTH);
frame.add(centerPanel, BorderLayout.CENTER);

// Add action listeners to buttons


borrowButton.addActionListener(this);
returnButton.addActionListener(this);

// Initialize the borrowed books display


updateBorrowedBooksArea();

// Make the frame visible


frame.setVisible(true);
}
// Method to update the borrowed books display
private void updateBorrowedBooksArea() {
borrowedBooksArea.setText(String.join("\n", borrowedBooks));
}

// ActionPerformed method for handling button clicks


@Override
public void actionPerformed(ActionEvent e) {
String bookName = bookField.getText().trim();

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.");
}
}
}

// Logic for borrowing a book


public void borrowBook(String bookName) {
borrowedBooks.add(bookName);
JOptionPane.showMessageDialog(frame, "Book borrowed: " + bookName);
}

// Logic for returning a book


public void returnBook(String bookName) {
if (borrowedBooks.contains(bookName)) {
borrowedBooks.remove(bookName);
JOptionPane.showMessageDialog(frame, "Book returned: " + bookName);
} else {
JOptionPane.showMessageDialog(frame, "You have not borrowed this
book.");
}
}

// Getters for username and password


public String getUsername() {
return username;
}

public String getPassword() {


return password;
}

// Para rani sa testing


public static void main(String[] args) {
new Faculty("faculty1", "password1");
}
}

You might also like