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();
      }
   }
}

Recommended Answers

All 3 Replies

This should do it:

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 daniweb
{
  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;
      MessageBox.Show(string.Format("The price will be {0:C2}", price));

    }


    private void btnExit_Click(object sender, EventArgs e)
    {
      this.Close();
    }

  }
}

The code works well, and I hate to be a bothersome person, but I need to put the amount of nights and total price in text boxes. Since you posted your reply I have been trying to parse or convert the date to a string, but of course it will not allow me because it is a date, I also tried to use the TimeSpan for the amount of nights without any luck. The same thing is happening when I try to get the total price to show on the text box it keeps relating it to the DateTime.

I have attached an image of how it should look. I really have exhausted all I could think of (have not gotten much sleep either), but I guess I'm not getting it fast enough. Again, thanks for any help you can provide. :S

Here I put the values in a string, which you can assign to the textboxes.

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 daniweb
{
  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 days: " + 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();
    }

  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.