Search code examples
javamethodsarrayobject

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


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;

            }
        }
    }
}

Solution

  • 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;
            }
        }