Question How to create an ArrayList / List and check its values

samuel_1991

Member
Joined
Jan 19, 2009
Messages
13
Programming Experience
Beginner
Hi everyone, I need to do an assignment where it is about restaurant order taking. Now I have a problem that if I add a table number in new order (Taking Order.vb, known as Taking_Order as class) using its txttbNo (A textbox name), later if I do it again, the whole thing messed up.

For eg: Adding first customer of the day as 12 (Since assuming it seats on table 12)

Later, adding the second customer as 12 will give a prompt that the seat is occupied by another customer.

Now, by adding the second customer as anything other than 12 (Say 77 etc) , later, by adding third customer, the table 77 will not be blocked but still Table 12 will be blocked.

What it should be is both Table 12 and 77 be blocked. (And the 12 / 77 is something variable between 1 to 99 since my local area, at least has typically 99 tables per restaurant.) The prompt "Sorry, the table is currently occupied by the other customer." must be repeated until the respective table number's customer has paid for the meal fully in Payment.vb (Before that, it is also greeted by SelectTable.vb to select a valid Table Number (That follows by customer's order)


Here is my coding for the form
VB.NET:
Public Class Taking_Order
    Public Shared Table_Number As New List(Of Integer) 'Create List for Table ID

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim check = 0 'Check for occupied Table Number
        Dim proceed = 1 'Check if no errors occured. 0 = Cannot proceed
        Dim txttbNo = txttableNo.Text
        Dim i As Integer
        For Each txttbNo In Table_Number
            'Check for occupied Table Number
            If Table_Number.Item(i) = txttableNo.Text Then
                check = 1
            Else
                check = 0
            End If
        Next txttbNo
        If check = 1 Then
            MessageBox.Show("Sorry, the table is currently occupied by the other customer.")
            proceed = 0
        End If
        If txttableNo.Text = "" And check = 0 Then
            MessageBox.Show("Please enter a valid table number before we can process the order.")
            proceed = 0
        ElseIf txttableNo.Text > 99 And check = 0 Then
            MessageBox.Show("Sorry, the table was not found. Currently our restaurant has only 99 tables.")
            proceed = 0
        End If
        If proceed = 1 Then
            Table_Number.Add(txttableNo.Text)
            Dim Order As New Order
            Order.Show()
        End If
    End Sub

    Private Sub txttableNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttableNo.TextChanged
        If txttableNo.Text.Length > 0 Then
            Try
                Integer.Parse(txttableNo.Text)
            Catch ex As Exception
                MessageBox.Show("Table Number can only be integer.")
            End Try
        End If
        If txttableNo.Text = "0" Then
            MessageBox.Show("Table Number can only be integer of more than 0.")
        End If
        txttableNo.Text = txttableNo.Text
    End Sub
End Class


I do have other questions in other forms such as how to save each individual total price to be shown in Payment.vb etc.

You can access to my code though. (By PM, or I need many threads)
 
Last edited:
No comments in your code?

When youre learning to code, write comments in whatever language you think in, then translate the comments into code

Work out the algorithm in your native thoughts and then translate it.. If you try to think in vb.net now then you will find it very hard.
 
Codes with comments

VB.NET:
Public Class Taking_Order
    Public Shared Table_Number As New List(Of Integer)[COLOR="Lime"] 'Create List for Table ID[/COLOR]
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
[COLOR="lime"]'Button1 is a button that click to Order.vb, where it shows various dishes offered by the restaurant.[/COLOR]

        Dim check = 0[COLOR="lime"] 'Check for occupied Table Number[/COLOR]
        Dim proceed = 1 [COLOR="lime"]'Check if no errors occured. 0 = Cannot proceed. I assume there is no errors first.[/COLOR]        
Dim txttbNo = txttableNo.Text [COLOR="lime"]'Create an variable that equates to a textbox value that declares a new customer seat on a table[/COLOR]        
Dim i As Integer
        For Each txttbNo In Table_Number[COLOR="lime"] 'Is that the way to create a for loop
            'Check for occupied Table Number[/COLOR]            
If Table_Number.Item(i) = txttableNo.Text Then
                check = 1 [COLOR="lime"]'I want to specify that if an item in the list Table_Number is same as txttableNo.Text, then it is checked that the customer is seating on the table and therefore cannot continue[/COLOR]            
Else
        check = 0 [COLOR="Lime"]'As opposed to check = 1[/COLOR]            

End If
        Next txttbNo [COLOR="lime"]'Is that the way to go forward to next txttbNo[/COLOR]       

If check = 1 Then [COLOR="lime"]'If there is error on this customer seatting, then cannot proceed.[/COLOR]            
MessageBox.Show("Sorry, the table is currently occupied by the other customer.")
            proceed = 0
        End If

[COLOR="lime"]'Apart of checking against currently seat by another customer, I also need to check if that table number is valid or not. Assuming the restaurant table number has range of 1 to 99.[/COLOR]        
If txttableNo.Text = "" And check = 0 Then
            MessageBox.Show("Please enter a valid table number before we can process the order.")
            proceed = 0
        ElseIf txttableNo.Text > 99 And check = 0 Then
            MessageBox.Show("Sorry, the table was not found. Currently our restaurant has only 99 tables.")
            proceed = 0
        End If

[COLOR="lime"]'Only if proceed = 1, then allow taking of new order (And also check = 0)[/COLOR]
        If proceed = 1 Then
            Table_Number.Add(txttableNo.Text)
            Dim Order As New Order
            Order.Show()
        End If
    End Sub

    Private Sub txttableNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txttableNo.TextChanged
        If txttableNo.Text.Length > 0 Then [COLOR="lime"]'If entered an alphabet, special characters like !@#$....., Table Number cannot allow this[/COLOR]            
Try
             Integer.Parse(txttableNo.Text)
            Catch ex As Exception
                MessageBox.Show("Table Number can only be integer.")
            End Try
        End If
        If txttableNo.Text = "0" Then
            MessageBox.Show("Table Number can only be integer of more than 0.")
        End If
        txttableNo.Text = txttableNo.Text
    End Sub
End Class
 
Last edited:
Small extract & modifications

However, if I made an modification on here
VB.NET:
Dim txttbNo = txttableNo.Text 'Create an variable that equates to a textbox value that declares a new customer seat on a table        
[COLOR="Lime"]'Dim i As Integer //I removed this line, I just highlight for easier viewability[/COLOR]
        For Each txttbNo In Table_Number 'Is that the way to create a for loop
            'Check for occupied Table Number            
If [COLOR="lime"]Table_Number.Item(i)[/COLOR] [I]txttbNo[/I] = txttableNo.Text Then
[COLOR="lime"]I modified the Table_Number.Item(i) to txttbNo[/COLOR]
                check = 1 'I want to specify that if an item in the list Table_Number is same as txttableNo.Text, then it is checked that the customer is seating on the table and therefore cannot continue            
Else
        check = 0 'As opposed to check = 1            

End If
        Next txttbNo 'Is that the way to go forward to next txttbNo

Then what I got is if first customer takes on seat 23, second customer takes on seat 24, If there is input error, input as 23 for second customer, there will be a box showing it is been occupied. However, if say the third customer takes on seat 22, and input as 24, error message still show, but not with 23. (But 23 customer has not made payment yet, so he / she is still enjoying the meal.)
 
you know, VB.NET does have boolean types:

Dim shouldProceed as Boolean = True
Dim errorsOccurred as Boolean = False


I didn't want you to comment your code, i wanted you to write in high level english language the LOGIC of how your program will work.

If youre confused, switch off the computer, get out a pen and paper and describe in abstract English terms how the algorithm of checking table occupation will work
Now copy these words into comments
Now write code based on them


"Weeks of coding can save you hours of planning"
 
you know, VB.NET does have boolean types:

Dim shouldProceed as Boolean = True
Dim errorsOccurred as Boolean = False


I didn't want you to comment your code, i wanted you to write in high level english language the LOGIC of how your program will work.

If youre confused, switch off the computer, get out a pen and paper and describe in abstract English terms how the algorithm of checking table occupation will work
Now copy these words into comments
Now write code based on them


"Weeks of coding can save you hours of planning"
I see, now I will go and think about it and post it back here.
 
Sorry for my late reply, it is a highly complicated program with tons of feature.

It works as follows:
1.) Login to the system (If staff name & password match then proceed)
2.) Go to either Taking New Order or Recommendation (Includes System recommendation on various dishes for different ppl.) or Administrative (Change Password / Consolidate feedback / Orders)
3a.1) If goes to taking new order, it will prompt for a table number (Specify where the customer is located) -- Able to differenate if table number is occupied by another customer or not. (If occupied by another customer, it will not proceed)
3a.2) Now, it will show 4 tabs (4 categories of food -- Starters, Main Dish, Dessert and Drinks) with textbox (To enter quantity) and checkbox (To select an order). Now it is possible to return to home page (Did not take order)
3a.3) Goes to confirm order. (Still possible to return to 3a.2 so as to continue taking order or return to home page -- given up on order taking)
3a.4) returns to home page. Taking a new order procedure is finished.
3b.1) If goes to recommendation, it shows inhouse promotions, system recommendation, popular dish and chef recommendation. (All of the details except system recommendation is not part of Section 3. I will explain them later.)
3b.2) Select number of customer (1 to 10) and select various preferences (Want Starters? Dessert? or Salty Main dishes??) as well as allergic conditions (Alcohol, or Egg or something else??)
3b.3) Refer to 3a.1 for the rest reference (Except 3a.2 with 4 tabs for selecting food. This step is redundant here.)
4) Inhouse promotions, Popular dish and Chef recommendation are features to show current recommendations. Does nothing much.
5) Modify Order (An option in Home page) --- Only if existing order is available for modification (Only at least one order that has not been paid in either part or whole), it will show which table number to modify. Else, a message box will say this option is not available now.
5.1) Refer to 3.a.2 for reference. The only difference is that on that page, it has a delete order option available. (Not available in 3.a.2 as I have implemented a totally similar forms, one called Order.vb and the other called MOrder.vb)
6) Administrative has 3 sub functions. Change password , View Feedback and View Orders of the day.
6.1) The logic of View Feedback and View Order are quite similar. They can show a undefined number of feedbacks, paid orders using next / prev buttons.
6.2) Change Password whereas will match the current logged on staff with new password. (It must be between 8 to 17 characters and new password must be entered twice -- with 2 textbox.)
7) Payment (Another option in Home page). Shows which table number to paid. Then enters various information like card type (Masters? Visa? etc), card number, name, card expiry and how much wanted to pay using the card (Some ppl may have their card exceed the limit, therefore I implement the ability to pay partially.) . Of course, the program do show how much is the grandtotal and updates it only when the payment is successful (Be it a partial or full payment).
8) Lock. This function is self explanatory. Click the lock button in home page and it is locked until the password for the current logged on staff is entered which then returns to home page again.
 
Anyway, I have all my functions fully functional and left only 1 small thing. During view feedback / view order of the day (Shows only when a partial or full payment is made.), i would prefer to have the whole form printed out. How to do this?

This is a PDA application.
Regarding my previous question, I have found a way to do:
Using
VB.NET:
For i As Integer = 0I To Table_Number.Count - 1I
means the variable i range from 0 to this list's size / length
then use
VB.NET:
 if textbox1.text = Table_Number.Item(i) then
means if user input's in a textbox (Asssuming it is named as textbox1) equals to Table_Number i's object, then do whatever I want.

And the full sample is
VB.NET:
For i As Integer = 0I To Table_Number.Count - 1I
if textbox1.text = Table_Number.Item(i) then
[COLOR="Lime"]'Do whatever I want[/COLOR] 
End If
Next i
 
Last edited:

Latest posts

Back
Top