Implementation of a Hospital management system using ArrayList in Java

By | November 21, 2019

This code explains the implementation of a hospital management system using ArrayList. This application has a provision to insert, modify, delete, and display patient records such as name, age, sex, address, illness, hospital charges. The records are stored in an ArrayList. Arraylist is a class in Java that implements the List interface. ArrayList offers to store data in an array format and it is very flexible to sort, modify records.

This Hospital management application offers the following operations.

1. Register a patient with records such as name, age, sex, address, illness, hospital charges
2. Update patient details
3. Display patient details
4. Remove inactive patients. A patient is treated as inactive if he has been admitted to the hospital for more than 15 days.

hospital management system using ArrayList

 

Suggested Read:

The following video explains the execution of the Hospital management application:

 

HospitalProcess.java

package arraylist.hospital;
import java.text.SimpleDateFormat;
import java.util.*;
import java.io.*;
public class HospitalProcess {
static ArrayList<Patient> arl  = new ArrayList<Patient>();
static String joindate;
public static void main(String[] args) throws IOException {
char l='i';
Calendar currentDate = Calendar.getInstance();
SimpleDateFormat formatter= new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
joindate = formatter.format(currentDate.getTime());
Patient p1 = new Patient(1,"Raja", "Miyapur,Hyderabad", 40, 'm', "Fever", 250f, joindate);
arl.add(p1);
Patient p2 = new Patient(2,"Rani", "Madhapur,Hyderabad", 45, 'f', "Cough", 250f, joindate);
arl.add(p2);
Patient p3 = new Patient(3,"James", "Hydernagar,Hyderabad", 56, 'm', "Cancer", 250f, joindate);
arl.add(p3);
do
{
Patient p = new Patient();
System.out.println("Enter 1 to register a Patient \n 2 to update Patient details \n 3 to deactivate Patient details \n 4 to display Patient details");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int choice = Integer.parseInt(br.readLine());
switch(choice)
{
case 1:
p.registerPatient();
System.out.println("Patient Registered Successfully !!!");
p.showPatientDetails();
break;
case 2:
p.showPatientDetails();
p.updatePatientDetails();			 
break;
case 3:
p.removeInactivePatient();
break;
case 4:
Collections.sort(arl);
p.showPatientDetails();
break;
default:
System.out.println("Patient does not exist with the entered ID");
System.out.println("Try again");
break;
}		 
System.out.println("Do you want to continue selecting options (y/n):");
l=(char)br.read();
}while(l=='y');	
}/*End of main() method */
} /************** End of HospitalProcess class ******************/
/************ Patient class to implement operations  *******************/
class Patient implements Comparable<Patient> 
{
int patientID;
String patientName;
String patientAddress;
int age;
char patientSex;
String patientIllness;
float amountForAppointment;
String joindate;
HospitalProcess hm = new HospitalProcess();
Patient() {}
public int compareTo(Patient p)
{
return this.patientID = p.patientID;
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Patient(int patientID,String patientName,String patientAddress,int age,char patientSex,String patientIllness,float amountForAppointment, String joindate)
{
this.patientID=patientID;
this.patientName=patientName;
this.patientAddress=patientAddress;
this.age=age;
this.patientSex=patientSex;
this.patientIllness=patientIllness;
this.amountForAppointment=amountForAppointment;
this.joindate = joindate;
}
void registerPatient()throws IOException //function to insert new patient records
{
Patient pr = new Patient();
@SuppressWarnings("static-access")
int patientID= hm.arl.size();
pr.patientID = patientID+1;
System.out.println("Enter Patient name:");
pr.patientName = br.readLine();
System.out.println("Enter Patient Address:");
pr.patientAddress = br.readLine();
System.out.println("Enter Patient Age:");
pr.age = Integer.parseInt(br.readLine());
System.out.println("Enter Patient Sex:");
String temp = br.readLine();
pr.patientSex =temp.charAt(0);
System.out.println("Enter Patient Illness:");
pr.patientIllness = br.readLine();
System.out.println("Enter the amount patient needs to pay for appointment:");
pr.amountForAppointment = Float.parseFloat(br.readLine());
pr.joindate = hm.joindate;
hm.arl.add(pr);
}
@SuppressWarnings("static-access")
void removeInactivePatient() throws IOException //function to remove patient records
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Patient ID:");
int id1 = Integer.parseInt(br.readLine());
int flag=0;
int flag2=0;
String presentdate;
String afteraddingdays;
for(int i=0; i<hm.arl.size(); i++)
{
if(id1 != hm.arl.get(i).patientID)
{
flag=0;
}
else if(id1 == hm.arl.get(i).patientID)
{
presentdate = hm.arl.get(i).joindate; 
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DAY_OF_MONTH, 15);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MMM/dd HH:mm:ss");
afteraddingdays = formatter.format(cal.getTime());
flag=1;
if(presentdate == afteraddingdays)
{
hm.arl.remove(i);
System.out.println("Patient deleted as his validity expired");
flag2=1;
}
else{
flag2=0;
}
}
}		 
if((flag) == 0)
{
System.out.println("Patient with the entered ID does not exist");
}
if(flag2==0)
{
System.out.println("Patient has still days left before his appointment expires");
}
}
/*Function to update patient records*/
void updatePatientDetails() throws IOException
{
char l='n';
Patient p2 = new Patient(patientID, patientName, patientAddress, age, patientSex, patientIllness, amountForAppointment, joindate);
do{
System.out.println("Enter patient ID you want to update:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int idnum = Integer.parseInt(br.readLine());
for(int i=0; i<hm.arl.size(); i++)
{
if(idnum == hm.arl.get(i).patientID)
{
System.out.println("Enter 1 to change patient's name \n 2 to change patient's address \n 3 to change patient's age \n 4 to change patient's illness \n 5 to change registration fees along with the medical expenses");
int ch = Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter new patient's name:");
p2.patientName = br.readLine();
p2.patientID= hm.arl.get(i).patientID;
p2.patientAddress= hm.arl.get(i).patientAddress;
p2.age=hm.arl.get(i).age;
p2.patientSex=hm.arl.get(i).patientSex;
p2.patientIllness = hm.arl.get(i).patientIllness;
p2.amountForAppointment = hm.arl.get(i).amountForAppointment;
p2.joindate = hm.arl.get(i).joindate;
hm.arl.set(i,p2);
System.out.println("Patient updated !!!");
break;
case 2:
System.out.println("Enter new patient Address:");
p2.patientAddress = br.readLine();
p2.patientID = hm.arl.get(i).patientID;
p2.patientName = hm.arl.get(i).patientName;
p2.age = hm.arl.get(i).age;
p2.patientSex = hm.arl.get(i).patientSex;
p2.patientIllness = hm.arl.get(i).patientIllness;
p2.amountForAppointment = hm.arl.get(i).amountForAppointment;
p2.joindate = hm.arl.get(i).joindate;
hm.arl.set(i,p2);
System.out.println("Patient updated !!!");
break;
case 3:
System.out.println("Enter new Patient age:");
p2.age = Integer.parseInt(br.readLine());
p2.patientID = hm.arl.get(i).patientID;
p2.patientName = hm.arl.get(i).patientName;
p2.patientAddress = hm.arl.get(i).patientAddress;
p2.patientSex = hm.arl.get(i).patientSex;
p2.patientIllness = hm.arl.get(i).patientIllness;
p2.amountForAppointment = hm.arl.get(i).amountForAppointment;
p2.joindate= hm.arl.get(i).joindate;
hm.arl.set(i,p2);
System.out.println("Patient updated !!!");
break;
case 4:
System.out.println("Enter new Patient illness:");
p2.age = hm.arl.get(i).age;
p2.patientID= hm.arl.get(i).patientID;
p2.patientName = hm.arl.get(i).patientName;
p2.patientAddress = hm.arl.get(i).patientAddress;
p2.patientSex = hm.arl.get(i).patientSex;
p2.patientIllness = br.readLine();
p2.amountForAppointment = hm.arl.get(i).amountForAppointment;
p2.joindate = hm.arl.get(i).joindate;
hm.arl.set(i,p2);
System.out.println("Patient updated !!!");
break;
case 5:
System.out.println("Enter the new amount that patient needs to pay:");
p2.age = hm.arl.get(i).age;
p2.patientID = hm.arl.get(i).patientID;
p2.patientName = hm.arl.get(i).patientName;
p2.patientAddress = hm.arl.get(i).patientAddress;
p2.patientSex = hm.arl.get(i).patientSex;
p2.patientIllness = hm.arl.get(i).patientIllness;
p2.amountForAppointment = Float.parseFloat(br.readLine());
p2.joindate = hm.arl.get(i).joindate;
hm.arl.set(i,p2);
System.out.println("Patient details updated !!!");
break;
default:
System.out.println("Invalid choice.");
break;
} /*End of switch block */
}/*End of if block */
} /*End of for block */
System.out.println("Do you want to continue updating (y/n):");
l=(char)br.read();
}while(l=='y'); /*End of do-while block */
}	 
/* Function to display patients details*/
void showPatientDetails()
{
System.out.println("patient-ID \t Patient-Name \t Address \t\t Age \t Sex \t Illness \t Fees \t Join-date");	 
for(int i=0; i<hm.arl.size(); i++)
{	 
System.out.println(hm.arl.get(i).patientID + " \t\t "+hm.arl.get(i).patientName+" \t\t "+hm.arl.get(i).patientAddress+" \t "+hm.arl.get(i).age+" \t "+hm.arl.get(i).patientSex+" \t "+hm.arl.get(i).patientIllness+" \t\t "+hm.arl.get(i).amountForAppointment+" \t "+hm.arl.get(i).joindate);
}
}				
} /********* End of Patient class **************/
Suggested articles for you:

