簡體   English   中英

檢查 2 個對象是否在數組中,然后將其變成一個對象

[英]Checking if 2 objects are in array then making out of it an object

我正在處理一個必須導入 3 個文本文件的問題:醫生、患者、訪問。 導入后,我需要將患者分配給醫生並從中進行訪問。

我的問題是,當我導入訪問文件並創建對象訪問時,它會獲取患者的任何 ID 和醫生的任何 ID。 我需要添加一點,它會檢​​查文本文件的同一行中是否是醫生的 ID 和患者的 ID,然后將它們分配在一起以創建一個對象。

我當前的代碼顯示了這一點:

  public static void main(String[] args) {

    List<Lekarz> lekarz = new ArrayList<>();

    try {
        File fileLekarze = new File("src/com/company/lekarze.txt");
        Scanner readerLekarze = new Scanner(fileLekarze);
        readerLekarze.nextLine();
        while (readerLekarze.hasNextLine()) {
            String[] data = readerLekarze.nextLine().trim().split("\t+");
            lekarz.add(new Lekarz(Integer.parseInt(data[0]), data[1], data[2], data[3], new SimpleDateFormat("yyyy-MM-dd").parse(data[4]), data[5], data[6]));
        }
        readerLekarze.close();

    } catch (FileNotFoundException e) {
        System.out.println("Error File Lekarze");
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }


    List<Pacjenci> pacjent = new ArrayList<>();

    try {
        File pacjenciFile = new File("src/com/company/pacjenci.txt");
        Scanner readerPacjenci = new Scanner(pacjenciFile);
        readerPacjenci.nextLine();
        while (readerPacjenci.hasNextLine()) {
            String[] data = readerPacjenci.nextLine().trim().split("\t");
            pacjent.add(new Pacjenci(Integer.parseInt(data[0]), data[1], data[2], data[3], new SimpleDateFormat("yyyy-MM-dd").parse(data[4])));
        }
        readerPacjenci.close();


    } catch (FileNotFoundException e) {
        System.out.println("Error File Pacjenci");
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }


    List<Wizyty> wizytyList = new ArrayList<>();

    try {
        File fileWizyty = new File("src/com/company/wizyty.txt");
        Scanner readerWizyty = new Scanner(fileWizyty);
        readerWizyty.nextLine();
        while (readerWizyty.hasNextLine()) {
            String[] data = readerWizyty.nextLine().trim().split("\t");

            Lekarz lekarz1 = lekarz.stream()
                    .filter(m -> m.getIdLekarza() == Integer.parseInt(data[0]))
                    .findAny()
                    .orElse(null);

            Pacjenci pacjenci1 = pacjent.stream()
                    .filter(m -> m.getIdPacjenta() == Integer.parseInt(data[1]))
                    .findAny()
                    .orElse(null);

            Wizyty wizyta = new Wizyty(Integer.parseInt(data[0]), Integer.parseInt(data[1]), new SimpleDateFormat("yyyy-MM-dd").parse(data[2]));

            wizytyList.add(wizyta);
        }
        readerWizyty.close();
    } catch (FileNotFoundException e) {
        System.out.println("Error File Wizyty");
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    System.out.println(wizytyList);
}

我用於訪問的文本文件是這樣的(醫生 ID、患者 ID、日期):

Id_lekarza  Id_pacjenta Data_wizyty
  23             124     2006-12-13
  23             172     2006-1-25

(這是醫生文件中的一行:醫生 ID、姓氏、姓名、出生日期、NIP 號碼、個人號碼 PESEL)

Id_lekarza  Nazwisko    Imie    Specjalnosc Data_urodzenia  NIP PESEL
  23           Kadaj    Monika  laryngolog  1965-03-16   879-122-69-94  65031687654

(這是來自文件患者的一行:患者 ID、姓氏、姓名、個人編號 PESEL、出生日期)。

Id_pacjenta Nazwisko    Imie    PESEL   Data_urodzenia
     100    Kowal     Waldemar  01211309876 2001-1-13

該程序的目的是例如檢查患者去看醫生的次數,訪問次數最多的醫生等。

我已經從 Visit 的構造函數中刪除了病人和醫生。 你認為它是好的還是留下它並保留它的遺產更好?

現在還有代碼,我將如何更改它以使用 Paitient 對象和 Doctor 對象來制作 Visit 對象?

還是不使用繼承會更好?

該對象是在@Marcin 的幫助下創建的,但我正在展望我需要創建的方法。 你會建議嗎?

“任何 id”是什么意思?

我的問題是,當我導入訪問文件並創建對象訪問時,它會獲取患者的任何 ID 和醫生的任何 ID。

肯定你在這里有錯誤:

            Pacjenci pacjenci1 = pacjent.stream()
                    .filter(m -> m.getIdPacjenta() == Integer.parseInt(data[0]))
                    .findAny()
                    .orElse(null);

您可以按醫生 ID 篩選患者列表。 將其更改為:

            Pacjenci pacjenci1 = pacjent.stream()
                    .filter(m -> m.getIdPacjenta() == Integer.parseInt(data[1]))
                    .findAny()
                    .orElse(null);

將來,您可以通過在操作中創建變量來保存子產品來避免此類錯誤:

Integer doctorId = Integer.parseInt(data[0]);
Integer paitientId = Integer.parseInt(data[1]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM