Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Windows.Forms;
- namespace Reservations
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- public bool IsValidData()
- {
- return
- IsPresent(txtArrivalDate, "Arrival Date") &&
- IsDateTime(txtArrivalDate, "Arrival Date") &&
- IsWithinRange(txtArrivalDate, "Arrival Date", DateTime.Today, DateTime.Today.AddYears(5)) &&
- IsPresent(txtDepartureDate, "Departure Date") &&
- IsDateTime(txtDepartureDate, "Departure Date") &&
- IsWithinRange(txtDepartureDate, "Departure Date", DateTime.Today, DateTime.Today.AddYears(5));
- }
- 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)
- {
- if (DateTime.TryParse(textBox.Text, out DateTime dt))
- {
- return true;
- }
- else
- {
- MessageBox.Show(name + " is not valid ", "ENTRY ERROR");
- return false;
- }
- }
- public bool IsWithinRange(TextBox textBox, string name,
- DateTime min, DateTime max)
- {
- DateTime dt = DateTime.Parse(textBox.Text);
- if (dt < min || dt > max)
- {
- MessageBox.Show(name + " must be between " + min + " and " + max + " .", "ENTRY ERROR");
- textBox.Focus();
- return false;
- }
- return true;
- }
- private void btnExit_Click(object sender, EventArgs e)
- {
- this.Close();
- }
- private void btnCalculate_Click(object sender, EventArgs e)
- {
- try
- {
- if (IsValidData())
- {
- DateTime arrDate = Convert.ToDateTime(txtArrivalDate.Text);
- DateTime depDate = Convert.ToDateTime(txtDepartureDate.Text);
- if (arrDate >= depDate)
- {
- MessageBox.Show("Departure date should be after the arrival date");
- }
- else
- {
- double price_perNight = 0;
- double total = 0;
- double average;
- DateTime arrivalDate = DateTime.Today;
- DateTime.TryParse(txtArrivalDate.Text, out arrivalDate);
- DateTime departureDate = DateTime.Today;
- DateTime.TryParse(txtDepartureDate.Text, out departureDate);
- TimeSpan totalTime = departureDate.Subtract(arrivalDate);
- int totalNights = totalTime.Days;
- txtNights.Text = totalNights.ToString();
- DayOfWeek dayOfWeek = arrivalDate.DayOfWeek;
- for (DateTime counter = DateTime.Now; counter < departureDate; counter = counter.AddDays(1))
- {
- if (counter.DayOfWeek == DayOfWeek.Friday || counter.DayOfWeek == DayOfWeek.Saturday)
- {
- price_perNight = 150;
- }
- else
- {
- price_perNight = 120;
- }
- total += price_perNight;
- }
- txtTotalPrice.Text = total.ToString("c");
- average = total / totalNights;
- txtAvgPrice.Text = average.ToString("c");
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace + "\r\n" + ex.GetType().ToString(),
- "EXCEPTION");
- }
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- DateTime currentDate = DateTime.Today;
- DateTime depDate = currentDate.AddDays(3);
- txtArrivalDate.Text = currentDate.ToShortDateString().Replace("-", "/");
- txtDepartureDate.Text = depDate.ToShortDateString().Replace("-", "/");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement