loop structure,please help

Joannaono

New member
Joined
Oct 30, 2007
Messages
4
Programming Experience
Beginner
Hi everyone,

My name is Joanna and I am a woman learning VB.net.And it hurts …
badly. Please help me to sort out this problem:




• Write a solution using Structured English and
• Using the Structured English develop a Visual Basic.Net program to solve it.:
• The program must include a decision statement and a loop structure;


for the following:



The student canteen runs a discount scheme for meals allowing a student to obtain 10 meals a week from the breakfast, lunch and teas it serves Mon – Fri. In any one day only two meals may be purchased i.e one day a student could have a breakfast and lunch but not a tea but another day they may decide to have a lunch and a tea but no breakfast. Write a program to obtain the meal plans of a student for one week. It should display error messages if the rules of the discount scheme are broken (e.g. more than 10 meals a week or more than 2 a day) but also indicates if a student is missing some meals e.g. only 9 taken in the week..
 
i can't upload a zip file(i'm using vb2005), but heres the code. see the attached screenshot

VB.NET:
Public Class form1

    Dim btnArray(14) As Control
    Dim mealCount As Integer = 0

    Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click, btn10.Click, btn11.Click, btn12.Click, btn13.Click, btn14.Click, btn15.Click

        Dim ctrl As Control = CType(sender, Control)
        Dim btnNumber As Integer = CType(ctrl.Name.Replace("btn", ""), Integer)

        Select Case btnNumber
            Case 1, 4, 7, 10, 13
                selectMeal(ctrl, btnNumber, btnNumber + 1)
            Case 2, 5, 8, 11, 14
                selectMeal(ctrl, btnNumber, btnNumber - 2)
            Case 3, 6, 9, 12, 15
                selectMeal(ctrl, btnNumber - 3, btnNumber - 2)
        End Select

    End Sub


    Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        btnArray(0) = btn1
        btnArray(1) = btn2
        btnArray(2) = btn3
        btnArray(3) = btn4
        btnArray(4) = btn5
        btnArray(5) = btn6
        btnArray(6) = btn7
        btnArray(7) = btn8
        btnArray(8) = btn9
        btnArray(9) = btn10
        btnArray(10) = btn11
        btnArray(11) = btn12
        btnArray(12) = btn13
        btnArray(13) = btn14
        btnArray(14) = btn15

        For x As Integer = 0 To 14
            btnArray(x).Font = New Font("Wingdings", btnArray(x).Font.Size, FontStyle.Bold)
        Next

    End Sub

    Private Sub btnSummary_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSummary.Click
        Dim strMsg As String = ""

        For x As Integer = 0 To 12 Step 3

            Select Case x
                Case 0
                    If btnArray(x).Text <> "" Or btnArray(x + 1).Text <> "" Or btnArray(x + 2).Text <> "" Then
                        strMsg += "Monday:- " & whatMeals(x)
                    End If
                Case 3
                    If btnArray(x).Text <> "" Or btnArray(x + 1).Text <> "" Or btnArray(x + 2).Text <> "" Then
                        strMsg += "Tuesday:- " & whatMeals(x)
                    End If
                Case 6
                    If btnArray(x).Text <> "" Or btnArray(x + 1).Text <> "" Or btnArray(x + 2).Text <> "" Then
                        strMsg += "Wednesday:- " & whatMeals(x)
                    End If
                Case 9
                    If btnArray(x).Text <> "" Or btnArray(x + 1).Text <> "" Or btnArray(x + 2).Text <> "" Then
                        strMsg += "Thursday:- " & whatMeals(x)
                    End If
                Case 12
                    If btnArray(x).Text <> "" Or btnArray(x + 1).Text <> "" Or btnArray(x + 2).Text <> "" Then
                        strMsg += "Friday:- " & whatMeals(x)
                    End If
            End Select

        Next

        strMsg += ControlChars.CrLf
        strMsg += mealCount.ToString & " meals chosen this week."
        MessageBox.Show(strMsg, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)

        mealCount = 0
        lblMealCount.Text = ""

    End Sub

    Private Function whatMeals(ByVal x As Integer) As String
        Dim strLine As String = ""

        If btnArray(x).Text <> "" Then
            strLine += "breakfast, "
        End If
        If btnArray(x + 1).Text <> "" Then
            strLine += "lunch, "
        End If
        If btnArray(x + 2).Text <> "" Then
            strLine += "tea, "
        End If
        strLine = Strings.Left(strLine.Trim, strLine.Trim.Length - 1) & " selected."
        strLine += ControlChars.CrLf

        btnArray(x).Text = ""
        btnArray(x + 1).Text = ""
        btnArray(x + 2).Text = ""

        Return strLine

    End Function

    Private Sub selectMeal(ByVal c As Control, ByVal number1 As Integer, ByVal number2 As Integer)

        If c.Text = "" Then
            If btnArray(number1).Text = "" Or btnArray(number2).Text = "" Then
                c.Text = "ü"
                mealCount += 1
                lblMealCount.Text = mealCount.ToString & " meals chosen"
            ElseIf btnArray(number1).Text <> "" And btnArray(number2).Text <> "" Then
                If mealCount < 10 Then
                    MessageBox.Show("You're allowed 2 meals maximum per day", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
                Else
                    MessageBox.Show("You're allowed 2 meals maximum per day and" & ControlChars.CrLf & "10 meals maximum per week", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
                End If
            End If
        Else
            c.Text = ""
            mealCount -= 1
            lblMealCount.Text = mealCount.ToString & " meals chosen"
        End If

    End Sub

End Class
 

Attachments

  • screenshot.JPG
    screenshot.JPG
    9 KB · Views: 31
Last edited:
Paul, thank you so much.You are fantastic..Just a quick question...I don't Know what btnArray exactly means......Thank you, again.
 
Back
Top