Restaurant Menu Program in C#
Introduction
Table of Contents
Welcome to our next lesson on C# programming! In this topic, we will explore how to create a simple restaurant menu program. By utilizing if-else statements, we will enable users to select dishes from the menu and display the corresponding prices. This hands-on exercise will strengthen your understanding of conditional statements in C# while offering a practical application in a menu-based scenario.
So, grab your coding utensils and sharpen your appetite for knowledge! This lesson will guide you through the steps of creating your very own C# restaurant menu program, ready to serve up a satisfying dose of coding fun and practical skills. Bon appétit!
Objectives
In this lesson, our main focus will be on developing a solid understanding of how to create a restaurant menu program using C#. By the end of this lesson, you will learn how to implement if-else statements to enable users to choose dishes from the menu and display the corresponding prices. Through hands-on practice, you will gain confidence in writing code to build a simple yet functional menu program. You’ll not only comprehend the underlying concepts of conditional statements but also practice their application in a real-world scenario. As you progress, you’ll gain confidence in handling user interactions and dynamically presenting information based on their choices.
- Understand Menu Logic: Gain a comprehensive understanding of implementing menu structures using if-else statements in C# programming. Grasp the logic behind presenting options and handling user choices.
- Learn Input Handling: Learn to efficiently handle user input, ensuring robust interaction by incorporating methods for receiving and processing menu selections.
- Practice Conditional Statements: Practice applying conditional statements to dynamically display information based on user choices, honing your skills in writing clear and effective if-else logic.
- Apply Practical Programming: Apply the acquired knowledge by creating a functional restaurant menu program. Gain hands-on experience in translating conceptual understanding into practical coding solutions.
Source code example
using System; class RestaurantMenu { static void Main(string[] args) { Console.WriteLine("iNetTutor.com - Welcome to our Restaurant!"); Console.WriteLine("\nMenu:"); Console.WriteLine("1. Pizza - $12.99"); Console.WriteLine("2. Pasta - $8.99"); Console.WriteLine("3. Salad - $6.99"); Console.WriteLine("4. Burger - $10.99"); Console.Write("\nEnter your choice (1-4): "); int choice = int.Parse(Console.ReadLine()); double price; if (choice == 1) { price = 12.99; } else if (choice == 2) { price = 8.99; } else if (choice == 3) { price = 6.99; } else if (choice == 4) { price = 10.99; } else { Console.WriteLine("Invalid choice. Please try again."); price = 0.0; // Set price to 0 for invalid choice } if (price > 0.0) // Only print price if choice is valid { Console.WriteLine($"You selected: {choice}. The price is: ${price:F2}"); } Console.ReadLine(); } }
Explanation
This C# code implements a simple restaurant menu program that allows users to choose a dish and displays the corresponding price. Here’s a breakdown of the code:
- Introduction:
- using System;: This line tells the program to use the standard .NET libraries.
- class RestaurantMenu: This line defines the main class of the program.
- static void Main(string[] args): This is the entry point of the program, where the code execution starts.
- Welcome Message:
- Console.WriteLine(“iNetTutor.com – Welcome to our Restaurant!”);: This line prints a welcome message with the website name.
- Menu Display:
- Console.WriteLine(“\nMenu:”);: This line prints a newline followed by the word “Menu:”.
- Console.WriteLine(“1. Pizza – $12.99”); and similar lines: These lines print each menu item with its number, name, and price.
- User Input:
- Console.Write(“\nEnter your choice (1-4): “);: This line prompts the user to enter their choice as a number between 1 and 4.
- int choice = int.Parse(Console.ReadLine());: This line reads the user’s input, converts it to an integer, and stores it in the choice variable.
- Price Calculation:
- double price;: This line declares a double variable price to store the dish price.
- An if-else if chain assigns the corresponding price to the price variable based on the user’s choice:
- If choice is 1, price is set to 12.99.
- Similar conditions exist for choices 2, 3, and 4.
- If the choice is invalid (not between 1 and 4), an error message is printed, and the price is set to 0.
- Price Output:
- if (price > 0.0): This condition checks if the price is valid (not 0 due to invalid choice).
- If valid, the selected dish and its price are displayed in a formatted string using Console.WriteLine.
- Closing:
- Console.ReadLine();: This line pauses the program before closing, allowing the user to see the output.
Additional Notes:
- This code uses basic if-else if statements. You could explore using a switch statement for a more concise version.
- Consider adding more dishes and prices to the menu.
- The code could be improved with error handling for non-numeric input or invalid choices.
Overall, this code provides a basic implementation of a restaurant menu program in C#. You can use it as a starting point to explore further features and improve its functionality.
Output
Summary
In this C# lesson, we created a simple console program for a restaurant menu. The program allows users to choose a dish by entering a number (1-4) and then displays the corresponding price. We utilized if-else statements to match the user’s choice with the appropriate dish and price. The code also handles invalid choices gracefully, providing an informative message and setting the price to 0.0. The user is then informed of their selection and the associated price, promoting an understanding of conditional logic in programming and practical menu-based applications. This exercise encourages learners to practice control flow structures and enhance their ability to create interactive console programs.
Exercises and Assessment
- Expand the Menu:
- Add more dishes and corresponding prices to the menu. Modify the program to accommodate the extended menu.
- Ensure the program gracefully handles additional choices and displays the correct prices.
- Input Validation:
- Enhance input validation to check for non-numeric or out-of-range entries.
- Implement a loop that prompts users until they provide a valid choice.
- Custom Messages:
- Modify the program to display personalized messages based on the user’s selection.
- For example, add a congratulatory message if they choose a special dish.
Assessment:
Scenario: Restaurant Specials
Your task is to modify the existing restaurant menu program to incorporate daily specials. Each day, a special dish is featured with a discounted price.
- Special Dish Integration:
- Introduce a new menu option (e.g., option 5) for the daily special with a discounted price.
- Implement logic to identify and display the special dish.
- Price Adjustment:
- Introduce a discount factor for the special dish (e.g., 20% off the regular price).
- Calculate and display the discounted price for the special.
- Interactive Experience:
- Prompt the user if they want to explore the daily special and guide them through the process.
- Ensure the program continues to handle other menu items seamlessly.
These exercises and assessments encourage learners to extend functionality, improve input validation, and create dynamic programs. They reinforce the understanding of conditional statements and enhance practical problem-solving skills in a menu-driven application.
Related Topics and Articles:
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.