简体   繁体   中英

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

I'm working on a problem where I have to import 3 text files: Doctor, Paitient, Visits. Once imported I would need to assign a paitient to a doctor & create a visit out of it.

My problem is that when I import the visits file & create the object Visits, it would grab any id of an paitient and any id of a doctor. I would need to add a bit where it would check if in the same line of the text file is the ID of the doctor & ID of the paitient then assign them together to make an object.

My current code shows this:

  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);
}

The text file which I'm using for the visits is this(ID of the doctor, Id of the paitient, date):

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

(This is a line from file Doctor: ID of doctor, Surname, Name, Date of birth, NIP number , Personal number PESEL)

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

(This is a line from file Paitient: ID of paitient, Surname, Name, Personal number PESEL, Date of birth).

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

The aim of this program is for example to check how many times a paitient has visited a doctor, which doctor had the most visits etc.

I have removed the paitient & doctor from the Visit's constructor. Do you recon it was good or is it better to leave it and keep the inheritance of it?

Also with the code now how would I change it to use the Paitient object and the Doctor object to make the Visit object?

Or would it be better not to use inheritance?

The object has been created with the help of @Marcin but Im looking ahead of the methods which I need to create. Would you advise?

What do you mean by "any id"?

My problem is that when I import the visits file & create the object Visits, it would grab any id of an paitient and any id of a doctor.

For sure you have bug here:

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

You filter Paitient list by Doctor's ID. Change it to:

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

In the future you can avoid mistakes like that by creating variables to hold sub-products in your operations:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM