проверить, является ли ввод пользователя действительным или недействительным - PullRequest
0 голосов
/ 30 ноября 2018

Я написал способ добавить встречу.Пользователь вводит данные (день, время, имя врача, имя отделения, имя пациента), и если пользователь вводит неправильное имя врача или пациента, он будет отображать сообщение о том, что ввод недействителен, пока пользователь не введет действительный ввод.

Я попробовал этот код, но когда я ввел неправильное имя, он не отображает неверное сообщение.Я пытался использовать contains, все тот же.

public void add_appointment(ArrayList<appointment> appointments,
                            ArrayList<doctor> doctors,
                            ArrayList<section> sections,
                            ArrayList<Patient> PatientList,
                            ArrayList<String> dayArray,
                            ArrayList<String> timeArray) {
    Scanner input = new Scanner(System.in);
    System.out.println("==== Book appointment ====");
    System.out.println("* to book an appointment choose a doctor, section, time and day from below :");
    System.out.print("Doctors :  ");

    for (int r = 0; r < doctors.size(); r++) {
        System.out.print(doctors.get(r).getName() + ", ");
    }

    System.out.println("\n Sections : { Dermal , Dental } ");
    System.out.println("Times : from { 8AM to 4PM } ");
    System.out.println("Days : from { Sunday to Thursday } ");
    System.out.println(" ");

    System.out.println("please enter day :");
    String a1 = input.nextLine();
    if (!dayArray.contains(a1)) {
        System.out.println("invalid day , please enter another day : ");
        a1 = input.nextLine();
    }

    System.out.println("please enter time : ");
    String a2 = input.nextLine();
    if (!timeArray.contains(a2)) {
        System.out.println("invalid time , please enter another time : ");
        a2 = input.nextLine();
    }

    System.out.println("please enter the doctor name : ");
    String a3 = input.nextLine();
    for (int w = 0; w < doctors.size(); ++w) {
        if (doctors.contains(a3)) {
            System.out.println("invalid doctor , the doctor doesn't exists. please enter dr.name : ");
            a3 = input.nextLine();
        }
    }

    System.out.println("please enter the section : ");
    String a4 = input.nextLine();
    for (int e = 0; e < PatientList.size(); ++e) {
        if (sections.contains(a4)) {
            System.out.println("invalid section , the section doesn't exists. please enter section name : ");
            a4 = input.nextLine();
        }
    }

    System.out.println("please enter your name : ");
    String a5 = input.nextLine();
    for (int f = 0; f < PatientList.size(); ++f) {
        if (PatientList.contains(a5)) {
            System.out.println("invalid patient , the patient doesn't exists. please enter name again : ");
            a5 = input.nextLine();
        }
    }

    System.out.println("please assign number to your appointment : ");
    int a6 = input.nextInt();
    appointments.add(new appointment(a1, a2, a3, a4, a5, a6));
    System.out.println(appointments + " Successfully added. \n");
}

1 Ответ

0 голосов
/ 30 ноября 2018

Должно быть что-то вроде этого:

System.out.println("please enter the doctor name : ");
String a3 = input.nextLine();
while (!doctors.contains(a3)){
        System.out.println("invalid doctor , the doctor doesn't exists. please enter dr.name : ");
        a3 = input.nextLine();
    }
}

Повторяйте, пока введенное имя dr.name не окажется в списке врачей.

...