21 thoughts on “Implementation of a Hospital management system using ArrayList in Java

  1. Fatma

    Could this application be implemented using Linked List?

    i have a project i can’t do it ?! can you help me

    Reply
    1. Ravi Bandakkanavar Post author

      The same application can be done using LinkedList but you need to make necessary changes as it maintains order.

      Reply
  2. Alex

    Could this application be implemented using Linked List?

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Alex,

      We can make use of LinkedList but as LinkedList maintains the order of insertion we need to make desirable changes in the application.

      Reply
  3. kalyansai

    sir the code is getting error in following lines can you please say error
    package arraylist.hospital;
    public class HospitalProcess
    class Patient implements Comparable {

    Reply
  4. Raghav Shukla

    Sir,
    The code when copy-pasted dosen’t runs in any compiler or any online compilers.
    I have watched your video but got no clues about it.
    Please help , I am in pain.

    Thanks

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Raghav,

      Thanks for mentioning. I did see two errors in the code. Modified following two lines.
      static ArrayList arl = new ArrayList();
      class Patient implements Comparable

      You can check it now.

      Reply
  5. amit yadav

    hello sir,
    the code shows 48 errors when run.
    please revert back

    Reply
    1. May Ann Garnade

      Sir, can i change the names and informations to be inputed?

      Reply
  6. ANURAG VERMA

    i have an error in laste line
    Exp8.java:249: error: class, interface, or enum expected
    }/*End of patient class */
    ^
    please help me with that my email is galaxypegasis8@gmail.com

    Reply
      1. Anurag Verma

        Sir can you please send me the working code I need your help I have to submit it by tomorrow .. eroor is showing and I also see the video many times please just compile the code and give me the correct one …rest of your code is awesome but please resolve the eroor sir we need ur help at this time. Please give me correct working code I also give my email to you… Please….

        Reply
        1. Ravi Bandakkanavar Post author

          Hi Anurag,

          Thanks did go through the code after seeing your comment. I could see two errors in the code. Modified following two lines.
          static ArrayList arl = new ArrayList();
          class Patient implements Comparable

          You can check it now.

  7. Raman

    HospitalProcess hm = new HospitalProcess();
    this will show error message like cannot find symbol

    Reply

Did it help? Comment here..