0% found this document useful (0 votes)
31 views5 pages

Import Java

The document contains a Java program for a Hospital Management System that allows users to add and view patients and doctors. It defines two classes, Patient and Doctor, each with attributes and a method to display their information. The main program runs a loop to interact with the user, providing options to manage patient and doctor records.

Uploaded by

Sneha Dwivedi
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)
31 views5 pages

Import Java

The document contains a Java program for a Hospital Management System that allows users to add and view patients and doctors. It defines two classes, Patient and Doctor, each with attributes and a method to display their information. The main program runs a loop to interact with the user, providing options to manage patient and doctor records.

Uploaded by

Sneha Dwivedi
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/ 5

import java.util.

ArrayList;

import java.util.Scanner;

class Patient {

int id;

String name;

int age;

String disease;

Patient(int id, String name, int age, String disease) {

this.id = id;

this.name = name;

this.age = age;

this.disease = disease;

@Override

public String toString() {

return "Patient ID: " + id + ", Name: " + name + ", Age: " + age + ", Disease: " + disease;

class Doctor {

int id;

String name;

String specialization;

Doctor(int id, String name, String specialization) {

this.id = id;
this.name = name;

this.specialization = specialization;

@Override

public String toString() {

return "Doctor ID: " + id + ", Name: " + name + ", Specialization: " + specialization;

public class HospitalManagementSystem {

static ArrayList<Patient> patients = new ArrayList<>();

static ArrayList<Doctor> doctors = new ArrayList<>();

static Scanner scanner = new Scanner(System.in);

public static void main(String[] args) {

while (true) {

System.out.println("\n--- Hospital Management System ---");

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

System.out.println("2. View Patients");

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

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

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

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

int choice = scanner.nextInt();

switch (choice) {

case 1 -> addPatient();

case 2 -> viewPatients();


case 3 -> addDoctor();

case 4 -> viewDoctors();

case 5 -> {

System.out.println("Exiting the system. Goodbye!");

System.exit(0);

default -> System.out.println("Invalid choice! Please try again.");

static void addPatient() {

System.out.print("Enter Patient ID: ");

int id = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Enter Patient Name: ");

String name = scanner.nextLine();

System.out.print("Enter Patient Age: ");

int age = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Enter Patient Disease: ");

String disease = scanner.nextLine();

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

System.out.println("Patient added successfully!");

static void viewPatients() {

if (patients.isEmpty()) {
System.out.println("No patients found.");

} else {

System.out.println("\n--- Patient List ---");

for (Patient patient : patients) {

System.out.println(patient);

static void addDoctor() {

System.out.print("Enter Doctor ID: ");

int id = scanner.nextInt();

scanner.nextLine(); // Consume newline

System.out.print("Enter Doctor Name: ");

String name = scanner.nextLine();

System.out.print("Enter Specialization: ");

String specialization = scanner.nextLine();

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

System.out.println("Doctor added successfully!");

static void viewDoctors() {

if (doctors.isEmpty()) {

System.out.println("No doctors found.");

} else {

System.out.println("\n--- Doctor List ---");

for (Doctor doctor : doctors) {

System.out.println(doctor);
}

You might also like