0% found this document useful (0 votes)
11 views24 pages

Document

The document outlines a Python program for a Hospital Management System that connects to a MySQL database. It includes functionalities for managing patients, doctors, appointments, billing, inventory, and room/beds, with options for adding, viewing, updating, and deleting records. The program also features a main menu for navigation and submenus for each management category.

Uploaded by

kakulyadav062007
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)
11 views24 pages

Document

The document outlines a Python program for a Hospital Management System that connects to a MySQL database. It includes functionalities for managing patients, doctors, appointments, billing, inventory, and room/beds, with options for adding, viewing, updating, and deleting records. The program also features a main menu for navigation and submenus for each management category.

Uploaded by

kakulyadav062007
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/ 24

import mysql.

connector

from tabulate import tabulate

import pandas as pd

import matplotlib.pyplot as plt

import numpy as np

import datetime

# Database Connection

mydb = mysql.connector.connect(host="localhost", user="root",


passwd="root", database="hospital_management_system")

mycursor = mydb.cursor()

print("""

*********************************

CLASS 12 - INFORMATICS PRACTICES

FINAL PROJECT

HOSPITAL MANAGEMENT SYSTEM

*********************************

""")

print("""

TEAM MEMBERS : KAKUL YADAV, RISHABH TIWARI, DIVYANSHU

""")

# Initialize Tables if they don't exist

def initialize_tables():

mycursor.execute("""
CREATE TABLE IF NOT EXISTS patients (

patient_id VARCHAR(25) PRIMARY KEY,

name VARCHAR(50) NOT NULL,

age INT NOT NULL,

gender VARCHAR(10) NOT NULL,

contact VARCHAR(25) NOT NULL

""")

mycursor.execute("""

CREATE TABLE IF NOT EXISTS doctors (

doctor_id VARCHAR(25) PRIMARY KEY,

name VARCHAR(50) NOT NULL,

specialization VARCHAR(50) NOT NULL,

contact VARCHAR(25) NOT NULL

""")

mycursor.execute("""

CREATE TABLE IF NOT EXISTS appointments (

patient_id VARCHAR(25) NOT NULL,

doctor_id VARCHAR(25) NOT NULL,

date DATE NOT NULL,

PRIMARY KEY(patient_id, doctor_id, date),

FOREIGN KEY (patient_id) REFERENCES patients(patient_id),

FOREIGN KEY (doctor_id) REFERENCES doctors(doctor_id)

""")

mycursor.execute("""
CREATE TABLE IF NOT EXISTS bills (

patient_id VARCHAR(25) NOT NULL,

amount INT NOT NULL,

date DATE NOT NULL,

PRIMARY KEY(patient_id, date),

FOREIGN KEY (patient_id) REFERENCES patients(patient_id)

""")

mycursor.execute("""

CREATE TABLE IF NOT EXISTS inventory (

category ENUM('medicine', 'equipment', 'supplies') NOT NULL,

item_id VARCHAR(25) PRIMARY KEY,

name VARCHAR(50) NOT NULL,

quantity INT NOT NULL,

price INT NOT NULL

""")

mycursor.execute("""

CREATE TABLE IF NOT EXISTS rooms (

type ENUM('general', 'icu') PRIMARY KEY,

available INT NOT NULL

""")

mycursor.execute("""

CREATE TABLE IF NOT EXISTS beds (

type ENUM('general', 'icu') PRIMARY KEY,

available INT NOT NULL


)

""")

initialize_tables()

# Patient Functions

def add_patient():

patient_id = input("Enter Patient ID: ")

name = input("Enter Patient Name: ")

age = input("Enter Patient Age: ")

gender = input("Enter Patient Gender: ")

contact = input("Enter Patient Contact: ")

mycursor.execute("INSERT INTO patients (patient_id, name, age, gender,


contact) VALUES (%s, %s, %s, %s, %s)",

(patient_id, name, age, gender, contact))

