-2

我在小型医院系统中有一个用于患者的 ArrayList,需要编写一个方法来更新患者信息。此方法应采用一个名为“id”的整数,将此 id 与 Patient ArrayList 中的所有 id 匹配,并更改该对象中的一些信息。我怎样才能迭代这个列表?谢谢。

这是我的病人班

import java.util.ArrayList;
import java.util.List;

public class Patient {
    String strName,strSurname,strAddress,strDepartment,strGender;
    int iId,iClass,iDob;
    List<Entry> entries = new ArrayList<Entry>();
    long longTel;
    boolean boolAllergy,boolChronicDisease,boolRegularMedicine;

    public boolean isBoolAllergy() {
        return boolAllergy;
    }
    public void setBoolAllergy(boolean boolAllergy) {
        this.boolAllergy = boolAllergy;
    }
    public boolean isBoolChronicDisease() {
        return boolChronicDisease;
    }
    public void setBoolChronicDisease(boolean boolChronicDisease) {
        this.boolChronicDisease = boolChronicDisease;
    }
    public boolean isBoolRegularMedicine() {
        return boolRegularMedicine;
    }
    public void setBoolRegularMedicine(boolean boolRegularMedicine) {
        this.boolRegularMedicine = boolRegularMedicine;
    }
    public String getStrName() {
        return strName;
    }
    public void setStrName(String strName) {
        this.strName = strName;
    }
    public String getStrSurname() {
        return strSurname;
    }
    public void setStrSurname(String strSurname) {
        this.strSurname = strSurname;
    }
    public String getStrAddress() {
        return strAddress;
    }
    public void setStrAddress(String strAddress) {
        this.strAddress = strAddress;
    }
    public String getStrDepartment() {
        return strDepartment;
    }
    public void setStrDepartment(String strDepartment) {
        this.strDepartment = strDepartment;
    }
    public int getiId() {
        return iId;
    }
    public void setiId(int iId) {
        this.iId = iId;
    }
    public long getlongTel() {
        return longTel;
    }
    public void setiTel(long iTel) {
        this.longTel = iTel;
    }
    public int getiClass() {
        return iClass;
    }
    public void setiClass(int iClass) {
        this.iClass = iClass;
    }
    public int getiDob() {
        return iDob;
    }
    public void setiDob(int iDob) {
        this.iDob = iDob;
    }
    public String getStrGender() {
        return strGender;
    }
    public void setStrGender(String strGender) {
        this.strGender = strGender;
    }
    public void Details() {
        System.out.println("*********************************");
        System.out.println("Patient Id: " + getiId());
        System.out.println("Name: " + getStrName() + " Surname: " + getStrSurname());
        System.out.println("Address: " + getStrAddress() + " Department: " + getStrDepartment() + " Gender: " + getStrGender());
        System.out.println("Class: " + getiClass() + " Date of birth: " + getiDob() + " Tel: " + getlongTel());
        System.out.println("Chronical Disease? " + isBoolChronicDisease());
        System.out.println("Any Allergy? " + isBoolAllergy());
        System.out.println("Using regular drugs?" + isBoolRegularMedicine());
        System.out.println("*********************************");
    }
}

这是使用 Patient 类的主要类

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;


public class Main {

    static List<Doctor> listDoctors = new ArrayList<Doctor>();
    static List<Nurse> listNurses = new ArrayList<Nurse>();
    static List<Patient> listPatients = new ArrayList<Patient>();
    static String strUserName = "admin";
    static String strPass = "password";

    public static void main(String[] args) {
        @SuppressWarnings("unused")
        int iChoice;
        boolean exit = false;
        boolean boolAccess = Login();
        if (boolAccess) {
         while(!exit) {
            iChoice = Menu();
            switch(iChoice) {
                case 1:
                    AddDoctor();
                    break;
                case 2:
                    AddNurse();
                    break;
                case 3:
                    AddPatient();
                    break;
                case 4:
                    ListDoctors();
                    break;
                case 5:
                    ListNurses();
                    break;
                case 6:
                    ListPatients();
                    break;
                case 8:
                    ChangeUserName();
                    break;
                case 9:
                    ChangePass();
                    break;
                    //Hastalığa göre hastalar listelenecek
                    //Kronik hastlalıkları olan hastalar listelenecek
                    //Sigortasını yatırmayan hastalar listelenecek
                    //delete ve update olayları
                    //Hastaneye sevkedilen hastalar listelenecek
                    //Yatılı kalan hastalar listelensin
                    //Raporlu olan entryler listelenecek
                    //Kayıt girilirken eğer hasta ismi ilk defa giriliyorsa önce hasta kaydı yapın diye uyarı cıkacak
                case 15:
                    exit = true;
                    break;
            }
          }
       }
    }

    private static void ChangeUserName() {
        Scanner scan = new Scanner(System.in);
        if(Login()) {
            System.out.println("Enter new User Name:");
            strUserName = scan.next();
            System.out.println("Saved..");
        }
    }

