How can i search an arrray of object with a value in java

576 Views Asked by At

i have two classess named, Patient Class and Client Class. i have created methods in Patient Class and Calling them in Client Class. i want to add a method to find a entered record by its id and display it. what changes needed in my applications. Programs are given below:

Patient Class

import javax.swing.*;

public class Patient {
    private String patientname;
    private String fathername;
    private String date;
    private int dob;
    private static int id = 9000;
    private String disease;
    private String n;
    private double nic;
    private String doctorname;
    private String prescription;
    private String history;
    private String searchid;
    private int storesearchid;

    Patient() {}
    public void setPatientInformation() {

        id++;
        patientname = JOptionPane.showInputDialog("Enter Patient name: ");
        fathername = JOptionPane.showInputDialog("Enter Father name of patient: ");
        date = JOptionPane.showInputDialog("Enter date of birth : ");
        dob = Integer.parseInt(date);
        disease = JOptionPane.showInputDialog("Enter disease: ");
        n = JOptionPane.showInputDialog("Enter nic no: ");
        nic = Integer.parseInt(n);
        doctorname = JOptionPane.showInputDialog("Enter your doctor name: ");
        prescription = JOptionPane.showInputDialog("Enter description of disease: ");
        history = JOptionPane.showInputDialog("Enter history of disease? ");

    }
    public void showPatientInformation() {
        JOptionPane.showMessageDialog(null, "Patient Id" + id + "\nPatient Name: " + patientname + "\nPatient Father Name: " + fathername + "\nPatient Date of birth: " + dob + "\nDisease: " + disease + "\nNIC No:" + nic + "\nDoctor Name: " + doctorname + "\nPrescription: " + prescription + "\nHistory: " + history);
    }
    public void SearchByPatientId() {
        searchid = JOptionPane.showInputDialog("Enter Id of Patient.");
        storesearchid = Integer.parseInt(searchid);

        if (storesearchid == obj[id]) {
            JOptionPane.showMessageDialog(null, "Record Found");
        } else {
            JOptionPane.showMessageDialog(null, "Record With This Id Not Found.");
        }
    }
}

Client Class

import javax.swing.*;

public class Client {
    public static void main(String[] aa) {
        String input;
        int i = 0, op = 0;

        Patient[] obj = new Patient[50];


        obj[i] = new Patient();

        while (op != 3) {
            input = JOptionPane.showInputDialog("Press 1 for Add new Patient Record.\nPress 2 for search Record  by patient ID.\nPress 3 for exit.");
            op = Integer.parseInt(input);

            switch (op) {
                case 1:

                    JOptionPane.showMessageDialog(null, "Enter New Record");
                    obj[i].setPatientInformation();

                    JOptionPane.showMessageDialog(null, "Record added SuccessFully.");
                    obj[i].showPatientInformation();
                    break;

                case 2:
                    JOptionPane.showMessageDialog(null, "Search Record By patient ID.");
                    obj[i].SearchByPatientId();
                    break;

            }
        }
    }
}
3

There are 3 best solutions below

0
On BEST ANSWER

