none
A reservation form with modifications RRS feed

  • Question

  • I have written a program, that works, that lets the user input an Arrival Date and a Departure Date. It then calculates number of nights, total cost ($160 per night), and Average cost per night. I want to modify the code in the Load event handler so that Current date is the default for arrival date and 3 days later is the default for the departure date. Then I want to change the cost of Friday and Saturday nights to $180. With the new cost the program will be more efficient to calculate total cost and better for average cost per night. I would like to use a Do While Loop for the calculation. I think this would be easiest (unless someone has a better suggestion. I am lost trying to figure out the day of the week for the time span so that I can apply the cost for each night. Also the default values in the load handler have me a little confused. There is a number of ways to do this but I can't  seem to get it to work properly.
    Public Class Form1
    
        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
            Dim arrivalDate As Date = CDate(txtArrivalDate.Text)
            Dim departureDate As Date = CDate(txtDepartureDate.Text)
            Dim timeOfTrip As TimeSpan = departureDate.Subtract(arrivalDate)
            Dim numberOfNights As String = (txtNights.Text)
            Dim totalPrice As String = (txtTotalPrice.Text)
            Dim avgPricePerNight As String = (txtAvgPrice.Text)
    
            numberOfNights = CType((timeOfTrip.Days), String)
            totalPrice = CType(CDec(CDbl(numberOfNights) * 160.0), String)
            avgPricePerNight = CType(CDec(totalPrice) / CDec(numberOfNights), String)
    
            txtNights.Text = numberOfNights
            txtTotalPrice.Text = totalPrice
            txtAvgPrice.Text = avgPricePerNight
    
        End Sub
    
        Public Function IsValidData() As Boolean
    
        End Function
    
        Public Function IsPresent(textbox As TextBox, name As String) _
                As Boolean
            If textbox.Text = "" Then
                MessageBox.Show(name & " is a required field.", "Entry Error")
                textbox.Select()
                Return False
            Else
                Return True
            End If
        End Function
    
        Public Function IsDateTime(textbox As TextBox, name As String) _
                As Boolean
    
        End Function
    
        Public Function IsWithinRange(textbox As TextBox, name As String,
                ByVal min As Date, ByVal max As Date) As Boolean
    
        End Function
    
        Private Sub btnExit_Click(sender As Object,
            e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    End Class

    Thursday, October 25, 2018 8:42 PM

Answers

  • Hi

    Here is some code - again, using 2 DateTimePickers. This example calculates the total charge based on the nightly rate.

    Image

    Code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim charges As New Dictionary(Of Integer, Decimal)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' set up day charges (Sunday = 0)
        charges.Add(0, 160) ' Sunday
        charges.Add(1, 160) ' Monday
        charges.Add(2, 160) ' Tuesday
        charges.Add(3, 160) ' Wednesday
        charges.Add(4, 160) ' Thursday
        charges.Add(5, 180) ' Friday
        charges.Add(6, 180) ' Saturday
    
        With DateTimePicker1
          'set the display format for DateTimePicker1
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "ddd, dd MMM yyyy"
          .MinDate = Now.Date
          ' set the initial DateTimePicker values
          .Value = Now.Date
        End With
        With DateTimePicker2
          'set the display format for DateTimePicker2
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "ddd, dd MMM yyyy"
          .MinDate = Now.Date
    
          ' set the initial DateTimePicker values
          .Value = Now.Date.AddDays(3)
        End With
      End Sub
      Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged, DateTimePicker1.ValueChanged
        Dim v As Integer = DateTimePicker2.Value.Subtract(DateTimePicker1.Value).Days
    
        Label3.Text = v.ToString
        Label5.Text = GetCharges(DateTimePicker1.Value, DateTimePicker2.Value).ToString("#.00")
      End Sub
      Function GetCharges(d1 As Date, d2 As Date) As Decimal
        Dim charge As Decimal = 0D
        While d2 > d1
          charge += charges(d1.DayOfWeek)
          d1 = d1.AddDays(1)
        End While
        Return charge
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Prodson101 Friday, October 26, 2018 12:30 AM
    Thursday, October 25, 2018 9:45 PM
  • Hi

    A couple of things, both very important.

    When dealing with calculations  of any sort, where everything is expressed numerically, Strings MUST be converted to an appropriate numeric value. For example, your line:

    avgPricePerNight = charge / numberOfNights

    you are trying to perform a mathematical division on two String values - this will fail.(your exception)

    To enable that calculation, you need to convert each (Charge and numberOfNights) to numeric types, and convert the result back into a String to assign it to the avgPricePerNight variable. Here is an emended version of your code to show a little of that conversion being done.

    Another point: you have copied the appropriate code for the GetCharges function, but, you didn't actually use the function.

    Anyway, try this out (note I have ommited some empty subs etc here)

    Image

    Code

    ' ALWAYS HAVE THESE OPTIONS
    Option Strict On
    Option Explicit On
    
    Public Class Form1
      Dim charges As New Dictionary(Of Integer, Decimal)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        charges.Add(0, 160)
        charges.Add(1, 160)
        charges.Add(2, 160)
        charges.Add(3, 160)
        charges.Add(4, 160)
        charges.Add(5, 180)
        charges.Add(6, 180)
    
        txtArrivalDate.Text = Now.ToShortDateString
        txtDepartureDate.Text = Now.AddDays(3).ToShortDateString
      End Sub
      Private Sub btnCalculate_Click_1(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
        Dim arrivalDate As Date = CDate(txtArrivalDate.Text)
        Dim departureDate As Date = CDate(txtDepartureDate.Text)
        Dim timeOfTrip As TimeSpan = departureDate.Subtract(arrivalDate)
        Dim numberOfNights As Integer = timeOfTrip.Days
        Dim charge As Decimal = GetCharges(arrivalDate, departureDate)
        Dim avgPricePerNight As Decimal = (charge / numberOfNights)
    
    
        'have used $ here - adjust as needed
        txtNights.Text = numberOfNights.ToString
        txtTotalPrice.Text = charge.ToString("$#.00")
        txtAvgPrice.Text = avgPricePerNight.ToString("$#.00")
    
      End Sub
    
      Function GetCharges(d1 As Date, d2 As Date) As Decimal
        Dim charge As Decimal = 0D
        While d2 > d1
          charge += charges(d1.DayOfWeek)
          d1 = d1.AddDays(1)
        End While
        Return charge
      End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, October 26, 2018 2:07 PM
    • Marked as answer by Prodson101 Saturday, October 27, 2018 12:00 AM
    Friday, October 26, 2018 2:06 PM

All replies

  • Where is your data coming from???

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, October 25, 2018 9:41 PM
    Moderator
  • Hi

    Here is some code - again, using 2 DateTimePickers. This example calculates the total charge based on the nightly rate.

    Image

    Code

    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim charges As New Dictionary(Of Integer, Decimal)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' set up day charges (Sunday = 0)
        charges.Add(0, 160) ' Sunday
        charges.Add(1, 160) ' Monday
        charges.Add(2, 160) ' Tuesday
        charges.Add(3, 160) ' Wednesday
        charges.Add(4, 160) ' Thursday
        charges.Add(5, 180) ' Friday
        charges.Add(6, 180) ' Saturday
    
        With DateTimePicker1
          'set the display format for DateTimePicker1
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "ddd, dd MMM yyyy"
          .MinDate = Now.Date
          ' set the initial DateTimePicker values
          .Value = Now.Date
        End With
        With DateTimePicker2
          'set the display format for DateTimePicker2
          .Format = DateTimePickerFormat.Custom
          .CustomFormat = "ddd, dd MMM yyyy"
          .MinDate = Now.Date
    
          ' set the initial DateTimePicker values
          .Value = Now.Date.AddDays(3)
        End With
      End Sub
      Private Sub DateTimePicker1_ValueChanged(sender As Object, e As EventArgs) Handles DateTimePicker2.ValueChanged, DateTimePicker1.ValueChanged
        Dim v As Integer = DateTimePicker2.Value.Subtract(DateTimePicker1.Value).Days
    
        Label3.Text = v.ToString
        Label5.Text = GetCharges(DateTimePicker1.Value, DateTimePicker2.Value).ToString("#.00")
      End Sub
      Function GetCharges(d1 As Date, d2 As Date) As Decimal
        Dim charge As Decimal = 0D
        While d2 > d1
          charge += charges(d1.DayOfWeek)
          d1 = d1.AddDays(1)
        End While
        Return charge
      End Function
    End Class


    Regards Les, Livingston, Scotland

    • Marked as answer by Prodson101 Friday, October 26, 2018 12:30 AM
    Thursday, October 25, 2018 9:45 PM
  • Is there any way to do this without using the DateTimePickers? I was hoping to accomplish what I want just with calculations and loop.
    Friday, October 26, 2018 12:35 AM
  • Hi

    If you look at the code I posted, the Function GetCharges seems to do what you are asking for - doesn't it? Of course, that function needs the Charges Dictionary set up as shown.

    It just needs two Dates passed in and will return the total cost accordingly.


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, October 26, 2018 1:03 AM
    Friday, October 26, 2018 12:59 AM
  • Will try it. Thanks
    Friday, October 26, 2018 1:28 AM
  • Most of it looks good. I am having a problem with the build because of line 17, the 

    avgPricePerNight = charge / numberOfNights 

    line. There is a conversion problem.

    Public Class Form1
    
        Dim charges As New Dictionary(Of Integer, Decimal)
    
        Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
            Dim arrivalDate As Date = CDate(txtArrivalDate.Text)
            Dim departureDate As Date = CDate(txtDepartureDate.Text)
            Dim timeOfTrip As TimeSpan = departureDate.Subtract(arrivalDate)
            Dim numberOfNights As String = (txtNights.Text)
            Dim charge As String = (txtTotalPrice.Text)
            Dim avgPricePerNight As String = (txtAvgPrice.Text)
    
            numberOfNights = CType((timeOfTrip.Days), String)
            avgPricePerNight = charge / numberOfNights
    
            charges.Add(0, 160)
            charges.Add(1, 160)
            charges.Add(2, 160)
            charges.Add(3, 160)
            charges.Add(4, 160)
            charges.Add(5, 180)
            charges.Add(6, 180)
    
            txtNights.Text = numberOfNights
            txtTotalPrice.Text = charge
            txtAvgPrice.Text = avgPricePerNight
    
        End Sub
    
        Function GetCharges(d1 As Date, d2 As Date) As Decimal
            Dim charge As Decimal = 0D
            While d2 > d1
                charge += charges(d1.DayOfWeek)
                d1 = d1.AddDays(1)
            End While
            Return charge
        End Function
    
        Public Function IsValidData() As Boolean
    
        End Function
    
        Public Function IsPresent(textbox As TextBox, name As String) _
                As Boolean
            If textbox.Text = "" Then
                MessageBox.Show(name & " is a required field.", "Entry Error")
                textbox.Select()
                Return False
            Else
                Return True
            End If
        End Function
    
        Public Function IsDateTime(textbox As TextBox, name As String) _
                As Boolean
    
        End Function
    
        Public Function IsWithinRange(textbox As TextBox, name As String,
                ByVal min As Date, ByVal max As Date) As Boolean
    
        End Function
    
        Private Sub btnExit_Click(sender As Object,
            e As EventArgs) Handles btnExit.Click
            Me.Close()
        End Sub
    
    
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
    
    End Class

    Friday, October 26, 2018 11:59 AM
  • Hi

    A couple of things, both very important.

    When dealing with calculations  of any sort, where everything is expressed numerically, Strings MUST be converted to an appropriate numeric value. For example, your line:

    avgPricePerNight = charge / numberOfNights

    you are trying to perform a mathematical division on two String values - this will fail.(your exception)

    To enable that calculation, you need to convert each (Charge and numberOfNights) to numeric types, and convert the result back into a String to assign it to the avgPricePerNight variable. Here is an emended version of your code to show a little of that conversion being done.

    Another point: you have copied the appropriate code for the GetCharges function, but, you didn't actually use the function.

    Anyway, try this out (note I have ommited some empty subs etc here)

    Image

    Code

    ' ALWAYS HAVE THESE OPTIONS
    Option Strict On
    Option Explicit On
    
    Public Class Form1
      Dim charges As New Dictionary(Of Integer, Decimal)
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        charges.Add(0, 160)
        charges.Add(1, 160)
        charges.Add(2, 160)
        charges.Add(3, 160)
        charges.Add(4, 160)
        charges.Add(5, 180)
        charges.Add(6, 180)
    
        txtArrivalDate.Text = Now.ToShortDateString
        txtDepartureDate.Text = Now.AddDays(3).ToShortDateString
      End Sub
      Private Sub btnCalculate_Click_1(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
        Dim arrivalDate As Date = CDate(txtArrivalDate.Text)
        Dim departureDate As Date = CDate(txtDepartureDate.Text)
        Dim timeOfTrip As TimeSpan = departureDate.Subtract(arrivalDate)
        Dim numberOfNights As Integer = timeOfTrip.Days
        Dim charge As Decimal = GetCharges(arrivalDate, departureDate)
        Dim avgPricePerNight As Decimal = (charge / numberOfNights)
    
    
        'have used $ here - adjust as needed
        txtNights.Text = numberOfNights.ToString
        txtTotalPrice.Text = charge.ToString("$#.00")
        txtAvgPrice.Text = avgPricePerNight.ToString("$#.00")
    
      End Sub
    
      Function GetCharges(d1 As Date, d2 As Date) As Decimal
        Dim charge As Decimal = 0D
        While d2 > d1
          charge += charges(d1.DayOfWeek)
          d1 = d1.AddDays(1)
        End While
        Return charge
      End Function
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, October 26, 2018 2:07 PM
    • Marked as answer by Prodson101 Saturday, October 27, 2018 12:00 AM
    Friday, October 26, 2018 2:06 PM
  • GOT IT!! Thank You so much. I have been pulling my hair out with this. I thought it would be a lot easier. I think the book I am using to teach myself isn't very good. Any suggestions? Thanks again.
    Saturday, October 27, 2018 12:02 AM
  • GOT IT!! Thank You so much. I have been pulling my hair out with this. I thought it would be a lot easier. I think the book I am using to teach myself isn't very good. Any suggestions? Thanks again.

    Hi

    Maybe use the book to prop up a table leg :)


    Regards Les, Livingston, Scotland

    Saturday, October 27, 2018 12:06 AM