I\'m stuck on this, thank you for any help you guys can provide. Can you please
ID: 3844335 • Letter: I
Question
I'm stuck on this, thank you for any help you guys can provide. Can you please keep the form the same, I do not need a drop down list.
In this exercise, you’ll add code that calculates
1. the number of nights,
2. total price,
3. and average price
for a reservation based on the arrival and departure dates the user enters.
Open the project and implement the calculations
· display the code for the form and notice that some of the methods are commented out so they don’t return errors.
2. Add code to get the arrival and departure dates the user enters when the user clicks the Calculate button.
1. Then, calculate the number of days between those dates,
2. calculate the total price based on a price per night of $120,
3. calculate the average price per night,
4. and display the results.
3. Test the application to be sure it works correctly. At this point, the average price will be the same as the nightly price.
Enhance the way the form works
4. Add an event handler for the Load event of the form. This event handler should get the current date and three days after the current date and assign these dates to the Arrival Date and Departure Date text boxes as default values. Be sure to format the dates as shown above.
5. Modify the code so Friday and Saturday nights are charged at $150 and other nights are charged at $120.
· One way to do this is to use a while loop that checks the day for each date of the reservation.
6. Test the application to be sure that the default dates are displayed correctly and that the totals are calculated correctly.
Add code to validate the dates
7. Uncomment the IsDateTime method and then add code to check that the arrival and departure dates are valid dates.
8. Uncomment the IsWithinRange method and then add code to check that the arrival and departure dates are within a range that includes the minimum and maximum dates that are passed to it.
9. Uncomment the IsValidData method and then add code that uses the IsPresent, IsDateTime, and IsWithinRange methods to validate the arrival and departure dates. These dates should be in a range from the current date to five years after the current date.
10. Add code that uses the IsValidData method to validate the arrival and departure dates. In addition, add code to check that the departure date is after the arrival date.
11. Test the application to be sure the dates are validated properly.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Reservations
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//public bool IsValidData()
//{
//}
public bool IsPresent(TextBox textBox, string name)
{
if (textBox.Text == "")
{
MessageBox.Show(name + " is a required field.", "Entry Error");
textBox.Focus();
return false;
}
return true;
}
//public bool IsDateTime(TextBox textBox, string name)
//{
//}
//public bool IsWithinRange(TextBox textBox, string name,
// DateTime min, DateTime max)
//{
//}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
Explanation / Answer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Reservations
{
public partial class frmReservations : Form
{
const int PRICE_PER_DAY = 115;
public frmReservations()
{
InitializeComponent();
}
private void frmReservations_Load(object sender, EventArgs e)
{
textBoxDepart.Text = DateTime.Today.ToString("d");
textBoxArrive.Text = DateTime.Today.AddDays(3).ToString("d");
}
private void btnOk_Click(object sender, EventArgs e)
{
DateTime dtDepart;
DateTime dtArrive;
if (string.IsNullOrEmpty(textBoxDepart.Text) || !DateTime.TryParse(textBoxDepart.Text, out dtDepart))
{
MessageBox.Show("Invalid departure date!");
return;
}
if (string.IsNullOrEmpty(textBoxArrive.Text) ||!DateTime.TryParse(textBoxArrive.Text, out dtArrive))
{
MessageBox.Show("Invalid arrival date!");
return;
}
dtDepart = dtDepart.Date; //Trim off the time portion if they entered it
dtArrive = dtArrive.Date;
if (dtDepart < DateTime.Today)
{
MessageBox.Show("The departure date cannot be before today");
return;
}
//If it is less than dtDepart it is also less than today, so we dont need to check both
if (dtArrive < dtDepart)
{
MessageBox.Show("The departure date cannot be before the departure date");
return;
}
//Days in a year is technically 365.24something since every fourth year
//is a leap year except every 100 years we don't have a leap year unless
//it is the 400th year. So no leap year on 2100, 2200, but we do have
//leap year on 2400. How exact do you need to be?
DateTime dtUpperBoundary = DateTime.Today.AddDays(365 * 5);
if (dtDepart > dtUpperBoundary)
{
MessageBox.Show("The departure date is more than 5 years in the future");
return;
}
if (dtArrive > dtUpperBoundary)
{
MessageBox.Show("The arrival date is more than 5 years in the future");
return;
}
//Do you want to check that?
if (dtArrive == dtDepart)
{
MessageBox.Show("You cannot depart and arrive on the same date");
return;
}
//At this point the dates should be good
int duration = Convert.ToInt32(Math.Ceiling(Math.Abs(dtArrive.Subtract(dtDepart).TotalDays))) + 1;
int price = duration * PRICE_PER_DAY;
string sNumberOfDays = duration.ToString();
string sArrivalDate = dtArrive.ToString("d");
string sDeparture = dtDepart.ToString("d");
string sTotalPrice = price.ToString("C2");
StringBuilder sb = new StringBuilder();
sb.AppendLine("Number of nights: " + sNumberOfDays);
sb.AppendLine("Arrival Date: " + sArrivalDate);
sb.AppendLine("Departure Date: " + sDeparture);
sb.AppendLine("Total Price: " + sTotalPrice);
MessageBox.Show(sb.ToString());
//MessageBox.Show(string.Format("The price will be {0:C2}", price));
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}