gpt4 book ai didi

java - 我如何在java中搜索具有值的对象数组

转载 作者:行者123 更新时间:2023-11-30 03:38:11 24 4
gpt4 key购买 nike

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

}
}
}
}

最佳答案

一些变化:

  • obj[i] = new Patient(); 放入 for 循环中,如下所示:

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

  • 奇怪的方式,因为它的设计不好,但修改案例 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;
}
}

关于java - 我如何在java中搜索具有值的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27331557/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com