Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Code challenges will reinforce what you've learned and help you gain some confidence before proceeding.
You've been asked to add a feature to your company's software. The feature is intended to improve the renewal rate of subscriptions to the software. Your task is to display a renewal message when a user logs into the software system and is notified their subscription will soon end. You'll need to add a couple of decision statements to properly add branching logic to the application to satisfy the requirements.
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.
To create the initial code for this challenge, enter the following code:
Random random = new Random();
int daysUntilExpiration = random.Next(12);
int discountPercentage = 0;
// Your code goes here
Notice that this code will generate a random number with a value of 0 - 11. The random number is assigned to an integer variable named daysUntilExpiration
. You have another integer variable named discountPercentage
that is initialized to 0
.
Important
In this challenge, you can only remove the code comment. In other words, you may remove the line of code that starts with //
, but you may not remove any other code. Furthermore, you must use each of the variables in your solution.
Rule 1: Your code should only display one message.
The message that your code displays will depend on the other five rules. For rules 2-6, the higher numbered rules take precedence over the lower numbered rules.
Rule 2: If the user's subscription will expire in 10 days or less, display the message:
Your subscription will expire soon. Renew now!
Rule 3: If the user's subscription will expire in five days or less, display the messages:
Your subscription expires in _ days.
Renew now and save 10%!
Note
Be sure to replace the _
character displayed in the message above with the value stored in the variable daysUntilExpiration
when you construct your message output.
Rule 4: If the user's subscription will expire in one day, display the messages:
Your subscription expires within a day!
Renew now and save 20%!
Rule 5: If the user's subscription has expired, display the message:
Your subscription has expired.
Rule 6: If the user's subscription doesn't expire in 10 days or less, display nothing.
if
statementsYour solution must use separate if
and if-else
statements to implement the business rules. The if-else
statement can include multiple else if
parts.
Create an if-else
statement that displays a message about when the subscription will expire.
Tip
Use an else if
to ensure each expiration rule is accounted for.
Create a separate if
statement that displays a discount offer.
The business rules indicate when a discount should be offered.
Whether you get stuck and need to peek at the solution or you finish successfully, proceed to view a solution to this challenge.
Please sign in to use this experience.
Sign in