1

我有两个名为 Patient Class 和 Client Class 的类。我在患者类中创建了方法并在客户端类中调用它们。我想添加一种方法来通过其 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

1

id不能是一个static值,因为一个static类的多个实例之间的值保持不变......

public int getID(){
    return id;
}
于 2014-12-06T12:21:35.783 回答
1

几个变化:

  • 带入obj[i] = new Patient();for 循环,如:

    while (op != 3) {
        obj[i++] = new Patient();
    
  • 在情况 1 中,如果 i 超过 49,则不允许用户添加数据。如果您希望允许多个元素,则可以使用 LinkedList 代替 Patient 的数组。

  • 奇怪的方式,因为它的设计不好,但修改案例 2 如下:

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

然后在您的搜索方法中,迭代专利,如:

public void SearchByPatientId(Patient[] patient) {
    //take input from user
    for (Patient patient : patient) {
        if (patient.id == storesearchid){
            //found.. do whatever you want
            break;
        }
    }
于 2014-12-06T12:28:11.767 回答
0

您的代码有问题

在查看您的设计后,我可以立即发现问题;患者类中不应存在任何 UI 代码。这是因为您的患者类应该仅用于访问患者信息的目的。

因此,我已经修改并添加了文档到您的患者类,以便 (1) 在您的公共构造函数中初始化您的类属性 (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 回答