1

患者クラスとクライアント クラスという名前の 2 つのクラスがあります。私は患者クラスでメソッドを作成し、それらをクライアントクラスで呼び出しています。入力したレコードをIDで検索して表示するメソッドを追加したい。アプリケーションに必要な変更。プログラムは以下のとおりです。

患者クラス

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.");
        }
    }
}

クライアントクラス

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;

            }
        }
    }
}
4

3 に答える 3

0

コードの問題

あなたのデザインを見てすぐに問題を特定できます。患者クラス内に UI コードが存在しないようにする必要があります。これは、患者クラスは患者情報にアクセスするためだけに存在する必要があるためです。

したがって、(1) public コンストラクター内でクラス属性を初期化する (2) それらの属性へのパブリック読み取りアクセスを提供する (3) などの他の場所に存在する必要があるすべての UI コードを削除するために、患者クラスにドキュメントを変更して追加しました。ユーザー入力用に作成してクライアント クラスからアクセスするクライアント クラスまたはビュー クラス。

患者クラス

/**
* 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;
    }   
}
于 2014-12-06T13:34:38.110 に答える