import csv
from date me import date me
# File paths
PATIENTS_FILE = "pa [Link]"
DOCTORS_FILE = "[Link]"
APPOINTMENTS_FILE = "[Link]"
def read_csv(file_path):
"""Reads a CSV file and returns the data as a list of dic onaries."""
try:
with open(file_path, mode="r", newline="") as file:
return list([Link](file))
except FileNotFoundError:
return []
def write_csv(file_path, fieldnames, data):
"""Writes a list of dic onaries to a CSV file."""
with open(file_path, mode="w", newline="") as file:
writer = [Link](file, fieldnames=fieldnames)
[Link]()
[Link](data)
def display_pa ents():
data = read_csv(PATIENTS_FILE)
if data:
print("\nPa ent Records:")
for row in data:
print(f"ID: {row['pid']}, Name: {row['pname']}, Cell: {row['pcell']}")
else:
print("\nNo pa ents found.")
def display_doctors():
data = read_csv(DOCTORS_FILE)
if data:
print("\nDoctor Records:")
for row in data:
print(f"ID: {row['did']}, Name: {row['dname']}, Cell: {row['dcell']}")
else:
print("\nNo doctors found.")
def display_appointments():
data = read_csv(APPOINTMENTS_FILE)
if data:
print("\nAppointment Records:")
for row in data:
print(f"Transac on ID: {row[' d']}, Pa ent ID: {row['pid']}, "
f"Doctor ID: {row['did']}, Fees: {row['fees']}, Date: {row['Adate']}")
else:
print("\nNo appointments found.")
def add_pa ent():
data = read_csv(PATIENTS_FILE)
pid = input("Enter Pa ent ID: ")
pname = input("Enter Pa ent Name: ")
pcell = input("Enter Pa ent Cell: ")
[Link]({"pid": pid, "pname": pname, "pcell": pcell})
write_csv(PATIENTS_FILE, ["pid", "pname", "pcell"], data)
print("Pa ent added successfully!")
def add_doctor():
data = read_csv(DOCTORS_FILE)
did = input("Enter Doctor ID: ")
dname = input("Enter Doctor Name: ")
dcell = input("Enter Doctor Cell: ")
[Link]({"did": did, "dname": dname, "dcell": dcell})
write_csv(DOCTORS_FILE, ["did", "dname", "dcell"], data)
print("Doctor added successfully!")
def add_appointment():
pa ents = read_csv(PATIENTS_FILE)
doctors = read_csv(DOCTORS_FILE)
if not pa ents:
print("No pa ents available. Add pa ents first.")
return
if not doctors:
print("No doctors available. Add doctors first.")
return
data = read_csv(APPOINTMENTS_FILE)
d = input("Enter Transac on ID: ")
pid = input("Enter Pa ent ID: ")
did = input("Enter Doctor ID: ")
fees = input("Enter Fees: ")
Adate = input("Enter Appointment Date (YYYY-MM-DD): ")
# Validate pa ent and doctor IDs
if not any(pa ent["pid"] == pid for pa ent in pa ents):
print("Invalid Pa ent ID.")
return
if not any(doctor["did"] == did for doctor in doctors):
print("Invalid Doctor ID.")
return
# Validate date format
try:
date [Link] me(Adate, "%Y-%m-%d")
except ValueError:
print("Invalid date format. Use YYYY-MM-DD.")
return
[Link]({" d": d, "pid": pid, "did": did, "fees": fees, "Adate": Adate})
write_csv(APPOINTMENTS_FILE, [" d", "pid", "did", "fees", "Adate"], data)
print("Appointment added successfully!")
def main():
while True:
print("\n1. Display Pa ents")
print("2. Display Doctors")
print("3. Display Appointments")
print("4. Add Pa ent")
print("5. Add Doctor")
print("6. Add Appointment")
print("7. Exit")
choice = input("Enter your choice: ")
if choice == "1":
display_pa ents()
elif choice == "2":
display_doctors()
elif choice == "3":
display_appointments()
elif choice == "4":
add_pa ent()
elif choice == "5":
add_doctor()
elif choice == "6":
add_appointment()
elif choice == "7":
print("Exi ng program.")
break
else:
print("Invalid choice. Try again.")
if __name__ == "__main__":
main()