Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6824921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:57:33+00:00 2026-05-26T21:57:33+00:00

I have problems breaking out off these nested loops correctly. What the code is

  • 0

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

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:57:34+00:00Added an answer on May 26, 2026 at 9:57 pm

    Your method is violating the Single Responsibility rule — each class or method should have one and only one responsibility and reason for change. You have a single method that is responsible for doing 3 different things:

    • Finding a customer
    • Finding a movie
    • renting the movie to the customer.

    This makes it

    • difficult to test
    • difficult to maintain
    • hard to understand

    This is a Code Smell.

    You should refactor your method something like this, delegating the responsibility for finding a customer and finding a movie to their own methods:

    public void RentMovie( string titel , int movieID , string name , int customerID )
    {
      Customer  customer = FindCustomer( customerID , name ) ;
      MovieInfo movie    = FindMovie( movieID , titel ) ;
    
      if ( customer == null )
      {
        Console.WriteLine("No customer with that ID and name");
      }
    
      if ( movie == null )
      {
        Console.WriteLine("No movie with that titel and ID!") ;
      }
    
      if ( customer != null && movie != null )
      {
        string rentedMovie = string.Format( "{0}  ID: {1}" , movie.Titel , movie.MovieID );
        movie.rented = true;
        customer.customerRentedMovies.Add( rentedMovie );
      }
    
      return ;
    }
    

    If you aren’t using Linq, the FindCustomer() and FindMovie methods might look like this:

    private MovieInfo FindMovie( int movieID , string titel )
    {
      MovieInfo instance = null ;
      foreach( MovieInfo movie in movies )
      {
        if ( movie.MovieID == movieID && movie.Titel == titel )
        {
          instance = movie ;
          break ;
        }
      }
      return instance ;
    }
    
    private Customer FindCustomer( int customerID , string name )
    {
      Customer instance = null ;
      foreach( Customer customer in customers )
      {
        if ( customer.CustomerID == customerID && customer.Name == name )
        {
          instance = customer ;
          break ;
        }
      }
      return instance ;
    }
    

    If you’re using Ling, the same methods might be:

    private MovieInfo FindMovie( int movieID , string titel )
    {
      return movies.Where( x => x.MovieID == movieID && x.Titel == titel ).SingleOrDefault() ;
    }
    
    private Customer FindCustomer( int customerID , string name )
    {
      return customers.Where( x => x.Name == name && x.CustomerID == customerID ).SingleOrDefault() ;
    }
    

    The code is now much simpler, easier to understand and self-describing. When it’s time to make changes, the changes will be easier to make as well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been a number of problems related to IE8 breaking Visual Studio 2005/2008.
I have problems with dealing with widows within a multicols environment, that is, I
I have problems getting some of my views aligned in a relative layout that
I have problems with Darwin Streaming server 5.5.5 on Debian. When i'm trying to
I have problems with following code: http://lisper.ru/apps/format/96 The problem is in normalize function, which
I have a problem that I can't figure out what to do next. I
I have developed an extension that works great on Magento until 1.6 (I'm trying
I have a difficult mathematical question that is breaking my brain, my whiteboard, and
I have a piece of code that may or may not fail, which I
I have problems with bringing a windows mobile 6 form to the front. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.