Advertisement
monaliza86

Hotel Reservations

Jan 27th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.65 KB | None | 0 0
  1. using System;
  2. using System.Windows.Forms;
  3.  
  4. namespace Reservations
  5. {
  6.     public partial class Form1 : Form
  7.     {
  8.         public Form1()
  9.         {
  10.             InitializeComponent();
  11.         }
  12.  
  13.         public bool IsValidData()
  14.         {
  15.             return
  16.  
  17.                 IsPresent(txtArrivalDate, "Arrival Date") &&
  18.                 IsDateTime(txtArrivalDate, "Arrival Date") &&
  19.                 IsWithinRange(txtArrivalDate, "Arrival Date", DateTime.Today, DateTime.Today.AddYears(5)) &&
  20.  
  21.                 IsPresent(txtDepartureDate, "Departure Date") &&
  22.                 IsDateTime(txtDepartureDate, "Departure Date") &&
  23.                 IsWithinRange(txtDepartureDate, "Departure Date", DateTime.Today, DateTime.Today.AddYears(5));
  24.         }
  25.  
  26.         public bool IsPresent(TextBox textBox, string name)
  27.         {
  28.             if (textBox.Text == "")
  29.             {
  30.                 MessageBox.Show(name + " is a required field.", "Entry Error");
  31.                 textBox.Focus();
  32.                 return false;
  33.             }
  34.             return true;
  35.         }
  36.  
  37.         public bool IsDateTime(TextBox textBox, string name)
  38.         {
  39.             if (DateTime.TryParse(textBox.Text, out DateTime dt))
  40.             {
  41.                 return true;
  42.             }
  43.             else
  44.             {
  45.                 MessageBox.Show(name + " is not valid ", "ENTRY ERROR");
  46.                 return false;
  47.             }
  48.         }
  49.  
  50.         public bool IsWithinRange(TextBox textBox, string name,
  51.             DateTime min, DateTime max)
  52.         {
  53.             DateTime dt = DateTime.Parse(textBox.Text);
  54.  
  55.             if (dt < min || dt > max)
  56.             {
  57.                 MessageBox.Show(name + " must be between " + min + " and " + max + " .", "ENTRY ERROR");
  58.                 textBox.Focus();
  59.                 return false;
  60.             }
  61.             return true;
  62.         }
  63.  
  64.         private void btnExit_Click(object sender, EventArgs e)
  65.         {
  66.             this.Close();
  67.         }
  68.  
  69.         private void btnCalculate_Click(object sender, EventArgs e)
  70.         {
  71.             try
  72.             {
  73.                 if (IsValidData())
  74.                 {
  75.                     DateTime arrDate = Convert.ToDateTime(txtArrivalDate.Text);
  76.                     DateTime depDate = Convert.ToDateTime(txtDepartureDate.Text);
  77.  
  78.                     if (arrDate >= depDate)
  79.                     {
  80.                         MessageBox.Show("Departure date should be after the arrival date");
  81.                     }
  82.                     else
  83.                     {
  84.                         double price_perNight = 0;
  85.                         double total = 0;
  86.                         double average;
  87.                         DateTime arrivalDate = DateTime.Today;
  88.                         DateTime.TryParse(txtArrivalDate.Text, out arrivalDate);
  89.                         DateTime departureDate = DateTime.Today;
  90.                         DateTime.TryParse(txtDepartureDate.Text, out departureDate);
  91.                         TimeSpan totalTime = departureDate.Subtract(arrivalDate);
  92.                         int totalNights = totalTime.Days;
  93.                         txtNights.Text = totalNights.ToString();
  94.  
  95.                         DayOfWeek dayOfWeek = arrivalDate.DayOfWeek;
  96.  
  97.                         for (DateTime counter = DateTime.Now; counter < departureDate; counter = counter.AddDays(1))
  98.                         {
  99.                             if (counter.DayOfWeek == DayOfWeek.Friday || counter.DayOfWeek == DayOfWeek.Saturday)
  100.                             {
  101.                                 price_perNight = 150;
  102.                             }
  103.                             else
  104.                             {
  105.                                 price_perNight = 120;
  106.                             }
  107.                             total += price_perNight;
  108.                         }
  109.                         txtTotalPrice.Text = total.ToString("c");
  110.                         average = total / totalNights;
  111.                         txtAvgPrice.Text = average.ToString("c");
  112.                     }
  113.                 }
  114.             }
  115.             catch (Exception ex)
  116.             {
  117.                 MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace + "\r\n" + ex.GetType().ToString(),
  118.                     "EXCEPTION");
  119.             }
  120.         }
  121.  
  122.         private void Form1_Load(object sender, EventArgs e)
  123.         {
  124.             DateTime currentDate = DateTime.Today;
  125.             DateTime depDate = currentDate.AddDays(3);
  126.             txtArrivalDate.Text = currentDate.ToShortDateString().Replace("-", "/");
  127.             txtDepartureDate.Text = depDate.ToShortDateString().Replace("-", "/");
  128.  
  129.          
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement