This is the solution to the challenge called ChallengeFirstPapaBobsWebsite, in which you are tasked to create a website for a fictitious pizza company called Papa Bob’s. This solution is one way to solve the challenge, but it is far from the only. As long as your results meet the criteria set out in the challenge, it is an acceptable solution. If you got stuck in the process of creating the web page, please only read as much as you need in order to resolve the issue you’re facing. It will not help you to go through this course without struggling with these challenges. Learning comes through experience. With that said, here is the solution to this challenge.
Step 1: Add the Default.aspx Page
To begin, create a new ASP.NET Web Forms Application called ChallengeFirstPapaBobsWebsite, choose the Empty Template and add folders and references for Web Forms:
Next, add a new Web Form to your project and name it Default. Switch to the Design view to begin adding Server Controls in the form. We need to add text and server controls to match the form layout as shown in the CS-ASP_013-Challenge_Code folder:
Step 2: Adding the Image and Header
Begin by dragging and dropping the PapaBob image from that folder into your Solution Explorer to add it to your project:
Drag and drop that reference into your Design view to add the image to the form. Do not modify any of the information in the dialog box that results from adding the image. Next to the image, type in the following text:
Highlight the text you wrote and change the font to Arial, Helvetica and change the style to an H1:
Step 3: Adding the Server Controls
Next, add a series of RadioButtons, CheckBoxes a button and Label so that your form looks like this:
Next, change the Text and ID properties on each of the Server Controls so that it matches this:
Set the GroupName for the first three RadioButtons to SizeGroup, and for the other two, set it to CrustGroup. Remember, the GroupName property keeps the user from selecting more than one option per group of RadioButtons.
Next, we need to add the text that describes the special deal discount. That will go just about the purchaseButton:
Select the ‘Papa Bob’s Special Deal’ text and change the font to Arial, Helvetica and the style to an H3. We also need to change the color of the last two words to red. To do this, highlight those words and select the foreground color property on the toolbar:
Change the color to a red of your choice, then confirm your selection to set the color.
Underneath the purchaseButton, add the text "Total: " followed by a Label control:
Underneath the Label, type in the text that explains the one pizza per order policy. Finally, change the name of the Label control to resultLabel, and change its text to ‘$0.00’.
Step 4: Working with purchaseButton_Click
Double-click on the purchaseButton to generate the code block for purchaseButton_Click. Inside that code block, create a double variable called total:
This variable will be what we perform each operation on to affect the overall price of the pizza depending on what the user selects.
To do that, we’ll create a series of conditional statements to check which size the user selected:
Next, modify the total value accordingly; 10.0 for small, 13.0 for medium and 16.0 for large:
Next, create another conditional check to see if the user selected a deep dish pizza, and modify the price accordingly:
The syntax ‘total = total + 2.0’ is perfectly acceptable in this case to modify the total. If you were to input ‘total = 2.0’, it would override any modifications made prior to this statement. However, remember that there is a shorthand way of adding to variables, and that is the += operator:
What this says is, whatever variable is on the left of the operator, add to its existing value whatever is on the right-hand side of the operator. In this case, add 2.0 to the value of total.
Step 5: CheckBox Selections with the Ternary Operator
We don’t need an else() statement for the crust selection, because if the user selected thin crust, it will not affect the total price. The next step, then is to work with the CheckBoxes. While we could write out five separate if() statements, a shortcut to this would be to use the ternary operator we’ve seen before:
Remember, the syntax for this statement works like this:
- Identify the variable that will be modified (total in this case)
- Inside the parentheses, declare the condition to be evaluated (in this case, pepperoniCheckBox checked?)
- If so, the operation after the question mark will be executed (total + 1.5)
- Otherwise, if the condition is not true, return the value after the colon (total)
The ternary operator provides a shortcut to typing out more code, and that helps to keep the overall program shorter and more readable.
Step 6: Papa Bob’s Special
The last condition we need to check is whether or not the order placed matches the requirements to receive Papa Bob’s special discount. This discount applies if the pizza has either Pepperoni, Green Peppers and Anchovies, or if it has Pepperoni, Red Peppers and Onions. The way we can solve this is by creating an if() statement that evaluates to see if either of these cases is true, using the AND symbol (&&) and the OR symbol ( || ):
Note the use of parentheses to group which conditions needed to be true in order for the discount to be applied. We wrap each condition (comprised of three toppings) in its own set of parentheses and place the OR operator between them. If you were to leave the parentheses out of the conditional check, it would not function as we need.
Underneath the if() statement, simply subtract 2.0 from total to apply the special:
Step 7: Test your Project
Finally, set the resultLabel.Text to the following:
Note: There are better ways to format the string result for currency, and we’ll be learning about that in an upcoming lesson. In the meantime, this method will suffice.
Save your project and run it to see the result:
Without Papa Bob Special
With Papa Bob Special
This completes the solution for ChallengeFirstPapaBobsWebsite. Hopefully, you were able to complete most or all of it without help. Some of the requirements called for detailed work and that can be tricky when you’re first learning. Do your best to break the problem down into small, understandable tasks that you can tackle individually. Once each individual task is complete, then you can bring it all together in order to come up with a complete result.
If you didn’t complete this on your own, you’re encouraged to try again later. Keep trying until you understand the concepts you need in order to complete the challenge. You’ll learn much better from failing and trying again than simply reading and not accomplishing the goals. Keep up the good work, it’s hard to learn, but these concepts will begin to grow on each other and you’ll find the end result to be well worth the investment. Good job!