我有两个名为Patient Class和Client Class的classess。我在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;
}
}
}
}
答案 0 :(得分:1)
id
不能是static
值,因为static
值在类的多个实例中保持不变...
public int getID(){
return id;
}
答案 1 :(得分:1)
一些变化:
将obj[i] = new Patient();
置于循环中,如:
while (op != 3) {
obj[i++] = new Patient();
在案例1中,如果我超过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;
}
}
答案 2 :(得分: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;
}
}