Search code examples
c#menuuser-input

A couple menu choices coming back incorrect


Rough draft of the entire program, but I am trying to get my menu working properly and am running into an issue that I cannot stump. When testing the application for the customer menu, 1-4 returns correctly but 5 is not. I would like 5 to just return to the main menu, but it is returning my error checking output. The same issue is in the manager menu also, where 1-4 returns correctly but 5 and 6 do not. They both direct me to my error checking output also. How could I go about altering my code to make each of my submenu functions work correctly?

        MainMenu();
        Console.ReadKey();

    }
    static void MainMenu()
    {
        int userChoice = MainMenuChoice(); // Reading in the userChoice with the MenuChoice method

        if (userChoice == 3) // if user enters 3, the program ends
        {
            Console.WriteLine("Thank you, Goodbye!");
        }

        while (userChoice != 3)
        {
            if (userChoice == 1)
            {
                Console.WriteLine("Welcome to the customer menu!\n"); // The customer menu is brought up if user enters 1
                CustomerMenu();
            }
            if (userChoice == 2)
            {
                Console.WriteLine("Welcome to the manager menu!\n"); // The manager menu is brought up if user enters 2
                ManagerMenu();
            }
            userChoice = MainMenuChoice(); // program ends
            if (userChoice == 3)
            {
                Console.WriteLine("Thank you for visiting VideoMart at University Boulevard, Goodbye!");

            }
        }

    }
    static int MainMenuChoice()
    {
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------"); // introducing the user with the menu 
        Console.WriteLine("Welcome to VideoMart at University Boulevard!  \nAt VideoMart you are able to rent a variety of movies from many genres such as action, family, horror, etc!");
        Console.WriteLine("\nPress 1 if you are a customer");
        Console.WriteLine("\nPress 2 if you are a manager");
        Console.WriteLine("\nPress 3 to Exit");
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------");

        string choice = Console.ReadLine();
        Console.WriteLine();

        while (!(choice == "1" || choice == "2" || choice == "3")) // error checking
        {
            Console.WriteLine("Please try again");
            Console.WriteLine("Press 1 if you are a customer");
            Console.WriteLine("Press 2 if you are a manager");
            Console.WriteLine("Press 3 to Exit");

            choice = Console.ReadLine();
        }

        return int.Parse(choice);
    }


 static void CustomerMenu() {

        int customerChoice = CustomerMenuChoice(); // Reading in the customerChoice into the CustomerMenuChoice method

        if (customerChoice == 5) // if user enters 5, the user is returned to the main menu
        {
            Console.WriteLine("Returning to main menu, thank you.");
        }

        while (customerChoice != 5)
        {
            if (customerChoice == 1)
            {
                Console.WriteLine("This shows movies available.\n"); // this option gives the user the opportunity to view all movies available to rent
                 //MoviesAvailable();
            }
            if (customerChoice == 2)
            {
                Console.WriteLine("This is to rent a movie.\n"); // this option gives the user the opportunity to rent a movie, with email address
                //RentMovie();
            }
            if (customerChoice == 3)
            {
                Console.WriteLine("This is to show my rented movies.\n"); // this option gives the user the opportunity to view movies a user currently has rented, with email address
                //MyRentedMovie();
            }
            if (customerChoice == 4)
            {
                Console.WriteLine("This is to return a movie.\n"); // this option gives the user the opportunity to return a movie rented
                //ReturnMovie();
            }
            customerChoice = CustomerMenuChoice();
            if (customerChoice == 5)
            {
                Console.WriteLine("Returning to main menu, thank you.");
            }
        }            
}
    static int CustomerMenuChoice()
    {
        Console.WriteLine("Below is a list of actions that can be performed by customers: ");
        Console.WriteLine("\nPress 1 to view movies available to rent.");
        Console.WriteLine("\nPress 2 to rent a movie.");
        Console.WriteLine("\nPress 3 to view a list of movies you currently have rented.");
        Console.WriteLine("\nPress 4 to return a movie rented.");
        Console.WriteLine("\nPress 5 to return to menu.");
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------");

        string customerChoice2 = Console.ReadLine();
        Console.WriteLine();

        while (!(customerChoice2 == "1" || customerChoice2 == "2" || customerChoice2 == "3" || customerChoice2 == "4") || customerChoice2 == "5") // error checking
        {
        Console.WriteLine("Please enter a valid option: ");
        Console.WriteLine("\nPress 1 to view movies available to rent.");
        Console.WriteLine("\nPress 2 to rent a movie.");
        Console.WriteLine("\nPress 3 to view a list of movies you currently have rented.");
        Console.WriteLine("\nPress 4 to return a movie rented.");
        Console.WriteLine("\nPress 5 to return to menu.");

            customerChoice2 = Console.ReadLine();
        }

        return int.Parse(customerChoice2);
    }
 static void ManagerMenu() {

        int managerChoice = ManagerMenuChoice(); // Reading in the managerChoice into the ManagerMenuChoice method

        if (managerChoice == 6) // if user enters 6, the user is returned to the main menu
        {
            Console.WriteLine("Returning to main menu, thank you.");
        }

        while (managerChoice != 6)
        {
            if (managerChoice == 1)
            {
                Console.WriteLine("This is to add a movie to inventory.\n"); // this option gives the manager the ability to add a movie to inventory
                 //AddMovie();
            }
            if (managerChoice == 2)
            {
                Console.WriteLine("This is to remove a movie from inventory.\n"); // this option gives the manager the ability to remove a movie from inventory
                //RemoveMovie();
            }
            if (managerChoice == 3)
            {
                Console.WriteLine("This is to edit a movie from inventory.\n"); // this option gives the manager the ability to edit a movie to inventory
                //EditMovie();
            }
            if (managerChoice == 4)
            {
                Console.WriteLine("This is to process a btach transaction file.\n"); // this option gives the manager the ability to process a batch transaction file
                //BatchTransaction();
            }
            if (managerChoice == 5)
            {
                Console.WriteLine("This is to access the report menu.\n"); // this option gives the manager the ability to access the report menu
                 //ReportMenu();
            }
            managerChoice = ManagerMenuChoice();
            if (managerChoice == 6)
            {
                Console.WriteLine("Returing to main menu, thank you.!"); // returns to main menu
            }
        }            
}
    static int ManagerMenuChoice()
    {
        Console.WriteLine("Below is a list of actions that can be performed by managers: ");
        Console.WriteLine("\nPress 1 to add a new movie to to the inventory.");
        Console.WriteLine("\nPress 2 remove a movie from inventory.");
        Console.WriteLine("\nPress 3 to edit a movie in inventory.");
        Console.WriteLine("\nPress 4 to process a btach transaction file.");
        Console.WriteLine("\nPress 5 to access the report menu.");
        Console.WriteLine("\nPress 6 to return to the menu.");
        Console.WriteLine("-----------------------------------------------------------------------------------------------------------");

        string managerChoice2 = Console.ReadLine();
        Console.WriteLine();

        while (!(managerChoice2 == "1" || managerChoice2 == "2" || managerChoice2 == "3" || managerChoice2 == "4") || managerChoice2 == "5" || managerChoice2 == "6") // error checking
        {
        Console.WriteLine("Please enter a valid option: ");
        Console.WriteLine("\nPress 1 to add a new movie to to the inventory.");
        Console.WriteLine("\nPress 2 remove a movie from inventory.");
        Console.WriteLine("\nPress 3 to edit a movie in inventory.");
        Console.WriteLine("\nPress 4 to process a btach transaction file.");
        Console.WriteLine("\nPress 5 to access the report menu.");
        Console.WriteLine("\nPress 6 to return to the menu.");

            managerChoice2 = Console.ReadLine();
        }

        return int.Parse(managerChoice2);
    }
}
}

