• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1040
  • Last Modified:

Hotel Ratings

I need to finish the code for the following project:
I need to display a bar chart: very simple:
HOTEL 1: ****
HOTEL 2: ******
HOTEL 3: ***
HOTEL 4: ******
HOTEL 5: **
THE BAR CHART DEPICTS THE RATINGS FOR FIVE HOTELS.
The program allows the user to enter the hotel rating for each of five hotels. The rating can be from 1 through 6 only. If the user enters and invalid rating, the procedure should display an appropriate message, similar to the one listed here, and ask the user for the hotel rating again. When the user enters a number then this should be used to display the appropriate number of asterisks in the bar chart.
Name:         Hotel Project
' Purpose:      Display hotel ratings in a bar chart
' Programmer:   Vincent McPatrick on 12/15/2009

Public Class frmMain

    Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click
        ' create a chart depicting ratings for five hotels
        ' input will be made by user using 1 through 6 only

        ' declare variables

        Dim strRating As Integer

        'Dim lblHotel As Double

        ' clear message box
        lblStars.Text = String.Empty

        strRating = InputBox("Please enter your rating for this hotel:", "Hotel Rating")
       
        If strRating = 0 AndAlso strRating > 6 Then
            MessageBox.Show("Please enter a rating number between 1 and 6", "Hotel Rating", _
                                        MessageBoxButtons.OK, MessageBoxIcon.Information)
            ' start rating the hotels
            'For strClientRating As Integer = 1 To 6
            'lblStars.Text = lblStars.Text _
            '& "HOTEL 1:" & strClientRating.ToString & ControlChars.NewLine

            'Next strClientRating
            lblStars.Text = lblStars.Text & ControlChars.NewLine



        End If
    End Sub
End Class
0
vectormcp
Asked:
vectormcp
  • 3
  • 3
1 Solution
 
Arthur_WoodCommented:
your problem is in this line:

If strRating = 0 AndAlso strRating > 6 Then

cannot be both 0 and > 6 (AndAlso means that BOTH tests must be satisfied)

what you want is to be BOTH greater than 0 AndAlso Less than 6.

This looks suspiciously like a homework problem, so change the line

If strRating = 0 AndAlso strRating > 6 Then  

so that you test strRating to be BOTH Greater Than 0 AndAlso Less Than 6.

      Also, the name strRating is not the best choice for the name of an Integer variable - I first thought that you had declared it as a String.  Wouldn't intRating be a better choice?

AW
0
 
vectormcpAuthor Commented:
Indeed is a homework problem, I am just beginning to work with VB.
I can grasp the concepts, but I am still learning the ropes. I do not pretend to hide anything.
I have worked with other programs, and I think that this is by far the easiest, so probably that is the reason am having such a hard time, because I am expecting to be hard.
Thanks for your help.
0
 
Arthur_WoodCommented:
If you want to show up to 5 hotels at a single pass, then you will need to make use of an array (or a Collection, which is almost the same ting) of Hotel ratings , indexed by the hotel number (1-5).  You would aske the user for the hotel number (1-5) and thge rating (1-6), and after all of the hotels had been rated, show the ratings for the 5 hotels.

Do you grasp the concept that I have suggested?

AW
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
vectormcpAuthor Commented:
This is what I coded so far. It does work, but, I know it can be polished much better.
The concept is here.
Your explanation is very accurate, and I do grasp your idea. However I still need to make it into code.
Your suggestion is right on target about separating the two arrays. In fact I should not have to do it in a single pass. The idea that I have in my head is to use the FOR&NEXT statements, and make two different loops, one contained into the other. This is where I am stuck. Any suggestions on how to come up with the code? I know I am close, but is not as clean as I would like it to be. Any help would be certainly appreciate it.
Private Sub btnCreate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate.Click

        Dim i As Integer = 1
        lblRatings.Text = ""
        Do
            Dim intRating As Integer
            Dim sRating As String
            Dim bCorrect As Boolean = False
            Dim sError As String = ""

            Do While Not bCorrect
                sRating = InputBox(String.Format("Please enter your rating for hotel {0}", i), "Hotel Rating")
                bCorrect = Integer.TryParse(sRating, intRating) AndAlso intRating > 0 AndAlso intRating <= 6
                If (Not bCorrect) Then
                    MessageBox.Show("Incorrect Answer. Please check", "Incorrect Answer", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Loop

            lblRatings.Text &= _
                String.Format("Hotel {0}: {1}", i, New String("*"c, intRating)) & Environment.NewLine
            i += 1

        Loop While i <= 5

    End Sub

End Class
0
 
Arthur_WoodCommented:
an array is a structure declared as

Dim HotelRating(5) as Integer  


That reserves a block of 5 locations (indexed 1 through 5) to hold integers

The values are added to the array like this

     Dim Hotel as Integer = 1
     Dim strRating as String
     While Hotel <= 5
          strRating = InputBox(String.Format("Please enter your rating for hotel {0}",Hotel), "Hotel Rating")
          If Integer.TryParse(strRating, Rating) then
              If Rating >0 AndAlso Rating <= 6 then
                 HotelRating(Hotel) = Rating
                 Hotel += 1
              Else
                 MessageBox.Show("Rating must be a value between 1 and 6")
              End If
         Else
              MessageBox ("The Rating must be an Integer value, you entered a String")
         End If
     Loop

Now you have the hote_ratings in an array of 5 values, each of which is between 1 and 6

    Now you would loop throgh those 5 value to display the Hotell number, and the corresponding number of stars.

I will leave the rest of the code up to you, but you already have the guts to the idea.

AW
             
0
 
vectormcpAuthor Commented:
I have completed the coding on my own. I had to invest more time in this problem than expected originally. It was good help but not complete
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now