-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHospital.py
More file actions
136 lines (128 loc) · 5.52 KB
/
Hospital.py
File metadata and controls
136 lines (128 loc) · 5.52 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
import mysql.connector
class Hospital:
def __init__(self, host, port, user, password, database):
self.connection = mysql.connector.connect(
host=host,
port=port,
user=user,
password=password,
database=database
)
self.cursor = self.connection.cursor()
def add_doctor(self, doctors_id, name, specialty):
query = "INSERT INTO doctors (id, name, specialty) VALUES (%s, %s, %s)"
values = (doctors_id, name, specialty)
self.cursor.execute(query, values)
self.connection.commit()
def get_doctors(self):
query = "SELECT * FROM doctors"
self.cursor.execute(query)
return self.cursor.fetchall()
def add_patient(self, name, age, gender):
query = "INSERT INTO patients (name, age, gender) VALUES (%s, %s, %s)"
values = (name, age, gender)
self.cursor.execute(query, values)
self.connection.commit()
def details_patient(self, patient_id, doctor_id):
query = "SELECT id, name, age, gender, doctor_id, doctor_name FROM patients WHERE id = %s AND doctor_id = %s"
values = (patient_id, doctor_id)
self.cursor.execute(query, values)
row = self.cursor.fetchone()
if row:
return {
"id": row[0],
"name": row[1],
"age": row[2],
"gender": row[3],
"doctor_id": row[4],
"doctor_name": row[5]
}
else:
return None
def get_patients(self):
query = "SELECT * FROM patients"
self.cursor.execute(query)
return self.cursor.fetchall()
def close(self):
self.cursor.close()
self.connection.close()
def assign_doctor_to_patient(self, patient_id, doctor_id):
query = "UPDATE patients SET doctor_id = %s WHERE id = %s"
values = (doctor_id, patient_id)
self.cursor.execute(query, values)
self.connection.commit()
if self.cursor.rowcount > 0:
# Fetch the doctor's name to update in the patient's record
self.cursor.execute("SELECT name FROM doctors WHERE id = %s", (doctor_id,))
doctor = self.cursor.fetchone()
if doctor:
doctor_name = doctor[0]
update_query = "UPDATE patients SET doctor_name = %s WHERE id = %s"
update_values = (doctor_name, patient_id)
self.cursor.execute(update_query, update_values)
self.connection.commit()
return True
return False
# Now lets add console inputs to add doctors and patients
def main():
print("#################### Welcome to the Hospital Management System ####################")
print("########### ------------------ Connecting to the database... ------------------ ###########")
hospital = Hospital(host="localhost", port=3306, user="root", password="Root", database="hospital")
print("########### ------------------ Connected to the database. ------------------ ###########")
print("Please choose an option:")
while True:
print("1. Add Doctor")
print("2. Add Patient")
print("3. Assign Doctor to Patient")
print("4. View Doctors")
print("5. View Patients")
print("6. Exit")
choice = input("Enter your choice: ")
# add doctor option
if choice == '1':
doctors_id = int(input("Enter Doctor ID: "))
name = input("Enter Doctor Name: ")
specialty = input("Enter Doctor Specialty: ")
hospital.add_doctor(doctors_id, name, specialty)
print("-------------------------------")
print("Doctor added successfully.")
print("-------------------------------")
# Add patient option
elif choice == '2':
name = input("Enter Patient Name: ")
age = int(input("Enter Patient Age: "))
gender = input("Enter Patient Gender: ")
hospital.add_patient(name, age, gender)
print("-------------------------------")
print("Patient added successfully.")
print("-------------------------------")
# Assign doctor to patient option
elif choice == '3':
patient_id = int(input("Enter Patient ID: "))
doctor_id = int(input("Enter Doctor ID: "))
if hospital.assign_doctor_to_patient(patient_id, doctor_id):
print("-------------------------------")
print("Doctor assigned to patient successfully.")
print("-------------------------------")
else:
print("Failed to assign doctor. Please check the IDs.")
# View doctors option
elif choice == '4':
doctors = hospital.get_doctors()
for doc in doctors:
print("-------------------------------")
print(f"ID: {doc[0]}, Name: {doc[1]}, Specialty: {doc[2]}")
print("-------------------------------")
# View patients option
elif choice == '5':
patients = hospital.get_patients()
for pat in patients:
print("-------------------------------")
print(f"ID: {pat[0]}, Name: {pat[1]}, Age: {pat[2]}, Gender: {pat[3]}, Doctor ID: {pat[4]}, Doctor Name: {pat[5]}")
print("-------------------------------")
# Exit option
elif choice == '6':
hospital.close()
print("Exiting...")
break
main()