Hello I am working on this code for a hotel reservation. so far I am stuck on the part which requests that fridays and saturdays be charged 150 instead of the 120 usual cost per night.
I have dateTime variables for the arrival date and departure date and a timeSpan variable for the duration of the stay. my thought was to step through each day of the timeSpan variable and for each occurance of the DayOfWeek.Friday and DayOfWeek.Saturday I was going to add 30 dollars (the additional cost for a weekend night) to a variable weekendCost which i would then add to the total cost.
I know I am probably going about this wrong, my teacher wanted us to use a while loop, but I am not sure how to get that to work.
any help would be greatly appreciated.
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 frmReservations : Form
{
public frmReservations()
{
InitializeComponent();
}
//public bool IsValidData()
// {
// return
// }
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, System.EventArgs e)
{
this.Close();
}
private void btnCalculate_Click(object sender, EventArgs e)
{
DateTime dtArrivalDate;
DateTime.TryParse(txtArrivalDate.Text, out dtArrivalDate);
DateTime dtDepartureDate;
DateTime.TryParse(txtDepartureDate.Text, out dtDepartureDate);
TimeSpan tsDuration = dtDepartureDate.Subtract(dtArrivalDate);
int costPerNight = 120;
for (DateTime date = dtArrivalDate; date <= dtDepartureDate; date = date.AddDays(1))
{
DayOfWeek dw = date.DayOfWeek;
string days = dw.ToString();
int weekendCost = 0;
if (dw == DayOfWeek.Friday || dw == DayOfWeek.Saturday)
{
weekendCost = weekendCost + 30;
dw ++;
int totalCost = tsDuration.Days * costPerNight + weekendCost;
int avgPricePerNight = (tsDuration.Days * costPerNight + weekendCost) / tsDuration.Days;
txtNights.Text = tsDuration.Days.ToString("n");
txtTotalPrice.Text = totalCost.ToString("n");
txtAvgPrice.Text = avgPricePerNight.ToString("n");
}
}
}
private void frmReservations_Load(object sender, EventArgs e)
{
txtArrivalDate.Text = DateTime.Today.ToString("d");
txtDepartureDate.Text = DateTime.Today.AddDays(3).ToString("d");
}
}
}