Couple of changes:

  • Bring obj[i] = new Patient(); inside for loop like:

    while (op != 3) {
        obj[i++] = new Patient();
    
  • In case 1, if i goes beyond 49, don't allow user to add the data. If you wish to allow multiple elements you could use LinkedList there instead if Patient's array.

  • Odd way as its not well designed but modify Case 2 as below:

    case 2:
        JOptionPane.showMessageDialog(null, "Search Record By patient ID.");
        obj[i].SearchByPatientId(obj);
        break;
    

And then in your search method, iterate over the patent like:

public void SearchByPatientId(Patient[] patient) {
    //take input from user
    for (Patient patient : patient) {
        if (patient.id == storesearchid){
            //found.. do whatever you want
            break;
        }
    }
0
On

Problems with your code

Immediately, upon viewing your design I can identify a problem; there should not be any UI code existing inside of your patient class. This is because your patient class should exist only for the purpose of accessing patient information.

Consequently, I have modified and added documentation to your patient class in order to (1) initialize your class attributes within your public constructor (2) provide public reading access to those attributes (3) remove all UI code that should exist elsewhere such as in your client class or a view class that you create and access from your client class for user input.

Patient Class

/**
* The patient class is used for storing the information
* associated with a patient. 
*/
public class Patient {
    private String patientname;
    private String fathername;
    private String date;
    private int dob;
    private static instanceCount = 0;
    private int id = 9000;
    private String disease;
    private String n;
    private double nic;
    private String doctorname;
    private String prescription;
    private String history;

    /**
    * This method provides a public constructor for creating a patient instance.
    *
    * @param aPatientName The name of the patient instance to be created
    * @param aFatherName The name of the father of the patient instance to be created
    * @param aDate The date associated with the patient instance to be created
    * @param aDob The DOB associated with the patient instance to be created
    * @param aDisease The disease associated with the patient instance to be created
    * @param aN The N associated with the patient instance to be created
    * @param aNic The NIC number associated with the patient instance to be created
    * @param aPrescription The prescription associated with the patient instance to be created
    * @param aHistory The history associated with the patient instance to be created
    *
    * @return a Patient instance that has been populated with the input parameters
    */
    public Patient(String aPatientName, String aFatherName, String aDate, int aDob,
                String aDisease, String aN, double aNic, String aPrescription,
                String aHistory) {

        // Increment the count for the number of patient instance that exist
        // because static variables are shared across the class itself rather
        // than the actual objects. Consequently, this count is reflected across
        // all patient instances.
        instanceCount++;           

        // Increment the current id so that we get a new id for this patient instance
        id = id + instanceCount;

        // Initialize the values of all patient attributes according to constructor parameters
        patientname = aPatientName;
        fathername = aFatherName;
        date = aDate;
        dob = aDob;
        disease = aDisease;
        n = aN;
        nic = aNic;
        prescription = aPrescription;
        history = aHistory;
    }

    /**
    * This method retrieves the string needed for displaying all of this
    * patient’s attributes to the user.
    * @return a String containing the attributes for this patient
    */
    public String showPatientInformation() {
        String attributes = “”;

        attributes += “Patient Id” + id;
        attributes += “\nPatient Name: “ + patientname;
        attributes += “\nPatient Father Name: “ + fathername;
        attributes += “\nPatient Date of Birth: “ + dob;
        attributes += “\nDisease: “ + disease;
        attributes += “\nNIC No:” + nic;
        attributes += “\nDoctor Name: “ + doctorname;
        attributes += “\nPrescription: “ + prescription;
        attributes += “\nHistory: “ + history;

        return attributes;
    }

    /**
    * This method acquires the ID of this patient instance.
    * @return int The ID of this patient instance
    */
    public int getId() {
        return id;
    }

    /**
    * This method acquires the name of this patient instance.
    * @return String The name of this patient instance
    */
    public String getPatientName() {
        return patientName;
    }

    /**
    * This method acquires the name of the father of this patient instance.
    * @return String The name of the father of this patient instance
    */
    public String getFatherName() {
        return fathername;
    }

    /**
    * This method acquires the date for this patient instance.
    * @return String The date for this patient instance
    */
    public String getDate() {
        return date;
    }

    /**
    * This method acquires the DOB of this patient instance.
    * @return int The DOB of this patient instance
    */
    public int getDob() {
        return dob;
    }

    /**
    * This method acquires the disease of this patient instance.
    * @return String The disease of this patient instance
    */
    public String getDisease() {
        return disease;
    }

    /**
    * This method acquires the N of this patient instance.
    * @return String The N of this patient instance
    */
    public String getN() {
        return n;
    }

    /**
    * This method acquires the NIC of this patient instance.
    * @return int The NIC of this patient instance
    */
    public double getNic() {
        return nic;
    }

    /**
    * This method acquires the name of the doctor for this patient instance.
    * @return String The name of the doctor for this patient instance
    */
    public String getDoctorName() {
        return doctorname;
    }

    /**
    * This method acquires the prescription of this patient instance.
    * @return String The prescription of this patient instance
    */
    public String getPrescription() {
        return prescription;
    }

    /**
    * This method acquires the history of this patient instance.
    * @return String The history of this patient instance
    */
    public String getHistory() {
        return history;
    }   
}
0
On

id cannot be a static value, because a static value remains the same among multiple instances of a class...

public int getID(){
    return id;
}