google.com, pub-4617457846989927, DIRECT, f08c47fec0942fa0 Learn to enjoy every minute of your life.Only I can change my life.: Java program for a hospital management system

Thursday, March 13, 2025

Java program for a hospital management system

 Here's a simple Java program for a hospital management system:


```

import java.util.ArrayList;

import java.util.Scanner;


class Patient {

    private String name;

    private int age;

    private String disease;

    private String doctor;


    public Patient(String name, int age, String disease, String doctor) {

        this.name = name;

        this.age = age;

        this.disease = disease;

        this.doctor = doctor;

    }


    public String getName() {

        return name;

    }


    public int getAge() {

        return age;

    }


    public String getDisease() {

        return disease;

    }


    public String getDoctor() {

        return doctor;

    }

}


class Doctor {

    private String name;

    private String specialization;


    public Doctor(String name, String specialization) {

        this.name = name;

        this.specialization = specialization;

    }


    public String getName() {

        return name;

    }


    public String getSpecialization() {

        return specialization;

    }

}


public class HospitalManagement {

    private ArrayList<Patient> patients;

    private ArrayList<Doctor> doctors;

    private Scanner scanner;


    public HospitalManagement() {

        patients = new ArrayList<>();

        doctors = new ArrayList<>();

        scanner = new Scanner(System.in);

    }


    public void addPatient() {

        System.out.print("Enter patient name: ");

        String name = scanner.nextLine();

        System.out.print("Enter patient age: ");

        int age = scanner.nextInt();

        scanner.nextLine(); // Consume newline left-over

        System.out.print("Enter patient disease: ");

        String disease = scanner.nextLine();

        System.out.print("Enter doctor's name: ");

        String doctorName = scanner.nextLine();

        patients.add(new Patient(name, age, disease, doctorName));

    }


    public void addDoctor() {

        System.out.print("Enter doctor's name: ");

        String name = scanner.nextLine();

        System.out.print("Enter doctor's specialization: ");

        String specialization = scanner.nextLine();

        doctors.add(new Doctor(name, specialization));

    }


    public void displayPatients() {

        System.out.println("\nPatient List:");

        for (Patient patient : patients) {

            System.out.println("Name: " + patient.getName());

            System.out.println("Age: " + patient.getAge());

            System.out.println("Disease: " + patient.getDisease());

            System.out.println("Doctor: " + patient.getDoctor());

            System.out.println();

        }

    }


    public void displayDoctors() {

        System.out.println("\nDoctor List:");

        for (Doctor doctor : doctors) {

            System.out.println("Name: " + doctor.getName());

            System.out.println("Specialization: " + doctor.getSpecialization());

            System.out.println();

        }

    }


    public void run() {

        while (true) {

            System.out.println("\nHospital Management System");

            System.out.println("1. Add Patient");

            System.out.println("2. Add Doctor");

            System.out.println("3. Display Patients");

            System.out.println("4. Display Doctors");

            System.out.println("5. Exit");

            System.out.print("Enter your choice: ");

            int choice = scanner.nextInt();

            scanner.nextLine(); // Consume newline left-over

            switch (choice) {

                case 1:

                    addPatient();

                    break;

                case 2:

                    addDoctor();

                    break;

                case 3:

                    displayPatients();

                    break;

                case 4:

                    displayDoctors();

                    break;

                case 5:

                    System.exit(0);

                    break;

                default:

                    System.out.println("Invalid choice. Please try again.");

            }

        }

    }


    public static void main(String[] args) {

        HospitalManagement management = new HospitalManagement();

        management.run();

    }

}

```


*Example Output:*

```

Hospital Management System

1. Add Patient

2. Add Doctor

3. Display Patients

4. Display Doctors

5. Exit

Enter your choice: 1


Enter patient name: John Doe

Enter patient age: 30

Enter patient disease: Flu

Enter doctor's name: Dr. Smith


Hospital Management System

1. Add Patient

2. Add Doctor

3. Display Patients

4. Display Doctors

5. Exit

Enter your choice: 3


Patient List:

Name: John Doe

Age: 30

Disease: Flu

Doctor: Dr. Smith

```


This program defines three classes: `Patient`, `Doctor`, and `HospitalManagement`. The `Patient` class has private member variables for patient details and public methods for accessing those details. The `Doctor` class has private member variables for doctor details and public methods for accessing those details. The `HospitalManagement` class has private member variables for patient and doctor lists, as well as public methods for adding patients and doctors,

No comments:

Post a Comment

The rotation of money in business

 The rotation of money in business refers to the flow of funds within a company, encompassing various financial activities and transactions....