vendredi 20 décembre 2019

Randomly assigning a waiter from a list of waiters and cooks

I have tried to assign a random waiter using a while loop and then an if statement however when choosing a second index inside the while loop the java gives error is the cohsen index is a cook how can I fix this? This is my method:

public Waiter assignWaiter() {

    Random rand = new Random();

    int index = rand.nextInt(employees.size());
    Employee assignedWaiter = employees.get(index);
    while (!(employees.get(index) instanceof Waiter)) {
        index = rand.nextInt(employees.size());
        if ((Waiter) employees.get(index) instanceof Waiter) {
            assignedWaiter = (Waiter) employees.get(index);
            return (Waiter) assignedWaiter;

        }

    }
    return (Waiter) assignedWaiter;
}



Aucun commentaire:

Enregistrer un commentaire