Solution

  • while (!(customerChoice2 == "1" || customerChoice2 == "2" || customerChoice2 == "3" || customerChoice2 == "4") || customerChoice2 == "5")
    

    If you look at this line you can see that the parenthesis doesn't include the "5". So you are checking if the answer is something other than 1-4 OR it is 5. Meaning 5 will always count as invalid.

    I would also like to give you a few tips that could make your code shorter and simpler, if you want to.

    First off, you could make the code always go into the loop and return only once the input is valid. This way you Don't Repeat Yourself (DRY).

    static int CustomerMenuChoice()
    {
        Console.WriteLine("Below is a list of actions that can be performed by customers: ");
        while(true)
        {
            Console.WriteLine("\nPress 1 to view movies available to rent.");
            Console.WriteLine("\nPress 2 to rent a movie.");
            Console.WriteLine("\nPress 3 to view a list of movies you currently have rented.");
            Console.WriteLine("\nPress 4 to return a movie rented.");
            Console.WriteLine("\nPress 5 to return to menu.");
            Console.WriteLine("-----------------------------------------------------------------------------------------------------------");
    
            string customerChoice = Console.ReadLine();
    
            if (customerChoice == "1" || customerChoice == "2" || customerChoice == "3" || customerChoice == "4" || customerChoice == "5")
            {
                return int.Parse(customerChoice);
            }
            Console.WriteLine("Please enter a valid option: ");
        } 
    }
    

    Secondly I recommend looking a bit at the switch-case statement.

    You could use it in several places of your code like so:

    static void CustomerMenu()
    {
        int customerChoice = 0;
    
        while (customerChoice != 5)
        {
            customerChoice = CustomerMenuChoice();
    
            switch (customerChoice)
            {
                case 1:
                    Console.WriteLine("This shows movies available.\n");
                    break;
                case 2:
                    Console.WriteLine("This is to rent a movie.\n");
                    break;
                case 3:
                    Console.WriteLine("This is to show my rented movies.\n");
                    break;
                case 4:
                    Console.WriteLine("This is to show my rented movies.\n");
                    break;
                case 5:
                    Console.WriteLine("Returning to main menu, thank you.");
                    break;
                default:
                    // Option not in the list
                    break;
            }
        }
    }
    

    One last thing, you might wanna look into using Console.ReadKey() instead of Console.ReadLine(). It will return without the user having to press enter and only takes one key at a time. Personally I think it's perfect for these kinds of menus.