mydb.commit()

print("Patient added successfully!\n")

def view_patients():

mycursor.execute("SELECT * FROM patients")

patients = mycursor.fetchall()

if not patients:

print("No patients found!\n")

else:

for patient in patients:

print(f"ID: {patient[0]}, Name: {patient[1]}, Age: {patient[2]},


Gender: {patient[3]}, Contact: {patient[4]}\n")
def search_patient():

patient_id = input("Enter Patient ID to search: ")

mycursor.execute("SELECT * FROM patients WHERE patient_id = %s",


(patient_id,))

patient = mycursor.fetchone()

if patient:

print(f"ID: {patient[0]}, Name: {patient[1]}, Age: {patient[2]}, Gender:


{patient[3]}, Contact: {patient[4]}\n")

else:

print("Patient not found!\n")

def delete_patient():

patient_id = input("Enter Patient ID to delete: ")

mycursor.execute("DELETE FROM patients WHERE patient_id = %s",


(patient_id,))

mydb.commit()

if mycursor.rowcount:

print("Patient deleted successfully!\n")

else:

print("Patient not found!\n")

def update_patient():

patient_id = input("Enter Patient ID to update: ")

name = input("Enter new Patient Name (leave blank to keep current): ")

age = input("Enter new Patient Age (leave blank to keep current): ")

gender = input("Enter new Patient Gender (leave blank to keep current): ")
contact = input("Enter new Patient Contact (leave blank to keep current):
")

updates = []

if name:

updates.append(f"name = '{name}'")

if age:

updates.append(f"age = {age}")

if gender:

updates.append(f"gender = '{gender}'")

if contact:

updates.append(f"contact = '{contact}'")

if updates:

update_str = ", ".join(updates)

mycursor.execute(f"UPDATE patients SET {update_str} WHERE


patient_id = %s", (patient_id,))

mydb.commit()

print("Patient updated successfully!\n")

else:

print("No updates provided!\n")

# Doctor Functions

def add_doctor():

doctor_id = input("Enter Doctor ID: ")

name = input("Enter Doctor Name: ")

specialization = input("Enter Doctor Specialization: ")

contact = input("Enter Doctor Contact: ")

mycursor.execute("INSERT INTO doctors (doctor_id, name, specialization,


contact) VALUES (%s, %s, %s, %s)",
(doctor_id, name, specialization, contact))

mydb.commit()

print("Doctor added successfully!\n")

def view_doctors():

mycursor.execute("SELECT * FROM doctors")

doctors = mycursor.fetchall()

if not doctors:

print("No doctors found!\n")

else:

for doctor in doctors:

print(f"ID: {doctor[0]}, Name: {doctor[1]}, Specialization:


{doctor[2]}, Contact: {doctor[3]}\n")

def search_doctor():

doctor_id = input("Enter Doctor ID to search: ")

mycursor.execute("SELECT * FROM doctors WHERE doctor_id = %s",


(doctor_id,))

doctor = mycursor.fetchone()

if doctor:

print(f"ID: {doctor[0]}, Name: {doctor[1]}, Specialization: {doctor[2]},


Contact: {doctor[3]}\n")

else:

print("Doctor not found!\n")

def delete_doctor():

doctor_id = input("Enter Doctor ID to delete: ")


mycursor.execute("DELETE FROM doctors WHERE doctor_id = %s",
(doctor_id,))

mydb.commit()

if mycursor.rowcount:

print("Doctor deleted successfully!\n")

else:

print("Doctor not found!\n")

def update_doctor():

doctor_id = input("Enter Doctor ID to update: ")

name = input("Enter new Doctor Name (leave blank to keep current): ")

specialization = input("Enter new Doctor Specialization (leave blank to


keep current): ")

contact = input("Enter new Doctor Contact (leave blank to keep current):


")

updates = []

if name:

updates.append(f"name = '{name}'")

if specialization:

updates.append(f"specialization = '{specialization}'")

if contact:

updates.append(f"contact = '{contact}'")

if updates:

update_str = ", ".join(updates)

mycursor.execute(f"UPDATE doctors SET {update_str} WHERE doctor_id


= %s", (doctor_id,))

mydb.commit()

print("Doctor updated successfully!\n")


else:

print("No updates provided!\n")

# Appointment Functions

def schedule_appointment():

patient_id = input("Enter Patient ID: ")

doctor_id = input("Enter Doctor ID: ")

date = input("Enter Appointment Date (YYYY-MM-DD): ")

try:

date = datetime.datetime.strptime(date, "%Y-%m-%d").date()

mycursor.execute("INSERT INTO appointments (patient_id, doctor_id,


date) VALUES (%s, %s, %s)",

(patient_id, doctor_id, date))

mydb.commit()

print("Appointment scheduled successfully!\n")

except ValueError:

print("Invalid date format!\n")

def view_appointments():

mycursor.execute("SELECT * FROM appointments")

appointments = mycursor.fetchall()

if not appointments:

print("No appointments found!\n")

else:

for appointment in appointments:

print(f"Patient ID: {appointment[0]}, Doctor ID: {appointment[1]},


Date: {appointment[2]}\n")
def view_appointments_by_patient():

patient_id = input("Enter Patient ID: ")

mycursor.execute("SELECT * FROM appointments WHERE patient_id =


%s", (patient_id,))

appointments = mycursor.fetchall()

if not appointments:

print("No appointments found for this patient!\n")

else:

for appointment in appointments:

print(f"Doctor ID: {appointment[1]}, Date: {appointment[2]}\n")

def view_appointments_by_doctor():

doctor_id = input("Enter Doctor ID: ")

mycursor.execute("SELECT * FROM appointments WHERE doctor_id = %s",


(doctor_id,))

appointments = mycursor.fetchall()

if not appointments:

print("No appointments found for this doctor!\n")

else:

for appointment in appointments:

print(f"Patient ID: {appointment[0]}, Date: {appointment[2]}\n")

# Billing Functions

def add_bill():

patient_id = input("Enter Patient ID: ")

amount = input("Enter Bill Amount: ")


date = input("Enter Billing Date (YYYY-MM-DD): ")

try:

date = datetime.datetime.strptime(date, "%Y-%m-%d").date()

mycursor.execute("INSERT INTO bills (patient_id, amount, date) VALUES


(%s, %s, %s)",

(patient_id, amount, date))

mydb.commit()

print("Bill added successfully!\n")

except ValueError:

print("Invalid date format!\n")

def view_bills():

mycursor.execute("SELECT * FROM bills")

bills = mycursor.fetchall()

if not bills:

print("No bills found!\n")

else:

for bill in bills:

print(f"Patient ID: {bill[0]}, Amount: {bill[1]}, Date: {bill[2]}\n")

def view_bills_by_patient():

patient_id = input("Enter Patient ID: ")

mycursor.execute("SELECT * FROM bills WHERE patient_id = %s",


(patient_id,))

bills = mycursor.fetchall()

if not bills:

print("No bills found for this patient!\n")


else:

for bill in bills:

print(f"Amount: {bill[1]}, Date: {bill[2]}\n")

# Inventory Functions

def add_inventory():

category = input("Enter Inventory Category (medicine, equipment,


supplies): ")

item_id = input("Enter Item ID: ")

name = input("Enter Item Name: ")

quantity = input("Enter Item Quantity: ")

price = input("Enter Item Price: ")

mycursor.execute("INSERT INTO inventory (category, item_id, name,


quantity, price) VALUES (%s, %s, %s, %s, %s)",

(category, item_id, name, quantity, price))

mydb.commit()

print("Inventory item added successfully!\n")

def view_inventory():

mycursor.execute("SELECT * FROM inventory")

inventory = mycursor.fetchall()

if not inventory:

print("No inventory found!\n")

else:

for item in inventory:

print(f"Category: {item[0]}, ID: {item[1]}, Name: {item[2]},


Quantity: {item[3]}, Price: {item[4]}\n")
def update_inventory():

item_id = input("Enter Item ID to update: ")

name = input("Enter new Item Name (leave blank to keep current): ")

quantity = input("Enter new Quantity (leave blank to keep current): ")

price = input("Enter new Price (leave blank to keep current): ")

updates = []

if name:

updates.append(f"name = '{name}'")

if quantity:

updates.append(f"quantity = {quantity}")

if price:

updates.append(f"price = {price}")

if updates:

update_str = ", ".join(updates)

mycursor.execute(f"UPDATE inventory SET {update_str} WHERE item_id


= %s", (item_id,))

mydb.commit()

print("Inventory updated successfully!\n")

else:

print("No updates provided!\n")

# Room and Bed Management

def add_room():

room_type = input("Enter Room Type (general, icu): ")

available = input("Enter Available Rooms: ")

mycursor.execute("INSERT INTO rooms (type, available) VALUES (%s, %s)",


(room_type, available))
mydb.commit()

print("Room added successfully!\n")

def view_rooms():

mycursor.execute("SELECT * FROM rooms")

rooms = mycursor.fetchall()

if not rooms:

print("No rooms found!\n")

else:

for room in rooms:

print(f"Type: {room[0]}, Available: {room[1]}\n")

def update_room():

room_type = input("Enter Room Type (general, icu) to update: ")

available = input("Enter new Available Rooms: ")

mycursor.execute("UPDATE rooms SET available = %s WHERE type = %s",


(available, room_type))

mydb.commit()

print("Room updated successfully!\n")

def add_bed():

bed_type = input("Enter Bed Type (general, icu): ")

available = input("Enter Available Beds: ")

mycursor.execute("INSERT INTO beds (type, available) VALUES (%s, %s)",


(bed_type, available))

mydb.commit()

print("Bed added successfully!\n")


def view_beds():

mycursor.execute("SELECT * FROM beds")

beds = mycursor.fetchall()

if not beds:

print("No beds found!\n")

else:

for bed in beds:

print(f"Type: {bed[0]}, Available: {bed[1]}\n")

def update_bed():

bed_type = input("Enter Bed Type (general, icu) to update: ")

available = input("Enter new Available Beds: ")

mycursor.execute("UPDATE beds SET available = %s WHERE type = %s",


(available, bed_type))

mydb.commit()

print("Bed updated successfully!\n")

# View Reports

def display_chart():

mycursor.execute("SELECT * FROM inventory")

inventory = mycursor.fetchall()

df = pd.DataFrame(inventory, columns=['Category', 'Item ID', 'Name',


'Quantity', 'Price'])

categories = df.groupby('Category')['Quantity'].sum().index

quantities = df.groupby('Category')['Quantity'].sum().values

plt.pie(quantities, labels=categories, autopct='%1.1f%%', shadow=True)


plt.title("Inventory Distribution")

plt.show()

def display_statistics():

mycursor.execute("SELECT * FROM bills")

bills = mycursor.fetchall()

df = pd.DataFrame(bills, columns=['Patient ID', 'Amount', 'Date'])

df['Date'] = pd.to_datetime(df['Date'])

df.set_index('Date', inplace=True)

monthly_totals = df['Amount'].resample('M').sum()

months = np.arange(1, 13)

plt.plot(months, monthly_totals)

plt.xlabel('Month')

plt.ylabel('Total Bill Amount')

plt.title('Monthly Revenue')

plt.show()

# Main Menu

def main_menu():

while True:

print("""

**************************

HOSPITAL MANAGEMENT SYSTEM

**************************

1. Patient Management

2. Doctor Management
3. Appointment Management

4. Billing Management

5. Inventory Management

6. Room and Bed Management

7. View Reports

8. Exit

""")

choice = input("Enter your choice: ")

if choice == '1':

patient_menu()

elif choice == '2':

doctor_menu()

elif choice == '3':

appointment_menu()

elif choice == '4':

billing_menu()

elif choice == '5':

inventory_menu()

elif choice == '6':

room_bed_menu()

elif choice == '7':

reports_menu()

elif choice == '8':

print("Exiting the system...")

break
else:

print("Invalid choice, please try again!")

# Submenus

def patient_menu():

while True:

print("""

****** PATIENT MANAGEMENT ******

1. Add Patient

2. View Patients

3. Search Patient

4. Delete Patient

5. Update Patient

6. Go Back to Main Menu

""")

choice = input("Enter your choice: ")

if choice == '1':

add_patient()

elif choice == '2':

view_patients()

elif choice == '3':

search_patient()

elif choice == '4':

delete_patient()

elif choice == '5':

update_patient()
elif choice == '6':

break

else:

print("Invalid choice, please try again!")

def doctor_menu():

while True:

print("""

****** DOCTOR MANAGEMENT ******

1. Add Doctor

2. View Doctors

3. Search Doctor

4. Delete Doctor

5. Update Doctor

6. Go Back to Main Menu

""")

choice = input("Enter your choice: ")

if choice == '1':

add_doctor()

elif choice == '2':

view_doctors()

elif choice == '3':

search_doctor()

elif choice == '4':

delete_doctor()

elif choice == '5':


update_doctor()

elif choice == '6':

break

else:

print("Invalid choice, please try again!")

def appointment_menu():

while True:

print("""

****** APPOINTMENT MANAGEMENT ******

1. Schedule Appointment

2. View Appointments

3. View Appointments by Patient

4. View Appointments by Doctor

5. Go Back to Main Menu

""")

choice = input("Enter your choice: ")

if choice == '1':

schedule_appointment()

elif choice == '2':

view_appointments()

elif choice == '3':

view_appointments_by_patient()

elif choice == '4':

view_appointments_by_doctor()

elif choice == '5':


break

else:

print("Invalid choice, please try again!")

def billing_menu():

while True:

print("""

****** BILLING MANAGEMENT ******

1. Add Bill

2. View Bills

3. View Bills by Patient

4. Go Back to Main Menu

""")

choice = input("Enter your choice: ")

if choice == '1':

add_bill()

elif choice == '2':

view_bills()

elif choice == '3':

view_bills_by_patient()

elif choice == '4':

break

else:

print("Invalid choice, please try again!")

def inventory_menu():
while True:

print("""

****** INVENTORY MANAGEMENT ******

1. Add Inventory Item

2. View Inventory

3. Update Inventory

4. Go Back to Main Menu

""")

choice = input("Enter your choice: ")

if choice == '1':

add_inventory()

elif choice == '2':

view_inventory()

elif choice == '3':

update_inventory()

elif choice == '4':

break

else:

print("Invalid choice, please try again!")

def room_bed_menu():

while True:

print("""

****** ROOM & BED MANAGEMENT ******

1. Add Room

2. View Rooms
3. Update Room

4. Add Bed

5. View Beds

6. Update Bed

7. Go Back to Main Menu

""")

choice = input("Enter your choice: ")

if choice == '1':

add_room()

elif choice == '2':

view_rooms()

elif choice == '3':

update_room()

elif choice == '4':

add_bed()

elif choice == '5':

view_beds()

elif choice == '6':

update_bed()

elif choice == '7':

break

else:

print("Invalid choice, please try again!")

def reports_menu():

while True:
print("""

****** REPORTS & STATISTICS ******

1. Display Inventory Chart

2. Display Monthly Revenue Statistics

3. Go Back to Main Menu

""")

choice = input("Enter your choice: ")

if choice == '1':

display_chart()

elif choice == '2':

display_statistics()

elif choice == '3':

break

else:

print("Invalid choice, please try again!")

# Start the system

main_menu()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy