Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I could use some help witht his question! The code must be a C# form, and it mus

ID: 3844268 • Letter: I

Question

I could use some help witht his question!

The code must be a C# form, and it must be simple and easy to understand as I am a beginner.

---------------------------------------------------------------------------------------

In this exercise, you’ll add code that calculates

1.the number of nights,

2.total price,

3.and average price

for a reservation based on the arrival and departure dates the user enters.

Open the project and implement the calculations

1.     Open the Reservations (Start) project in the Assignment7EX1_Reservations directory.

Then, display the code for the form and notice that some of the methods are commented out so they don’t return errors.

2.     Add code to get the arrival and departure dates the user enters when the user clicks the Calculate button.

1.Then, calculate the number of days between those dates,

2.calculate the total price based on a price per night of $120,

3.calculate the average price per night,

4.and display the results.

3.     Test the application to be sure it works correctly. At this point, the average price will be the same as the nightly price.

Enhance the way the form works

4.     Add an event handler for the Load event of the form. This event handler should get the current date and three days after the current date and assign these dates to the Arrival Date and Departure Date text boxes as default values. Be sure to format the dates as shown above.

5.     Modify the code so Friday and Saturday nights are charged at $150 and other nights are charged at $120.

One way to do this is to use a while loop that checks the day for each date of the reservation.

6.     Test the application to be sure that the default dates are displayed correctly and that the totals are calculated correctly.

Add code to validate the dates

7.     Uncomment the IsDateTime method and then add code to check that the arrival and departure dates are valid dates.

8.     Uncomment the IsWithinRange method and then add code to check that the arrival and departure dates are within a range that includes the minimum and maximum dates that are passed to it.

9.     Uncomment the IsValidData method and then add code that uses the IsPresent, IsDateTime, and IsWithinRange methods to validate the arrival and departure dates. These dates should be in a range from the current date to five years after the current date.

10. Add code that uses the IsValidData method to validate the arrival and departure dates. In addition, add code to check that the departure date is after the arrival date.

11. Test the application to be sure the dates are validated properly.

--------------------------------------------------------------------------------------------------------

The code that needs to be modified

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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//public bool IsValidData()
//{

//}

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, EventArgs e)
{
this.Close();
}

}
}

------------------------------------------------------------

Thanks!

Reservations Amival date: /29/2016 Departure date: 2/1/2016 Number of nights 3 Total price: $420.00 Avg. price per night: $140.00 Calculate Exit

Explanation / Answer

frmReservations.Designer.cs

namespace Reservations
{
    partial class frmReservations
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Label5 = new System.Windows.Forms.Label();
            this.btnExit = new System.Windows.Forms.Button();
            this.btnCalculate = new System.Windows.Forms.Button();
            this.txtDepartureDate = new System.Windows.Forms.TextBox();
            this.txtArrivalDate = new System.Windows.Forms.TextBox();
            this.Label4 = new System.Windows.Forms.Label();
            this.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.txtNights = new System.Windows.Forms.TextBox();
            this.txtAvgPrice = new System.Windows.Forms.TextBox();
            this.txtTotalPrice = new System.Windows.Forms.TextBox();
            this.label3 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            //
            // Label5
            //
            this.Label5.Location = new System.Drawing.Point(22, 64);
            this.Label5.Name = "Label5";
            this.Label5.Size = new System.Drawing.Size(96, 16);
            this.Label5.TabIndex = 34;
            this.Label5.Text = "Number of nights:";
            this.Label5.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            //
            // btnExit
            //
            this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.btnExit.Location = new System.Drawing.Point(132, 155);
            this.btnExit.Name = "btnExit";
            this.btnExit.Size = new System.Drawing.Size(80, 24);
            this.btnExit.TabIndex = 33;
            this.btnExit.Text = "E&xit";
            this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
            //
            // btnCalculate
            //
            this.btnCalculate.Location = new System.Drawing.Point(30, 155);
            this.btnCalculate.Name = "btnCalculate";
            this.btnCalculate.Size = new System.Drawing.Size(80, 24);
            this.btnCalculate.TabIndex = 32;
            this.btnCalculate.Text = "&Calculate";
            this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click);
            //
            // txtDepartureDate
            //
            this.txtDepartureDate.Location = new System.Drawing.Point(125, 37);
            this.txtDepartureDate.Name = "txtDepartureDate";
            this.txtDepartureDate.Size = new System.Drawing.Size(88, 20);
            this.txtDepartureDate.TabIndex = 29;
            //
            // txtArrivalDate
            //
            this.txtArrivalDate.Location = new System.Drawing.Point(125, 11);
            this.txtArrivalDate.Name = "txtArrivalDate";
            this.txtArrivalDate.Size = new System.Drawing.Size(88, 20);
            this.txtArrivalDate.TabIndex = 28;
            //
            // Label4
            //
            this.Label4.Location = new System.Drawing.Point(22, 90);
            this.Label4.Name = "Label4";
            this.Label4.Size = new System.Drawing.Size(96, 16);
            this.Label4.TabIndex = 27;
            this.Label4.Text = "Total price:";
            this.Label4.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            //
            // Label2
            //
            this.Label2.Location = new System.Drawing.Point(23, 38);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(96, 16);
            this.Label2.TabIndex = 25;
            this.Label2.Text = "Departure date:";
            this.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            //
            // Label1
            //
            this.Label1.Location = new System.Drawing.Point(23, 12);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(96, 16);
            this.Label1.TabIndex = 24;
            this.Label1.Text = "Arrival date:";
            this.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            //
            // txtNights
            //
            this.txtNights.Location = new System.Drawing.Point(124, 63);
            this.txtNights.Name = "txtNights";
            this.txtNights.ReadOnly = true;
            this.txtNights.Size = new System.Drawing.Size(88, 20);
            this.txtNights.TabIndex = 35;
            this.txtNights.TabStop = false;
            //
            // txtAvgPrice
            //
            this.txtAvgPrice.Location = new System.Drawing.Point(124, 115);
            this.txtAvgPrice.Name = "txtAvgPrice";
            this.txtAvgPrice.ReadOnly = true;
            this.txtAvgPrice.Size = new System.Drawing.Size(88, 20);
            this.txtAvgPrice.TabIndex = 36;
            this.txtAvgPrice.TabStop = false;
            //
            // txtTotalPrice
            //
            this.txtTotalPrice.Location = new System.Drawing.Point(124, 89);
            this.txtTotalPrice.Name = "txtTotalPrice";
            this.txtTotalPrice.ReadOnly = true;
            this.txtTotalPrice.Size = new System.Drawing.Size(89, 20);
            this.txtTotalPrice.TabIndex = 37;
            this.txtTotalPrice.TabStop = false;
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(16, 118);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(102, 13);
            this.label3.TabIndex = 38;
            this.label3.Text = "Avg. price per night:";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            //
            // frmReservations
            //
            this.AcceptButton = this.btnCalculate;
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.CancelButton = this.btnExit;
            this.ClientSize = new System.Drawing.Size(234, 197);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtTotalPrice);
            this.Controls.Add(this.txtAvgPrice);
            this.Controls.Add(this.txtNights);
            this.Controls.Add(this.Label5);
            this.Controls.Add(this.btnExit);
            this.Controls.Add(this.btnCalculate);
            this.Controls.Add(this.txtDepartureDate);
            this.Controls.Add(this.txtArrivalDate);
            this.Controls.Add(this.Label4);
            this.Controls.Add(this.Label2);
            this.Controls.Add(this.Label1);
            this.Name = "frmReservations";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Reservations";
            this.Load += new System.EventHandler(this.frmReservations_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        internal System.Windows.Forms.Label Label5;
        internal System.Windows.Forms.Button btnExit;
        internal System.Windows.Forms.Button btnCalculate;
        internal System.Windows.Forms.TextBox txtDepartureDate;
        internal System.Windows.Forms.TextBox txtArrivalDate;
        internal System.Windows.Forms.Label Label4;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Label Label1;
        private System.Windows.Forms.TextBox txtNights;
        private System.Windows.Forms.TextBox txtAvgPrice;
        private System.Windows.Forms.TextBox txtTotalPrice;
        private System.Windows.Forms.Label label3;
    }
}

Program.cs

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reservations
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmReservations());
        }
    }
}

frmReservations.cs

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
    {    
        DateTime currentDateTime = DateTime.Today;
        decimal pricePerNight = 0m;
        decimal totalPrice = 0m;
        decimal averagePricePerNight = 0m;
      
        public frmReservations()
        {
            InitializeComponent();
        }

        private void frmReservations_Load(object sender, EventArgs e)
        {
            txtArrivalDate.Text = currentDateTime.ToShortDateString();
            txtDepartureDate.Text = currentDateTime.AddDays(3).ToShortDateString();
        }

        public bool IsValidData()
        {
            return
                IsPresent(txtArrivalDate, "Arrival Date") &&
                IsDateTime(txtArrivalDate, "Arrival Date") &&
                IsWithinRange(txtArrivalDate, "Arrival Date", currentDateTime, currentDateTime.AddYears(5)) &&

                IsPresent(txtDepartureDate, "Departure Date") &&
                IsDateTime(txtDepartureDate, "Departure Date") &&
                IsWithinRange(txtDepartureDate, "Departure Date", currentDateTime, currentDateTime.AddYears(5)) &&

                IsSequential(txtArrivalDate, txtDepartureDate);
        }

        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)
        {
            DateTime dateTime;
            if (DateTime.TryParse(textBox.Text, out dateTime))
            {
                return true;
            }
            else
            {
                MessageBox.Show(name + " was entered incorrectly. Please try again.", "Entry Error");
                textBox.Focus();
                return false;
            }
        }

        public bool IsWithinRange(TextBox textBox, string name,
            DateTime min, DateTime max)
        {
            DateTime enteredDate = DateTime.Parse(textBox.Text);
            if (enteredDate < min || enteredDate > max)
            {
                MessageBox.Show(name + " must be between " + min + " and " + max + ".", "Entry Error");
                textBox.Focus();
                return false;
            }
            return true;
        }

        public bool IsSequential(TextBox arrival, TextBox departure)
        {
            if (DateTime.Parse(arrival.Text) > DateTime.Parse(departure.Text))
            {
                MessageBox.Show("The departure date should be after the arrival date.", "Entry Error");
                departure.Focus();
                return false;
            }
            return true;
        }     

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            totalPrice = 0m;
            averagePricePerNight = 0m;

            if (IsValidData())
            {
                DateTime arrivalDate = Convert.ToDateTime(txtArrivalDate.Text);
                DateTime departureDate = Convert.ToDateTime(txtDepartureDate.Text);

                TimeSpan timeSpan = departureDate.Subtract(arrivalDate);
                int numberOfDays = timeSpan.Days;

                DateTime currentDateCounter = arrivalDate;
                DayOfWeek dayOfWeek = currentDateCounter.DayOfWeek;

                int numberOfDaysCounter = numberOfDays;
                while (numberOfDaysCounter > 0)
                {
                    if (dayOfWeek == DayOfWeek.Friday || dayOfWeek == DayOfWeek.Saturday)
                    {
                        pricePerNight = 150m;
                    }
                    else
                    {
                        pricePerNight = 120m;
                    }
                    totalPrice += pricePerNight;

                    currentDateCounter = currentDateCounter.AddDays(1);
                    dayOfWeek = currentDateCounter.DayOfWeek;
                    numberOfDaysCounter--;
                }

                averagePricePerNight = totalPrice / numberOfDays;

                txtNights.Text = timeSpan.Days.ToString();
                txtTotalPrice.Text = totalPrice.ToString("c");
                txtAvgPrice.Text = averagePricePerNight.ToString("c");
            }   
        }

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote