Clase de alergia en Java

Sheeraz Gul 12 octubre 2023
Clase de alergia en Java

Este tutorial demuestra cómo crear una clase llamada Alergia, que comprueba si los pacientes tienen alergia o no en Java.

Crear una clase de alergia en Java

Una clase de alergia significa una clase que puede detectar la alergia de un paciente. Podemos crear una clase en Java para la alergia, que establecerá y obtendrá la gravedad del nombre de la alergia.

Para respaldar este sistema, necesitaremos crear otras clases para ejecutar el sistema en función de las alergias del paciente o si el paciente tiene alergia u otras enfermedades.

Vamos a crear un sistema para detectar la alergia del paciente y ver los pasos.

  1. Necesitamos crear cuatro clases.
  2. La primera clase es la clase de alergia que establecerá y obtendrá la información sobre las alergias.
  3. La segunda clase será la clase paciente que se utilizará para configurar y obtener el nombre del paciente, la edad y las alergias que tiene.
  4. La tercera clase será la clase enfermedad que se utilizará si el paciente tiene otra enfermedad distinta de la alergia.
  5. La cuarta y última clase será la clase controlador, que se utilizará para agregar la información y los síntomas del usuario y mostrar el resultado del paciente.

Intentemos implementar el sistema anterior en Java.

En primer lugar, el AllergyClass.java:

package delftstack;

public class AllergyClass {
  private String Allergy_Name;

  private String Allergy_Severity;

  private String Allergy_Symptoms;

  public String GetAllergyName() {
    return Allergy_Name;
  }

  public void SetAllergyName(String Allergy_Name) {
    this.Allergy_Name = Allergy_Name;
  }

  public void SetAllergySymptom(String Allergy_Symptoms) {
    this.Allergy_Symptoms = Allergy_Symptoms;
  }
  public String GetAllergySymptom() {
    return Allergy_Symptoms;
  }
  public String GetSeverity() {
    return Allergy_Severity;
  }

  public void SetSeverity(String Allergy_Severity) {
    this.Allergy_Severity = Allergy_Severity;
  }
}

La clase alergia se utilizará para establecer y obtener el nombre, la gravedad y los síntomas de la alergia. Ahora el PatientClass.java:

package delftstack;

import java.util.List;

public class PatientClass {
  private String Patient_Name;

  private int Patient_Age;

  private List<AllergyClass> Allergy_List;

  private List<DiseaseClass> Disease_List;

  public String GetPatientName() {
    return Patient_Name;
  }

  public void SetPatientName(String Patient_Name) {
    this.Patient_Name = Patient_Name;
  }

  public int GetPatientAge() {
    return Patient_Age;
  }

  public void SetPatientAge(int Patient_Age) {
    this.Patient_Age = Patient_Age;
  }

  public List<AllergyClass> GetAllergyList() {
    return Allergy_List;
  }

  public void SetAllergyList(List<AllergyClass> Allergy_List) {
    this.Allergy_List = Allergy_List;
  }

  public List<DiseaseClass> GetDiseaseList() {
    return Disease_List;
  }

  public void SetDiseaseList(List<DiseaseClass> Disease_List) {
    this.Disease_List = Disease_List;
  }
}

La clase paciente se utilizará para configurar y obtener el nombre y la edad del paciente y asignarle alergias o enfermedades. Ahora el DiseaseClass.java:

package delftstack;

public class DiseaseClass {
  private String Disease_Name;

  private String Disease_Severity;

  private String Disease_Symptoms;

  public String GetDiseaseName() {
    return Disease_Name;
  }

  public void SetDiseaseName(String Disease_Name) {
    this.Disease_Name = Disease_Name;
  }

  public void SetDiseaseSymptom(String Disease_Symptoms) {
    this.Disease_Symptoms = Disease_Symptoms;
  }
  public String GetDiseaseSymptom() {
    return Disease_Symptoms;
  }
  public String GetSeverity() {
    return Disease_Severity;
  }

  public void SetSeverity(String Disease_Severity) {
    this.Disease_Severity = Disease_Severity;
  }
}

La clase enfermedad realizará las mismas operaciones que la clase alergia; esta es una clase opcional en caso de que los síntomas del paciente no estén relacionados con la alergia. Ahora la clase principal es Patient_Health_Info.java:

package delftstack;

import java.util.ArrayList;
import java.util.List;

public class Patient_Health_Info {
  public static void main(String[] args) {
    // Create an instance of patient
    PatientClass Demo_Patient = new PatientClass();
    Demo_Patient.SetPatientName("Sheeraz");
    Demo_Patient.SetPatientAge(28);

    // Create an instance of Allergy
    AllergyClass First_Allergy = new AllergyClass();
    First_Allergy.SetAllergyName("Peanuts");
    First_Allergy.SetAllergySymptom("Swelling/Chocking");
    First_Allergy.SetSeverity("Severe");

    // Create another instance of Allergy if a patient has multiple allergies
    AllergyClass Second_Allergy = new AllergyClass();
    Second_Allergy.SetAllergyName("Eggs");
    Second_Allergy.SetAllergySymptom("Skin inflammation/Nasal congestion");
    Second_Allergy.SetSeverity("Medium");

    // Add Allergies to a list
    List<AllergyClass> Allergy_List = new ArrayList<AllergyClass>();
    Allergy_List.add(First_Allergy);
    Allergy_List.add(Second_Allergy);

    // Assign Allergies to the Patient.
    Demo_Patient.SetAllergyList(Allergy_List);
    String Patient_Diagnosis = PatientDiagnosis(Demo_Patient);

    // Add Diseases to a list if present.
    List<DiseaseClass> Disease_List = new ArrayList<DiseaseClass>();
    // No diseases added

    System.out.println("The Patient's Name is: " + Demo_Patient.GetPatientName());
    System.out.println("The Patient's Age is: " + Demo_Patient.GetPatientAge());
    System.out.println("The Patient has been diagnosed with :: " + Patient_Diagnosis);

    if (Patient_Diagnosis == "Allergy") {
      for (AllergyClass Patient_Allergy : Allergy_List) {
        System.out.println("The Patient has a/an " + Patient_Allergy.GetAllergyName() + " allergy"
            + " with the symptoms of " + Patient_Allergy.GetAllergySymptom()
            + " and the allergy is " + Patient_Allergy.GetSeverity());
      }
    } else if (Patient_Diagnosis == "Disease") {
      for (DiseaseClass Patient_Disease : Disease_List) {
        System.out.println("The Patient has " + Patient_Disease.GetDiseaseName() + " Disease"
            + " with the symptoms of " + Patient_Disease.GetDiseaseSymptom()
            + " and the allergy is " + Patient_Disease.GetSeverity());
      }
    }
  }

  public static String PatientDiagnosis(PatientClass patient) {
    if (patient.GetAllergyList().size() > 0) {
      return "Allergy";
    } else if (patient.GetDiseaseList().size() > 0) {
      return "Disease";
    }
    return null;
  }
}

Esta clase principal se utilizará para insertar y recuperar la información de la alergia o enfermedad de un paciente; también determina si el paciente tiene alergia o cualquier otra enfermedad. Ejecutemos todo el sistema y veamos el resultado.

The Patient's Name is: Sheeraz
The Patient's Age is: 28
The Patient has been diagnosed with :: Allergy
The Patient has a/an Peanuts allergy with the symptoms of Swelling/Chocking and the allergy is Severe
The Patient has a/an Eggs allergy with the symptoms of Skin inflammation/Nasal congestion and the allergy is Medium
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Artículo relacionado - Java Class