Solution - Challenge Epic Spies Assignment

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

Previous Article  |  Next Article


Get GitHub Code

This is the solution for the challenge titled ChallengeEpicSpiesAssignment. If you're having trouble completing this challenge, read through this solution to find the area that you need help finishing. Read only as much as you need, and do your best to complete the rest on your own.

Remember that the details for the challenge are found in the epic-spies-business-rules.txt file found in the CS-ASP_019-Challenge folder. The changes we make will be to meet the business rule requirements set out in that document.

Step 1: Setup Default.aspx Page

To begin, create a new Project and call it ChallengeEpicSpiesAssignment. Add a new Web Form to this and call it Default.aspx. Switch to the Design view and add the following text and Server Controls:

firstform

SecondHalfForm

Give the Server Controls the following programmatic IDs:

  1. codeNameTextBox

  2. newAssignmentTextBox

  3. previousCalendar

  4. newCalendar

  5. endCalendar

  6. assignButton

  7. resultLabel

Clear the text from the resultLabel and change the text in the assignButton to "Assign Spy".

Step 2: Business Rule 1

The first step to complete is to satisfy the first business rule laid out for us: The calendar controls need to have initialized values. As per the instructions, they should be initialized as follows:

  • previousCalendar - Today's Date
  • newCalendar - 14 Days from Today
  • endCalendar - 21 Days from Today

In order to initialize these values, go to the Default.aspx.cs page, and type the following code in the Page_Load event:

Page_Load

This will initialize the calendar controls to the desired values, but because it is located in Page_Load, the values will be set every time the page is loaded. In order to only initialize the values the first time the page loads, check to ensure that this is not a PostBack:

PostBack

This ensures that the values will not be re-initialized when the page is posted back due to the assignButton being pressed.

Step 3: Business Rules 3 and 4

Because business rule 2 affects whether or not the budget is calculated, we will first write the code to perform the budget calculation. The budget is said to be equivalent to $500 per day between endCalendar and newCalendar. In order to perform this calculation, we will create a TimeSpan variable called totalDurationOfAssignment that determines the difference of the two calendars like so:

totalDuration

Next, we will create a new double called totalCost to hold the budget total:

totalCost

Note: totalDurationOfAssignment is a TimeSpan value, which cannot be converted implicitly to a double. To solve this, the TotalDays property is attached to totalDurationOfAssignment in order to receive a double value that we can multiply by.

Next, we need to add $1,000 to totalCost if the duration of the assignment is greater than 3 weeks. In order to do so, create in if() statement that checks this condition, then increments totalCost:

AddThousand

Finally, we need to display the message to the user in the resultLabel. In order to do this, make use of the String.Format() method we discussed in the previous lesson. This method will take in the Text properties for both TextBoxes as well as the totalCost variable:

StringFormat

Tip:

Remember that the placeholders in String.Format() are zero-based and correspond to the order in which you pass in the arguments. In this case, codeNameTextBox.Text will correspond with {0}, not {1}.

Save and run the application to see the result of both scenarios:

result1

result2

Step 4: Business Rule 2

Now that we have all the other business rules in place, the last step is to make sure that at least 14 days are between the end of the previous assignment and the start of the new assignment. To do this, create a new TimeSpan variable called timeBetweenAssignments:

timeBetween

Create an if() statement underneath your code in the assignButton_Click Event. Check if the TotalDays property of timeBetweenAssignments is less than 14. If so, we need to (1) set the resultLabel's text and (2) change the newCalendar's SelectedDate. The first part can be done using simple string concatenation, but for the calendar we need to create a new DateTime variable called earliestNewAssignmentDate:

earliestNew

This variable will always be equal to the minimum time required between assignments: 14 days. Now, we can set the newCalendar's date and the resultLabel's text. Your code should look like this:

Else

Now, save and run the application to see the result:

error

Note: Notice that the date on newCalendar has changed back to the required 2-week difference.

Conclusion

This completes the solution for the ChallengeEpicSpiesAssignment. With this challenge, you can begin to see the different elements of C# you've been learning working together to form useful and customizable software. This challenge has been the most complicated yet, but hopefully you were able to complete most, if not all of it on your own. If you struggled, try solving it on your own again at a later time. The more you try and struggle, the more you will ultimately learn. Great job with this challenge, keep it up!


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