hostel management system in java (GUI)

introduction
The Hostel Management System is designed to assist in the efficient operation of hostels, particularly those associated with educational institutions. This Java GUI application allows the hostel manager to manage room allocation, student check-in and check-out, rent payments, meal preferences, and complaints. Admins can maintain a database of residents, track room availability, and generate monthly reports. The intuitive graphical interface ensures quick access to student records and room statuses.
It minimizes manual paperwork, reduces data redundancy, and ensures better organization of hostel-related information. Ideal for college or university hostels, the system ensures transparency and systematic accommodation handling.
steps to create hostel management system
Define entities — Students, Rooms, Payments.
Design GUI for room allocation, student registration.
Implement Room and Student classes with status flags.
Handle payments and meal plans.
Store all info in files or DB.
Add reports on room occupancy.
Connect GUI with backend operations.
Test allocation, check-in/out, payment.
Add notification for due payments.
Document and package.
code explanation
1. Importing Required Libraries
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
These libraries help in building GUI (
Swing
,AWT
) and event handling.ArrayList
is used to store student data temporarily in memory.
2. Class Declaration
public class HostelManagementSystem extends JFrame {
Extends
JFrame
to create a top-level Swing window.
3. Global Variables
CardLayout cardLayout;
JPanel mainPanel;
JTextField usernameField, nameField, roomField;
JPasswordField passwordField;
JTextArea studentArea;
JLabel welcomeLabel;
ArrayList studentList = new ArrayList();
GUI components and student list used across multiple methods.
4. Constructor
public HostelManagementSystem() {
setTitle("Hostel Management System");
setSize(600, 450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
mainPanel.add(loginPanel(), "Login");
mainPanel.add(dashboardPanel(), "Dashboard");
add(mainPanel);
cardLayout.show(mainPanel, "Login");
setVisible(true);
}
Creates two screens: Login and Dashboard.
Uses
CardLayout
to switch views based on login success.
5. Login Panel
private JPanel loginPanel() {
...
JButton okBtn = new JButton("OK");
okBtn.addActionListener(e -> {
String user = usernameField.getText();
String pass = new String(passwordField.getPassword());
if (user.equals("admin") && pass.equals("1234")) {
welcomeLabel.setText("Welcome, " + user + "!");
cardLayout.show(mainPanel, "Dashboard");
} else {
JOptionPane.showMessageDialog(this, "Invalid Username or Password");
}
});
...
}
Simple hardcoded login with username:
admin
and password:1234
.On success, switches to Dashboard.
6. Dashboard Panel
private JPanel dashboardPanel() {
...
regBtn.addActionListener(e -> registerStudent());
viewBtn.addActionListener(e -> viewStudents());
roomBtn.addActionListener(e -> allocateRoom());
logoutBtn.addActionListener(e -> {
usernameField.setText("");
passwordField.setText("");
cardLayout.show(mainPanel, "Login");
});
}
Contains 4 actions: Register, View, Allocate, Logout.
7. Register Student
private void registerStudent() {
...
if (!name.isEmpty() && !room.isEmpty()) {
studentList.add(name + " - Room " + room);
JOptionPane.showMessageDialog(this, "Student Registered Successfully!");
} else {
JOptionPane.showMessageDialog(this, "All fields are required!");
}
}
Uses
JOptionPane
dialog to input student name and room.Saves into
studentList
.
8. View Students
private void viewStudents()
{
studentArea = new JTextArea();
if (studentList.isEmpty())
{
studentArea.setText("No students registered yet.");
}
else
{
for (String s : studentList)
{
studentArea.append(s + "\n");
}
}
}
Displays list of registered students.
9. Room Allocation Simulation
private void allocateRoom()
{
if (studentList.isEmpty())
{
JOptionPane.showMessageDialog(this, "No students available for room allocation.");
}
else
{
JOptionPane.showMessageDialog(this, "Rooms allocated successfully to all registered students.");
}
}
This is a simulated room allocation; it does not change any data.
10. Main Method
public static void main(String[] args)
{
SwingUtilities.invokeLater(HostelManagementSystem::new);
}
Launches the GUI on the Event Dispatch Thread.
source code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
public class HostelManagementSystem extends JFrame {
CardLayout cardLayout;
JPanel mainPanel;
JTextField usernameField, nameField, roomField;
JPasswordField passwordField;
JTextArea studentArea;
JLabel welcomeLabel;
ArrayList studentList = new ArrayList();
public HostelManagementSystem() {
setTitle("Hostel Management System");
setSize(600, 450);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
cardLayout = new CardLayout();
mainPanel = new JPanel(cardLayout);
mainPanel.add(loginPanel(), "Login");
mainPanel.add(dashboardPanel(), "Dashboard");
add(mainPanel);
cardLayout.show(mainPanel, "Login");
setVisible(true);
}
// 🔐 Login Panel
private JPanel loginPanel() {
JPanel panel = new JPanel(null);
panel.setBackground(new Color(255, 241, 210));
JLabel title = new JLabel("Hostel Login", SwingConstants.CENTER);
title.setFont(new Font("Arial", Font.BOLD, 24));
title.setBounds(180, 30, 240, 30);
panel.add(title);
JLabel userLabel = new JLabel("Username:");
userLabel.setBounds(120, 100, 100, 25);
panel.add(userLabel);
usernameField = new JTextField();
usernameField.setBounds(220, 100, 200, 25);
panel.add(usernameField);
JLabel passLabel = new JLabel("Password:");
passLabel.setBounds(120, 140, 100, 25);
panel.add(passLabel);
passwordField = new JPasswordField();
passwordField.setBounds(220, 140, 200, 25);
panel.add(passwordField);
JButton okBtn = new JButton("OK");
okBtn.setBounds(220, 200, 100, 30);
okBtn.setBackground(new Color(255, 153, 51));
okBtn.setForeground(Color.WHITE);
okBtn.addActionListener(e -> {
String user = usernameField.getText();
String pass = new String(passwordField.getPassword());
if (user.equals("admin") && pass.equals("1234")) {
welcomeLabel.setText("Welcome, " + user + "!");
cardLayout.show(mainPanel, "Dashboard");
} else {
JOptionPane.showMessageDialog(this, "Invalid Username or Password");
}
});
panel.add(okBtn);
return panel;
}
// 🏠 Dashboard Panel
private JPanel dashboardPanel() {
JPanel panel = new JPanel(null);
panel.setBackground(new Color(220, 248, 198));
welcomeLabel = new JLabel("Welcome!", SwingConstants.CENTER);
welcomeLabel.setFont(new Font("Arial", Font.BOLD, 20));
welcomeLabel.setBounds(180, 20, 250, 30);
panel.add(welcomeLabel);
JButton regBtn = new JButton("Register Student");
regBtn.setBounds(200, 80, 180, 30);
regBtn.addActionListener(e -> registerStudent());
panel.add(regBtn);
JButton viewBtn = new JButton("View Students");
viewBtn.setBounds(200, 130, 180, 30);
viewBtn.addActionListener(e -> viewStudents());
panel.add(viewBtn);
JButton roomBtn = new JButton("Allocate Room");
roomBtn.setBounds(200, 180, 180, 30);
roomBtn.addActionListener(e -> allocateRoom());
panel.add(roomBtn);
JButton logoutBtn = new JButton("Logout");
logoutBtn.setBounds(200, 240, 180, 30);
logoutBtn.setBackground(Color.LIGHT_GRAY);
logoutBtn.addActionListener(e -> {
usernameField.setText("");
passwordField.setText("");
cardLayout.show(mainPanel, "Login");
});
panel.add(logoutBtn);
return panel;
}
// 📝 Register Student
private void registerStudent() {
JPanel regPanel = new JPanel(new GridLayout(3, 2, 10, 10));
nameField = new JTextField();
roomField = new JTextField();
regPanel.add(new JLabel("Student Name:"));
regPanel.add(nameField);
regPanel.add(new JLabel("Room Number:"));
regPanel.add(roomField);
int result = JOptionPane.showConfirmDialog(this, regPanel, "Register Student",
JOptionPane.OK_CANCEL_OPTION);
if (result == JOptionPane.OK_OPTION) {
String name = nameField.getText();
String room = roomField.getText();
if (!name.isEmpty() && !room.isEmpty()) {
studentList.add(name + " - Room " + room);
JOptionPane.showMessageDialog(this, "Student Registered Successfully!");
} else {
JOptionPane.showMessageDialog(this, "All fields are required!");
}
}
}
// 📋 View Students
private void viewStudents() {
studentArea = new JTextArea();
if (studentList.isEmpty()) {
studentArea.setText("No students registered yet.");
} else {
for (String s : studentList) {
studentArea.append(s + "\n");
}
}
studentArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(studentArea);
scrollPane.setPreferredSize(new Dimension(300, 200));
JOptionPane.showMessageDialog(this, scrollPane, "Registered Students", JOptionPane.INFORMATION_MESSAGE);
}
// 🛏 Allocate Room (Simulation)
private void allocateRoom() {
if (studentList.isEmpty()) {
JOptionPane.showMessageDialog(this, "No students available for room allocation.");
} else {
JOptionPane.showMessageDialog(this, "Rooms allocated successfully to all registered students.");
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(HostelManagementSystem::new);
}
}
output



