DEV Community

Cover image for Java Learning Series - Patient Management System
G Gokul
G Gokul

Posted on

Java Learning Series - Patient Management System

Patient Management System:

PatientInfo.java

public class PatientInfo
{

    private int id;
    private String name;
    private int age;
    private String diagnosis; 


public PatientInfo(int id, String name,int age, String diagnosis){
    this.id = id;
    this.name = name;
    this.age = age;
    this.diagnosis = diagnosis;
}

public int getId(){
    return this.id;
}

public void setId(int id){
    this.id = id;
}

public String getName(){
    return this.name;
}

public void setName(String name){
    this.name = name;
}

public int getAge(){
    return this.age;
}

public void setAge(int age){
    this.age = age;
}

public String getDiagnosis(){
    return this.diagnosis;
}

public void setDiagnosis(String diagnosis){
    this.diagnosis = diagnosis;
}

}
Enter fullscreen mode Exit fullscreen mode

PatientInfoService.java

import java.util.ArrayList;

public class PatientInfoService
{

    ArrayList<PatientInfo> inventory = new ArrayList();

public String addPatientInfo(PatientInfo patient){
    boolean result = inventory.add(patient);
    if(result){
         return "success";
    }else{
         return "failed";
    }
}
public void getPatientInfo(int patientId){
    boolean find = false;
    for(PatientInfo patient : inventory){
         if(patient.getId() == patientId){
             int id = patient.getId();
             String name = patient.getName();
             int age = patient.getAge();
             String diagnosis = patient.getDiagnosis();
             System.out.println(id+" - "+name+" - "+age+" - "+diagnosis);
             find = true;
         }
    }
    if(find == false){
        System.out.println("patientinfo not found");
    }
}

public void getPatientInfo(){
    for(PatientInfo patient : inventory){
        int id = patient.getId();
        String name = patient.getName();
        int age = patient.getAge();
        String diagnosis = patient.getDiagnosis();
        System.out.println(id+" - "+name+" - "+age+" - "+diagnosis);
    }
}



public void deletePatientInfo(int id) {
    boolean find = false;

    for (PatientInfo patient : inventory) {
        if (patient.getId() == id) {
            inventory.remove(patient);
            find = true;
            break;
        }
    }

    if (find == true) {
        System.out.println("patientinfo deleted");
    } else {
        System.out.println("patientinfo not found");
    }
}             

public void updatePatientInfo(PatientInfo patientInformation){
        boolean find = false;
        for(PatientInfo patient : inventory){
            if(patient.getId() == patientInformation.getId()){
               patient.setName(patientInformation.getName());
               patient.setAge(patientInformation.getAge());
               patient.setDiagnosis(patientInformation.getDiagnosis());
                find = true;
            }
        }
        if(find == false){
        System.out.println("patientinfo not updated");
        }else{
        System.out.println("patientinfo updated");
        }
}



public void getMaxAge() {
    if (inventory.isEmpty()) {
        System.out.println("No patient information available");
        return;
    }

    int maxAge = inventory.get(0).getAge();

    for (PatientInfo patient : inventory) {
        if (patient.getAge() > maxAge) {
            maxAge = patient.getAge();
        }
    }

    System.out.println("Maximum age: " + maxAge);
}

public void getMinAge() {
    if (inventory.isEmpty()) {
        System.out.println("No patient information available");
        return;
    }

    int minAge = inventory.get(0).getAge();

    for (PatientInfo patient : inventory) {
        if (patient.getAge() < minAge) {
            minAge = patient.getAge();
        }
    }

    System.out.println("Minimum age: " + minAge);
}

public void getAvgAge() {
    if (inventory.isEmpty()) {
        System.out.println("No patient information available");
        return;
    }

    int totalAge = 0;

    for (PatientInfo patient : inventory) {
        totalAge = totalAge + patient.getAge();
    }
    int averageAge = totalAge / inventory.size();
    System.out.println("Average age: " + averageAge);
}


}
Enter fullscreen mode Exit fullscreen mode

PatientInfoController.java

public class PatientInfoController
{

public static void main(String[] args){


    PatientInfo patient1 = new PatientInfo(1,"arun",45,"fever");
    PatientInfo patient2 = new PatientInfo(2,"ashok",25,"cold");

    PatientInfoService service = new PatientInfoService();
    String result = service.addPatientInfo(patient1);
    System.out.println(result);
    service.addPatientInfo(patient2);
    service.getPatientInfo();
    service.getPatientInfo(3);
    service.updatePatientInfo(new PatientInfo(2,"abi",34,"cough"));
    service.getPatientInfo();
    service.getMaxAge();
    service.getMinAge();
    service.getAvgAge();
    service.deletePatientInfo(2);
    service.getPatientInfo();
}

}
Enter fullscreen mode Exit fullscreen mode

output:

success
1 - arun - 45 - fever
2 - ashok - 25 - cold
patientinfo not found
patientinfo updated
1 - arun - 45 - fever
2 - abi - 34 - cough
Maximum age: 45
Minimum age: 34
Average age: 39
patientinfo deleted
1 - arun - 45 - fever

Top comments (0)