I have a project were the user enters an arrival date and a departure date and clicks the Calculate button or presses the enter key and the app. calculates, formats, and displays the number of nights and the total price.
The price per night should be $115. The application should validate both entries to make sure they are dates that are on or after the current date. The application should also validate the departure date to be sure that it is after the arrival date. This app. should only accept reservations within five years of the current date.
When the app. starts, it should display the current date in the first text box and the current date plus 3 days in the second text box. That way, the user can modify these default dates as necessary.
I have been breaking my head trying to figure out what I'm doing wrong since I can get the dates to appear in the text boxes as specified and only have a vague idea as to how to accept reservations for up 5 years. :'(
Any help will be appreciated. Following is the messed up code I have so far:
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 CalculateReservation
{
public partial class frmReservations : Form
{
public frmReservations()
{
InitializeComponent();
//DateTime arrivalDate = new DateTime();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnCalculate_Click(object sender, EventArgs e)
{
//DateTime arrivalDate = DateTime.Now;
DateTime arrivalDate = DateTime.Parse(txtArrivalDate.Text);
DateTime departureDate = DateTime.Parse(txtDepartureDate.Text);
TimeSpan numberOfNights = departureDate.Subtract(arrivalDate);
int totalPrice = int.Parse(txtTotalPrice.Text);
string pricePerNight = String.Format("0:c}, 115.00");
if (txtArrivalDate.Text == "")
{
txtArrivalDate.Focus();
MessageBox.Show("Please enter a valid date");
txtArrivalDate.Focus();
}
else if (txtDepartureDate.Text == "")
{
txtDepartureDate.Focus();
MessageBox.Show("You must enter a departure date");
txtDepartureDate.Focus();
}
else if (arrivalDate != DateTime.Today)
{
txtArrivalDate.Focus();
MessageBox.Show("Date is out of range");
}
else if (departureDate >= arrivalDate)
{
txtDepartureDate.Focus();
MessageBox.Show("Date is invalid", "Enter a date other than today's date");
}
//arrivalDate = txtArrivalDate.Text;
//departureDate = txtDepartureDate;
totalPrice = numberOfNights.Days * pricePerNight.Length;
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}