    private static void ChangePass() {
        Scanner scan = new Scanner(System.in);
        if(Login()) {
            System.out.println("Enter new Password:");
            strPass = scan.next();
            System.out.println("Saved..");
        }
    }

    private static void ListNurses() {
        Iterator<Nurse> it = listNurses.iterator();
        while(it.hasNext()) {
            Nurse nur = (Nurse) it.next();
            nur.Details();
        }
    }

    private static void ListPatients() {
        Iterator<Patient> it = listPatients.iterator();
        while(it.hasNext()) {
            Patient patient = (Patient) it.next();
            patient.Details();
        }
    }

    public static boolean Login() {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter Username: ");
        if(strUserName.contentEquals(scan.next())) {
            System.out.println("Enter Password:");
            if(strPass.contentEquals(scan.next())) {
                return true;
            }
            else
                System.out.println("Wrong password");   
                return Login();
            }
        System.out.println("Wrong user name");
        return Login();
    }

    public static int Menu() {
        Scanner scan = new Scanner(System.in);

        System.out.println("Choose one of the following order:");
        System.out.println("1.Enter new Doctor");
        System.out.println("2.Enter new Nurse");
        System.out.println("3.Enter new Patient");
        System.out.println("4.List all doctors");
        System.out.println("5.List all nurses");
        System.out.println("6.List all patients");
        System.out.println("8.Change user name");
        System.out.println("9.Change password");
        System.out.println("15.Exit");

        return scan.nextInt();
    }

    private static void ListDoctors() {
        Iterator<Doctor> it = listDoctors.iterator();
        while(it.hasNext()) {
            Doctor doc = (Doctor) it.next();
            doc.Details();
        }
    }

    private static void AddDoctor() {
        Doctor doctor;
        String strName,strSurname,strSpeciality;
        int iSSN;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter name:");
        strName=scan.next();
        System.out.println("Enter surname:");
        strSurname = scan.next();
        System.out.println("Enter speciality");
        strSpeciality = scan.next();
        System.out.println("Enter SSN");
        iSSN = scan.nextInt();

        doctor = new Doctor(strName,strSurname,strSpeciality,iSSN);
        listDoctors.add(doctor);

        System.out.println("Saved..");
    }

    private static void AddNurse() {
        Nurse nurse;
        String strName,strSurname;
        int iSSN;
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter name:");
        strName=scan.next();
        System.out.println("Enter surname:");
        strSurname = scan.next();
        System.out.println("Enter SSN");
        iSSN = scan.nextInt();

        nurse = new Nurse(strName,strSurname,iSSN);
        listNurses.add(nurse);

        System.out.println("Saved..");
    }

    private static void AddPatient() {

        Patient patient = new Patient();
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter name:");
        patient.setStrName(scan.next());
        System.out.println("Enter surname:");
        patient.setStrSurname(scan.next());
        System.out.println("Enter date of birth(DDMMYYYY)");
        patient.setiDob(scan.nextInt());
        System.out.println("Enter address:");
        patient.setStrAddress(scan.next());
        System.out.println("Enter department:");
        patient.setStrDepartment(scan.next());
        System.out.println("Enter gender:");
        patient.setStrGender(scan.next());
        System.out.println("Enter Telephone number:");
        patient.setiTel(scan.nextLong());
        System.out.println("Enter id:");
        patient.setiId(scan.nextInt());
        System.out.println("Enter class:");
        patient.setiClass(scan.nextInt());
        System.out.println("Does patient have any chronical disease? (Y/N)");
        if(scan.next().contentEquals("Y"))
            patient.setBoolChronicDisease(true);
        else
            patient.setBoolChronicDisease(false);
        System.out.println("Does patient use any regular drugs? (Y/N)");
        if(scan.next().contentEquals("Y"))
            patient.setBoolRegularMedicine(true);
        else
            patient.setBoolRegularMedicine(false);
        System.out.println("Does patient have any allergies?");
        if(scan.next().contentEquals("Y"))
            patient.setBoolAllergy(true);
        else
            patient.setBoolAllergy(false);

        listPatients.add(patient);
    }

    private static void SearchPatientById(int id) {
        Iterator<Patient> it = listPatients.iterator();
        while(it.hasNext()) {
            Patient patient = (Patient) it.next();
            if(it.next().getiId() == id)
                patient.Details();
        }
    }
}
4

1 回答 1

2

稍作修改,您的 searchPatientById 就可以返回找到的患者对象:

private static Patient searchPatientById(int id) {
    Iterator<Patient> it = listPatients.iterator();
    while(it.hasNext()) {
        Patient patient = (Patient) it.next();
        if(patient.getiId() == id)
            return patient;
    }
    // if not found return null
    return null;
}

请注意,我删除了您对next() 这样的双重调用,您可以执行类似的操作

Patient foundPatient = searchPatientById(idToFind);
if (foundPatient != null) {
    foundPatient.setBoolAllergy(patientsAllergyState);
} else {
    // whatever you need to do if the patient cannot be found
}

另请注意,在 Java 中,将变量类型编码为变量名称并不常见,但有一个普遍遵循的命名约定

于 2013-01-02T01:15:15.943 回答