|
| 1 | +import java.util.ArrayList; |
| 2 | +import java.util.List; |
| 3 | +import java.util.Scanner; |
| 4 | + |
| 5 | +class Patient { |
| 6 | + private int id; |
| 7 | + private String name; |
| 8 | + private int age; |
| 9 | + |
| 10 | + public Patient(int id, String name, int age) { |
| 11 | + this.id = id; |
| 12 | + this.name = name; |
| 13 | + this.age = age; |
| 14 | + } |
| 15 | + |
| 16 | + public int getId() { |
| 17 | + return id; |
| 18 | + } |
| 19 | + |
| 20 | + public String getName() { |
| 21 | + return name; |
| 22 | + } |
| 23 | + |
| 24 | + public int getAge() { |
| 25 | + return age; |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class Doctor { |
| 30 | + private int id; |
| 31 | + private String name; |
| 32 | + private String specialization; |
| 33 | + |
| 34 | + public Doctor(int id, String name, String specialization) { |
| 35 | + this.id = id; |
| 36 | + this.name = name; |
| 37 | + this.specialization = specialization; |
| 38 | + } |
| 39 | + |
| 40 | + public int getId() { |
| 41 | + return id; |
| 42 | + } |
| 43 | + |
| 44 | + public String getName() { |
| 45 | + return name; |
| 46 | + } |
| 47 | + |
| 48 | + public String getSpecialization() { |
| 49 | + return specialization; |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +class Appointment { |
| 54 | + private int id; |
| 55 | + private Patient patient; |
| 56 | + private Doctor doctor; |
| 57 | + private String date; |
| 58 | + |
| 59 | + public Appointment(int id, Patient patient, Doctor doctor, String date) { |
| 60 | + this.id = id; |
| 61 | + this.patient = patient; |
| 62 | + this.doctor = doctor; |
| 63 | + this.date = date; |
| 64 | + } |
| 65 | + |
| 66 | + public int getId() { |
| 67 | + return id; |
| 68 | + } |
| 69 | + |
| 70 | + public Patient getPatient() { |
| 71 | + return patient; |
| 72 | + } |
| 73 | + |
| 74 | + public Doctor getDoctor() { |
| 75 | + return doctor; |
| 76 | + } |
| 77 | + |
| 78 | + public String getDate() { |
| 79 | + return date; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +public class HospitalManagementSystem { |
| 84 | + public static void main(String[] args) { |
| 85 | + Scanner scanner = new Scanner(System.in); |
| 86 | + List<Patient> patients = new ArrayList<>(); |
| 87 | + List<Doctor> doctors = new ArrayList<>(); |
| 88 | + List<Appointment> appointments = new ArrayList<>(); |
| 89 | + int patientIdCounter = 1; |
| 90 | + int doctorIdCounter = 1; |
| 91 | + int appointmentIdCounter = 1; |
| 92 | + |
| 93 | + while (true) { |
| 94 | + System.out.println("Hospital Management System Menu:"); |
| 95 | + System.out.println("1. Add Patient"); |
| 96 | + System.out.println("2. Add Doctor"); |
| 97 | + System.out.println("3. Schedule Appointment"); |
| 98 | + System.out.println("4. View Appointments"); |
| 99 | + System.out.println("5. Exit"); |
| 100 | + System.out.print("Select an option (1/2/3/4/5): "); |
| 101 | + |
| 102 | + int option = scanner.nextInt(); |
| 103 | + |
| 104 | + switch (option) { |
| 105 | + case 1: |
| 106 | + scanner.nextLine(); |
| 107 | + System.out.print("Enter patient name: "); |
| 108 | + String patientName = scanner.nextLine(); |
| 109 | + System.out.print("Enter patient age: "); |
| 110 | + int patientAge = scanner.nextInt(); |
| 111 | + patients.add(new Patient(patientIdCounter, patientName, patientAge)); |
| 112 | + patientIdCounter++; |
| 113 | + System.out.println("Patient added successfully."); |
| 114 | + break; |
| 115 | + case 2: |
| 116 | + scanner.nextLine(); |
| 117 | + System.out.print("Enter doctor name: "); |
| 118 | + String doctorName = scanner.nextLine(); |
| 119 | + System.out.print("Enter doctor specialization: "); |
| 120 | + String doctorSpecialization = scanner.nextLine(); |
| 121 | + doctors.add(new Doctor(doctorIdCounter, doctorName, doctorSpecialization)); |
| 122 | + doctorIdCounter++; |
| 123 | + System.out.println("Doctor added successfully."); |
| 124 | + break; |
| 125 | + case 3: |
| 126 | + System.out.print("Enter patient ID: "); |
| 127 | + int patientId = scanner.nextInt(); |
| 128 | + System.out.print("Enter doctor ID: "); |
| 129 | + int doctorId = scanner.nextInt(); |
| 130 | + scanner.nextLine(); |
| 131 | + System.out.print("Enter appointment date: "); |
| 132 | + String appointmentDate = scanner.nextLine(); |
| 133 | + |
| 134 | + Patient patient = findPatientById(patients, patientId); |
| 135 | + Doctor doctor = findDoctorById(doctors, doctorId); |
| 136 | + |
| 137 | + if (patient != null && doctor != null) { |
| 138 | + appointments.add(new Appointment(appointmentIdCounter, patient, doctor, appointmentDate)); |
| 139 | + appointmentIdCounter++; |
| 140 | + System.out.println("Appointment scheduled successfully."); |
| 141 | + } else { |
| 142 | + System.out.println("Patient or doctor not found."); |
| 143 | + } |
| 144 | + break; |
| 145 | + case 4: |
| 146 | + System.out.println("Appointments:"); |
| 147 | + for (Appointment appointment : appointments) { |
| 148 | + System.out.println("Appointment ID: " + appointment.getId()); |
| 149 | + System.out.println("Patient: " + appointment.getPatient().getName()); |
| 150 | + System.out.println("Doctor: " + appointment.getDoctor().getName() + " (" + appointment.getDoctor().getSpecialization() + ")"); |
| 151 | + System.out.println("Date: " + appointment.getDate()); |
| 152 | + System.out.println(); |
| 153 | + } |
| 154 | + break; |
| 155 | + case 5: |
| 156 | + System.out.println("Exiting the Hospital Management System."); |
| 157 | + scanner.close(); |
| 158 | + System.exit(0); |
| 159 | + default: |
| 160 | + System.out.println("Invalid option. Please select 1, 2, 3, 4, or 5."); |
| 161 | + } |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private static Patient findPatientById(List<Patient> patients, int id) { |
| 166 | + for (Patient patient : patients) { |
| 167 | + if (patient.getId() == id) { |
| 168 | + return patient; |
| 169 | + } |
| 170 | + } |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + private static Doctor findDoctorById(List<Doctor> doctors, int id) { |
| 175 | + for (Doctor doctor : doctors) { |
| 176 | + if (doctor.getId() == id) { |
| 177 | + return doctor; |
| 178 | + } |
| 179 | + } |
| 180 | + return null; |
| 181 | + } |
| 182 | +} |
0 commit comments