-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 に答える