dimanche 20 septembre 2015

Design Pattern to implement Allergy class in Java

Which design pattern to use to implement an Allergy class in Java such that each time a patient reports an allergy, the allergy name, severity, symptoms etc are recorded along with patient's demographic details. I have implemented this using very simple logic.

public class PatientHealth{


public static void main(String[] args){


    Patient patient = new Patient();             // set patient's name in the constructor
    //Disease disease = new Disease();


    System.out.println("Enter Patient's name:");

    @SuppressWarnings("resource")
    Scanner scanner = new Scanner(System.in);
    String name= scanner.nextLine();
    patient.setName(name);

    System.out.println(" Number of allergies to enter:" ); // for multiple allergies

    int allergyCount = scanner.nextInt();
    scanner.nextLine();

    List<Allergy> allergyList = new ArrayList<Allergy>();
    String allergyName, severity, reporter, symptoms;

    for( int i=0; i<allergyCount; i++){
        Allergy allergy = new Allergy();
        System.out.println("Enter allergy name:");
        allergyName = scanner.nextLine();
        allergy.setAllergyName(allergyName);  // set allergy name

        System.out.println("Enter severity:");
        severity = scanner.nextLine();  // set severity
        allergy.setSeverity(severity);

        System.out.println("Enter the reporter's name:");
        reporter = scanner.nextLine();  //set reporter's name
        allergy.setReporter(reporter);

        System.out.println(" Enter the symptoms:"); // set symptoms
        symptoms = scanner.nextLine();
        allergy.setSymptoms( symptoms);
        allergyList.add(allergy);
    }


    patient.setAllergy(allergyList);


    System.out.println(" Patient name:" + patient.getName());
    System.out.println(" Allergies:" );
    List<Allergy> result = patient.getAllergyList();
    for(Allergy al:result){
    System.out.println(al.allergyName);
    System.out.println(al.severity);
    System.out.println(al.reporter);
    System.out.println(al.symptoms);
    System.out.println("");


    }

}

}

Patient class:

public class Patient{

public String name;
//private int age;
private List<Allergy> allergyList;
private List<Disease> diseaseList;

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

public void setAllergy(List<Allergy> allergyList){

    this.allergyList = allergyList;

}

public void setDisease(List <Disease> diseaseList){

    this.diseaseList = diseaseList;

}

public String getName(){

    return name;
}


public List<Allergy> getAllergyList(){
    return allergyList;

}

public List<Disease> getDiseaseList(){
    return diseaseList;

}

}

Allergy Class:

public class Allergy{

public String allergyName, reporter, symptoms, severity;

public void setAllergyName( String allergyName){

    this.allergyName = allergyName;
}

public void setSeverity( String severity){

    this.severity = severity;
}

public void setReporter( String reporter){

    this.reporter = reporter;
}

public void setSymptoms( String symptoms){

    this.symptoms = symptoms;
}

public String getAllergyName( ){
    return allergyName;
}

public String getReporter( ){

    return reporter;
}

public String getSymptoms(){

    return symptoms;
}

public String getSeverity(){

    return severity;
}

}

Note:- Ignore the Disease class.

Any Java Design Pattern that I can use to make it more efficient?

Aucun commentaire:

Enregistrer un commentaire