Problems breaking out of nested loops

Posted by user1040281 on Stack Overflow See other posts from Stack Overflow or by user1040281
Published on 2011-11-16T17:47:14Z Indexed on 2011/11/16 17:50 UTC
Read the original article Hit count: 213

Filed under:
|

I have problems breaking out off these nested loops correctly. What the code is trying to do is to indicate that a customer has rented a certain movie. Both the movie and customer are compared to properties of arraylist objects and then if all checks out the name property and ID property of a movie object are added as a string to another arraylist. All this works correctly as long as I use the first movie (from movies) and the first customer (from customers) but if I try renting other movies further down my arraylist with other customers then it adds the rented movie to the customerRentedMovies arraylist but prints out the "else message". I figure I need to break out of the foreach(blabla) loops aswell? or could goto be used? Comments was removed (looked kinda messy, can explain further if needed)

public void RentMovie(string titel, int movieID, string name, int customerID) 
    {
        foreach (Customer customer in customers)  
        {
            if (name == customer.Name && customerID == customer.CustomerID)  
            {
                foreach (MovieInfo movie in movies)
                {
                    if (titel == movie.Titel && movieID == movie.MovieID)
                    {
                        movie.rented = true;
                        string rentedMovie = string.Format("{0}  ID: {1}", movie.Titel, movie.MovieID);
                        customer.customerRentedMovies.Add(rentedMovie); 

                        break; 
                    }

                    else { Console.WriteLine("No movie with that titel and ID!"); } 

                }
             break;      
            }
            else { Console.WriteLine("No customer with that ID and name"); } 
        }

    }

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET