Solution - Challenge First Papa Bob's Website

Tutorial Series: Free C# Fundamentals via ASP.NET Web Apps

Previous Article  |  Next Article


Get GitHub Code

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:


cs-asp-013---challengefirstpapabobswebsite-001


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:


ChallengeFirstPapaBobsWebsite


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:


cs-asp-013---challengefirstpapabobswebsite-002


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:


cs-asp-013---challengefirstpapabobswebsite-003


Highlight the text you wrote and change the font to Arial, Helvetica and change the style to an H1:


cs-asp-013---challengefirstpapabobswebsite-004


Step 3: Adding the Server Controls

Next, add a series of RadioButtons, CheckBoxes a button and Label so that your form looks like this:


cs-asp-013---challengefirstpapabobswebsite-005


Next, change the Text and ID properties on each of the Server Controls so that it matches this:


cs-asp-013---challengefirstpapabobswebsite-006


  1. smallRadioButton
  2. mediumRadioButotn
  3. largeRadioButton
  4. thinRadioButton
  5. deepRadioButton
  6. pepperoniCheckBox
  7. onionCheckBox
  8. greenPepperCheckBox
  9. redPepperCheckBox
  10. anchovieCheckBox
  11. purchaseButton

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:


cs-asp-013---challengefirstpapabobswebsite-007


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:


cs-asp-013---challengefirstpapabobswebsite-008


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:


cs-asp-013---challengefirstpapabobswebsite-009


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:


cs-asp-013---challengefirstpapabobswebsite-010


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:


cs-asp-013---challengefirstpapabobswebsite-011


Next, modify the total value accordingly; 10.0 for small, 13.0 for medium and 16.0 for large:


cs-asp-013---challengefirstpapabobswebsite-012


Next, create another conditional check to see if the user selected a deep dish pizza, and modify the price accordingly:


cs-asp-013---challengefirstpapabobswebsite-013


Tip:

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:


cs-asp-013---challengefirstpapabobswebsite-014


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:


cs-asp-013---challengefirstpapabobswebsite-015


Remember, the syntax for this statement works like this:

  1. Identify the variable that will be modified (total in this case)
  2. Inside the parentheses, declare the condition to be evaluated (in this case, pepperoniCheckBox checked?)
  3. If so, the operation after the question mark will be executed (total + 1.5)
  4. 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 ( || ):


cs-asp-013---challengefirstpapabobswebsite-016


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:


cs-asp-013---challengefirstpapabobswebsite-017


Step 7: Test your Project

Finally, set the resultLabel.Text to the following:


cs-asp-013---challengefirstpapabobswebsite-018


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


cs-asp-013---challengefirstpapabobswebsite-019


With Papa Bob Special


cs-asp-013---challengefirstpapabobswebsite-020


Conclusion:

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!


Related Articles in this Tutorial:

Lesson 1 - Series Introduction

Lesson 2 - Installing Visual Studio 2015

Lesson 3 - Building Your First Web App

Lesson 4 - Understanding What You Just Did

Lesson 5 - Working with Projects in Visual Studio

Lesson 6 - Simple Web Page Formatting in Visual Studio

Challenge 1

Solution 1

Lesson 7 - Variables and Data Types

Lesson 8 - Data Type Conversion

Lesson 9 - Arithmetic Operators

Lesson 10 - C# Syntax Basics

Challenge 2 - ChallengeSimpleCalculator

Solution - ChallengeSimpleCalculator

Lesson 11 - Conditional If Statements

Lesson 12 - The Conditional Ternary Operator

Challenge 3 - ChallengeConditionalRadioButton

Solution - Challenge Conditional RadioButton

Lesson 13 - Comparison and Logical Operators

Lesson 13 Challenge - First Papa Bob's Website

Solution - Challenge First Papa Bob's Website

Lesson 14 - Working with Dates and Times

Lesson 15 - Working With Spans of Time

Lesson 16 - Working with the Calendar Server Control

Challenge 4 - Challenge Days Between Dates

Solution - Challenge Days Between Dates

Lesson 17 - Page_Load and Page.IsPostBack

Lesson 18 - Setting a Break Point and Debugging

Lesson 19 - Formatting Strings

Challenge 5 - Challenge Epic Spies Assignment

Solution - Challenge Epic Spies Assignment

Lesson 20 - Maintaining State with ViewState

Lesson 21 - Storing Values in Arrays

Lesson 22 - Understanding Multidimensional Arrays

