Hotel Ratings

Posted on 2009-12-16
Last Modified: 2012-05-08
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 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
    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
Question by:vectormcp
    LVL 44

    Expert Comment

    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?


    Author Comment

    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.
    LVL 44

    Expert Comment

    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?


    Author Comment

    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 = ""
                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

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

            Loop While i <= 5

        End Sub

    End Class
    LVL 44

    Accepted Solution

    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
                     MessageBox.Show("Rating must be a value between 1 and 6")
                  End If
                  MessageBox ("The Rating must be an Integer value, you entered a String")
             End If

    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.


    Author Closing Comment

    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

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Excel Visual Basic Code for Option Buttons 8 44
    vba find the last empty column 10 74
    Use Multiple Forms 4 30
    Adding to a VBA? 6 35
    Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now