Exercise - Complete a challenge activity using Boolean expressions
Code challenges reinforce what you've learned and help you gain some confidence before continuing on.
Decision logic challenge
In this challenge, you implement decision logic based on a series of business rules. The business rules specify the access that is granted to users based on their role-based permissions and their career level. Code branches display a different message to the user depending on their permissions and level.
Initialize permission and level values
Ensure that you have an empty Program.cs file open in Visual Studio Code.
If necessary, open Visual Studio Code, and then complete the following steps to prepare a Program.cs file in the Editor:
On the File menu, select Open Folder.
Use the Open Folder dialog to navigate to, and then open, the CsharpProjects folder.
In the Visual Studio Code EXPLORER panel, select Program.cs.
On the Visual Studio Code Selection menu, select Select All, and then press the Delete key.
Type the following code into the Visual Studio Code Editor:
string permission = "Admin|Manager"; int level = 55;
Review the initial code lines.
Your application will be using a combination of
permission
andlevel
to apply/evaluate the business rules in this challenge scenario. The full list of conditions for business rules is specified in the next step. Your completed solution must usepermission
andlevel
.Tip
To sufficiently test all of the combinations for
permission
andlevel
that are described in the business rules below, you'll need to assign additional values to these variables and run the application multiple times.
Implement business rules
Important
You'll need to use the Contains()
helper method to determine whether the value assigned to the permission
string contains one of the permission values specified by the "business rules". For example, the expression permission.Contains("Admin")
will return true
when using the initial data values specified in the code above.
Here are the Business Rules that your solution must satisfy:
If the user is an Admin with a level greater than 55, output the message:
Welcome, Super Admin user.
If the user is an Admin with a level less than or equal to 55, output the message:
Welcome, Admin user.
If the user is a Manager with a level 20 or greater, output the message:
Contact an Admin for access.
If the user is a Manager with a level less than 20, output the message:
You do not have sufficient privileges.
If the user isn't an Admin or a Manager, output the message:
You do not have sufficient privileges.
Update your Program.cs code to accommodate each of the business rules.
Save your code.
Test your solution using the initial data values suggested
Build and run your code.
Evaluate the output.
When you run your code, including the initial configuration data, you should see the following output:
Welcome, Admin user.
Test for the other business rules
Update the values assigned to
permission
andlevel
.Save and run your code.
Evaluate the output to verify that the other business rules are satisfied.
Whether you get stuck and need to peek at the solution or you finish successfully, continue on to view a solution to this challenge.