Lesson 23 - Changing the Length of an Array

Challenge 6 - Challenge Epic Spies Asset Tracker

Solution - Challenge Epic Spies Asset Tracker

Lesson 24 - Understanding Variable Scope

Lesson 25 - Code Blocks and Nested If Statements

Lesson 26 - Looping with the For Iteration Statement

Challenge 7 - Challenge For Xmen Battle Count

Solution - Challenge For Xmen Battle Count

Lesson 27 - Looping with the while() & do...while() Iteration Statements

Lesson 28 - Creating and Calling Simple Helper Methods

Lesson 29 - Creating Methods with Input Parameters

Lesson 30 - Returning Values from Methods

Lesson 31 - Creating Overloaded Methods

Lesson 32 - Creating Optional Parameters

Lesson 33 - Creating Names Parameters

Lesson 34 - Creating Methods with Output Parameters

Challenge 8 - Challenge Postal Calculator Helper Methods

Solution - Challenge Postal Calculator Helper Methods

Mega Challenge Casino

Solution - Mega Challenge Casino

Lesson 35 - Manipulating Strings

Challenge 9 - Phun With Strings

Solution - Challenge Phun With Strings

Lesson 36 - Introduction to Classes and Objects

Challenge - Hero Monster Classes Part 1

Solution - Hero Monster Classes Part 1

Challenge - Hero Monster Classes Part 2

Solution - Challenge Hero Monster Classes Part 2

Lesson 37 - Creating Class Files Creating Cohesive Classes and Code Navigation

Lesson 38 - Understanding Object References and Object Lifetime

Lesson 39 - Understanding the .NET Framework and Compilation

Lesson 40 - Namespaces and Using Directives

Lesson 41 - Creating Class Libraries and Adding References to Assemblies

Lesson 42 - Accessibility Modifiers, Fields and Properties

Lesson 43 - Creating Constructor Methods

Lesson 44 - Naming Conventions for Identifiers

Lesson 45 - Static vs Instance Members

Challenge 10 - Challenge Simple Darts

Solution - Challenge Simple Darts

Lesson 46 - Working with the List Collection

Lesson 47 - Object Initializers

Lesson 48 - Collection Initializers

Lesson 49 - Working with the Dictionary Collection

Lesson 50 - Looping with the foreach Iteration Statement

Lesson 51 - Implicitly-Typed Variables with the var Keyword

Challenge 11 - Challenge Student Courses

Solution - Challenge Student Courses

Mega Challenge War

Solution - Mega Challenge War

Lesson 52 - Creating GUIDs

Lesson 53 - Working with Enumerations

Lesson 54 - Understanding the switch() Statement

Lesson 55 - First Pass at the Separation of Concerns Principle

Lesson 56 - Understanding Exception Handling

Lesson 57 - Understanding Global Exception Handling

Lesson 58 - Understanding Custom Exceptions

Lesson 59 - Creating a Database in Visual Studio

Lesson 60 - Creating an Entity Data Model

Lesson 61 - Displaying the DbSet Result in an ASP.NET GridView

Lesson 62 - Implementing a Button Command in a GridView

Lesson 63 - Using a Tools-Centric Approach to Building a Database Application

Lesson 64 - Using a Maintenance-Driven Approach to Building a Database Application

Lesson 65 - Creating a New Instance of an Entity and Persisting it to the Database

Lesson 66 - Package Management with NuGet

Lesson 67 - NuGet No-Commit Workflow

Lesson 68 - Introduction the Twitter Bootstrap CSS Framework

Lesson 69 - Mapping Enum Types to Entity Properties in the Framework Designer

Lesson 70 - Deploying the App to Microsoft Azure Web Services Web Apps

Papa Bob's Mega Challenge

Papa Bob's Mega Solution Part 1 - Setting up the Solution

Papa Bob's Mega Solution Part 2 - Adding an Order to the Database

Papa Bob's Mega Solution Part 3 - Passing an Order from the Presentation Layer

Papa Bob's Mega Solution Part 4 - Creating the Order Form

Papa Bob's Mega Solution Part 5 - Adding Enums

Papa Bob's Mega Solution Part 6 - Creating an Order with Validation

Papa Bob's Mega Solution Part 7 - Calculating the Order Price

Papa Bob's Mega Solution Part 8 - Displaying the Price to the User

Papa Bob's Mega Solution Part 9 - Creating the Order Management Page


Comments

Please login or register to add a comment