David
David

Reputation: 31

Nested For-each loop printing for every item in Arraylist

How can I modify this code so that it only prints out the invalid messages once it has iterated through the whole list first. My current problem is that it is printing out these messages every time there isn't a match. Which is not what I intend it to do.

public void assignSchedule(Depot depotName) throws Exception {
        String client, driver;
        Boolean exit = false;

        do {
            System.out.printf("%-10s %-10s %10s %n", "Client", "Start Date", "End Date");
            depotName.listWorkSchedulue();
            System.out.print("Enter the client name for the schedule you wish to assign: ");
            client = DepotSystem.input.nextLine();

            for (WorkSchedule ws : depotName.getWorkSchedules())
            {
                if (client.equals(ws.getClient()))
                {
                    System.out.printf("%n%20s%n", "Drivers");
                    depotName.listDrivers();
                    System.out.print("Which Driver do you want to assign this to: ");
                    driver = DepotSystem.input.nextLine();

                    for (Driver drivers : depotName.getDrivers())
                    {
                        if (driver.equals(drivers.userName) && !drivers.assigned)
                        {
                            System.out.printf("%nThe work schedule for client %s is now assigned to %s%n ", client,
                                driver);
                            depotName.getDriver(driver).setSchedule(depotName.getWorkSchedule(client));
                            drivers.setAssigned(true);
                            exit = true;
                        } else if ((!driver.equals(drivers.userName) && drivers.assigned)) {
                            System.out.printf("%nEither not a valid driver or Driver is already assigned a Job.%n");
                        }
                    }
                } else if (!client.equals(ws.getClient())) {
                    System.out.printf("%nPlease enter a valid client.%n");
                }
            }
        } while (!exit);
    }

The current output of this if I enter a Incorrect name is as followed

Client     Start Date   End Date 
Gary       2020-04-25   2020-04-27
Bob        2020-05-05   2020-05-06
Enter the client name for the schedule you wish to assign: f

Please enter a valid client.

Please enter a valid client.

This is how I would like for it to work when I enter a incorrect name.

Client     Start Date   End Date 
Gary       2020-04-25   2020-04-27
Bob        2020-05-05   2020-05-06
Enter the client name for the schedule you wish to assign: f

Please enter a valid client.

Upvotes: 0

Views: 45

Answers (3)

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79005

The message,Please enter a valid client should be printed outside the for loop. Do it as follows:

public void assignSchedule(Depot depotName) throws Exception {
    String client, driver;
    Boolean exit = false, validClient;

    do {
        System.out.printf("%-10s %-10s %10s %n", "Client", "Start Date", "End Date");
        depotName.listWorkSchedulue();
        System.out.print("Enter the client name for the schedule you wish to assign: ");
        client = DepotSystem.input.nextLine();

        for (WorkSchedule ws : depotName.getWorkSchedules()) {
            if (client.equals(ws.getClient())) {
                validClient = true;
                System.out.printf("%n%20s%n", "Drivers");
                depotName.listDrivers();
                System.out.print("Which Driver do you want to assign this to: ");
                driver = DepotSystem.input.nextLine();

                for (Driver drivers : depotName.getDrivers()) {
                    if (driver.equals(drivers.userName) && !drivers.assigned) {
                        System.out.printf("%nThe work schedule for client %s is now assigned to %s%n ", client,
                                driver);
                        depotName.getDriver(driver).setSchedule(depotName.getWorkSchedule(client));
                        drivers.setAssigned(true);
                        exit = true;
                    } else if ((!driver.equals(drivers.userName) && drivers.assigned)) {
                        System.out.printf("%nEither not a valid driver or Driver is already assigned a Job.%n");
                    }
                }
            } else {
                validClient = false;
            }
        }
        if (!validClient) {
            System.out.printf("%nPlease enter a valid client.%n");
        }
    } while (!exit);
}

Upvotes: 1

Jason
Jason

Reputation: 5246

At the top of the for loop you can do the following to determine if any of the work schedules contain the client entered. You can subsequently replace depotName.getWorkSchedules() with clientSchedules and remove the condition if (client.equals(ws.getClient())) since we know they're all schedules with the same client.

Please note that the question didn't contain a full SSCCE, a lot of the fundamental classes needed to compile were missing and so there may be unexpected compilation errors.

            List<WorkSchedule> clientSchedules = depotName.getWorkSchedules().stream()
                            .filter(schedule -> client.equals(schedule.getClient()))
                            .collect(Collectors.toList());

            if (clientSchedules.isEmpty()) {
                System.out.printf("%nPlease enter a valid client.%n");
                continue;
            }

Upvotes: 1

Shubham
Shubham

Reputation: 549

Declare a boolean outside of loop and set it in else if condition and also check for this boolean in else if, something like

 else if (condition && !booleanVariable) {
          System.out.printf("%nPlease enter a valid client.%n");
           booleanVariable = true;
           }

Upvotes: 0

Related Questions