I am creating a seat booking system in visual basic for coursework and it has to allow users to book seats from rows A to E. For row B, I have set it so that there are no seats remaining, however the message simply tells the user that there are not enough seats and that the maximum available seats is 0. I need the code to tell users that there are no seats remaining. My code is as follows:
Public Class Form1
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rowNumber As Integer
Dim SeatsData As String(,) = {{"booked", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10"},
{"booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked"},
{"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"},
{"D1", "booked", "booked", "D4", "D5", "D6", "D7", "D8", "D9", "D10"},
{"E1", "E2", "booked", "booked", "booked", "E6", "E7", "E8", "E9", "E10"}}
If ListBox2.Text = "A" Then
rowNumber = 0
ElseIf ListBox2.Text = "B" Then
rowNumber = 1
ElseIf ListBox2.Text = "C" Then
rowNumber = 2
ElseIf ListBox2.Text = "D" Then
rowNumber = 3
ElseIf ListBox2.Text = "E" Then
rowNumber = 4
End If
FindSeats(rowNumber, ListBox1.Text, SeatsData)
End Sub
Function FindSeats(ByVal RowNumber As Integer, ByVal NumSeats As Integer, SeatsData As Array) As String
Dim i As Integer = 0
Dim arrayPos As Integer = 0
Dim maxSeats As Integer = 0
Dim FirstSeat As String = 0
Dim LastSeat As String = 0
Dim Seatsfound As Boolean = False
Dim returnMsg As String = ""
Do While Seatsfound = False
Dim seatChar As String = SeatsData(RowNumber, arrayPos)
arrayPos = arrayPos + 1
If seatChar = "booked" Then
i = 0
Else
If i = 0 Then
FirstSeat = seatChar
End If
i = i + 1
If i > maxSeats Then
maxSeats = i
End If
End If
If i = NumSeats Then
LastSeat = seatChar
If FirstSeat = LastSeat Then
returnMsg = "Found seat: " + FirstSeat
Else
returnMsg = "Found seats: " + FirstSeat + " - " + LastSeat
End If
MsgBox(returnMsg)
Label3.Text = returnMsg
Seatsfound = True
Exit Do
End If
If arrayPos = 10 Then
returnMsg = "Not enough available seats, maximum available seats: " + CStr(maxSeats)
MsgBox(returnMsg)
Label3.Text = returnMsg
Exit Do
End If
Loop
End Function
End Class
How can I set my code so that it tells the user "there are no remaining seats in this row" or something?
Without using a custom class to design your problem domain, a simple approach is using a Dictionary
For example
Sub Main
Dim seats = new Dictionary(Of string, List(Of String))()
seats.Add("A", new List(Of String) FROM {"A1", "A2", "A3"})
seats.Add("B", new List(Of String) FROM {"booked", "booked", "booked"})
seats.Add("C", new List(Of String) FROM {"C1", "C2", "booked"})
seats.Add("D", new List(Of String) FROM {"booked", "booked", "D3"})
Dim result = FindSeat(seats, "D")
Console.WriteLine(result) ' Output = D3
result = FindSeat(seats, "B")
Console.WriteLine(result) ' Output = "No free seat on that line"
End Sub
Function FindSeat(seats as Dictionary(Of String, List(Of String)), line as String) As String
if seats.ContainsKey(line) Then
dim seatLine = seats(line)
Dim placeFree = seatLine.FirstOrDefault(Function (x) x <> "booked")
if placeFree Is Nothing then
return "No free seat on that line"
else
return placeFree
End If
Else
return "Not a valid line letter"
End If
End Function
In Main, I have statically declared a Dictionary that contains as Key the line letter while a List(Of String) is used as value to declare the seats names and their current state.
Now to find a free seat I get the List(Of String) corresponding to the Line letter and the apply a simple FirstOrDefault to get the first seat not booked
Related
I am writing an IDE an while working on syntax highlighting, i've encountered a very annoying issue.
When I type something, the text flickers...
Here is my code:
Dim KeyWords As List(Of String) = New List(Of String)(New String() {"void", "int", "long", "char", "short", "unsigned", "signed", "#include", "#define", "return"})
Dim KeyWordsColors As List(Of Color) = New List(Of Color)(New Color() {Color.Purple, Color.Purple, Color.Purple, Color.Purple, Color.Purple, Color.Purple, Color.Purple, Color.Olive, Color.Olive, Color.Blue})
Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
Dim words As IEnumerable(Of String) = RichTextBox1.Text.Split(New Char() {" "c, ".", ",", "?", "!", "(", Chr(13), Chr(10), " "})
Dim index As Integer = 0
Dim rtb As RichTextBox = sender 'to give normal color according to the base fore color
For Each word As String In words
'If the list contains the word, then color it specially. Else, color it normally
'Edit: Trim() is added such that it may guarantee the empty space after word does not cause error
coloringRTB(sender, index, word.Length, If(KeyWords.Contains(word.ToLower().Trim()) Or KeyWords.Contains("<"), KeyWordsColors(KeyWords.IndexOf(word.ToLower().Trim())), rtb.ForeColor))
index = index + word.Length + 1 '1 is for the whitespace, though Trimmed, original word.Length is still used to advance
Next
Dim strings() As String = RichTextBox1.Text.Split(Chr(34))
Dim count As Integer = 0
Dim cpart As Integer = 0
For Each part In strings
cpart = cpart + 1
If cpart Mod 2 = 0 Then
coloringRTB(RichTextBox1, count - 1, part.Length + 2, Color.Olive)
End If
count = count + part.Length + 1
Next
Dim strings2() As String = RichTextBox1.Text.Split(New Char() {"<", ">"})
count = 0
cpart = 0
For Each part In strings2
cpart = cpart + 1
If cpart Mod 2 = 0 Then
coloringRTB(RichTextBox1, count - 1, part.Length + 2, Color.Olive)
End If
count = count + part.Length + 1
Next
End Sub
Private Sub coloringRTB(rtb As RichTextBox, index As Integer, length As Integer, color As Color)
Dim selectionStartSave As Integer = rtb.SelectionStart 'to return this back to its original position
rtb.SelectionStart = index
rtb.SelectionLength = length
rtb.SelectionColor = color
rtb.SelectionLength = 0
rtb.SelectionStart = selectionStartSave
rtb.SelectionColor = rtb.ForeColor 'return back to the original color
End Sub
Private Sub RichTextBox1_KeyUp(sender As Object, e As KeyPressEventArgs) Handles RichTextBox1.KeyPress
If e.KeyChar = "{"c Then
RichTextBox1.SelectedText = "{}"
RichTextBox1.SelectionStart = RichTextBox1.Text.Substring(0, RichTextBox1.SelectionStart).LastIndexOf("{") + 1
e.Handled = True
End If
If e.KeyChar = "("c Then
RichTextBox1.SelectedText = "()"
RichTextBox1.SelectionStart = RichTextBox1.Text.Substring(0, RichTextBox1.SelectionStart).LastIndexOf("(") + 1
e.Handled = True
End If
If e.KeyChar = "["c Then
RichTextBox1.SelectedText = "[]"
RichTextBox1.SelectionStart = RichTextBox1.Text.Substring(0, RichTextBox1.SelectionStart).LastIndexOf("[") + 1
e.Handled = True
End If
If e.KeyChar = "'"c Then
RichTextBox1.SelectedText = "''"
RichTextBox1.SelectionStart = RichTextBox1.Text.Substring(0, RichTextBox1.SelectionStart).LastIndexOf("'")
e.Handled = True
End If
Dim currentLength = RichTextBox1.Text.Length
End Sub
hope someone can help Thanks ^_^
RichTextBox1 is the richtextbox
Given a string, how do you generate all partitions of it (shown as smaller strings separated by commas)?
Also, what is the total number of partitions for a string of length n?
The following will give the result, but is not good on long strings.
String: CODE
C,O,D,E
C,O,DE
C,OD,E
C,ODE
CO,D,E
CO,DE
COD,E
String: PEACE
P,E,A,C,E
P,E,A,CE
P,E,AC,E
P,E,ACE
P,EA,C,E
P,EA,CE
P,EAC,E
PE,A,C,E
PE,A,CE
PE,AC,E
PE,ACE
PEA,C,E
PEA,CE
Sub getAllComb()
oriStr = TextBox1.Text
Dim tmp = ""
Dim k = 0
For i = 0 To oriStr.Length
For j = 1 To 3
'tmp = Mid(oriStr, i, j)
Try
tmp1(k) = oriStr.Substring(i, j)
k = k + 1
'tmp = oriStr.Substring(i, j)
'Debug.Print(tmp)
Catch ex As Exception
'Debug.Print("Error>>>>" + ex.Message)
Exit For
End Try
Next
Next
tmp = ""
For i = 0 To k
Debug.Print(i.ToString + "<i " + tmp1(i))
tmp = tmp & tmp1(i) & vbCrLf
Next
'MessageBox.Show(tmp)
Dim tmpAll1 = ""
tmpAll1 = addFunclen4(k)
MessageBox.Show(tmpAll1)
Debug.Print(tmpAll1)
TextBox1.Text = oriStr & vbCrLf & vbCrLf & tmpAll1
End Sub
Function addFunclen4(k As Integer) As String
Dim retVal = ""
Dim tmp = ""
Dim tmpAll = ""
Dim tmpStr = ""
Dim tmpAll1 = ""
For i = 0 To k
For i1 = 0 To k
For i2 = 0 To k
For i3 = 0 To k
For i4 = 0 To k
tmp = Form1.tmp1(i) + Form1.tmp1(i1) + Form1.tmp1(i2) + Form1.tmp1(i3) + Form1.tmp1(i4)
If Form1.tmp1(i) <> "" Then
If tmp = Form1.oriStr Then
tmpStr = Form1.tmp1(i) + "," + Form1.tmp1(i1) + "," + Form1.tmp1(i2) + "," + Form1.tmp1(i3) + "," + Form1.tmp1(i4)
Do While tmpStr.Contains(",,") = True
tmpStr = Replace(tmpStr, ",,", ",")
Loop
If Mid(tmpStr, tmpStr.Length, 1) = "," Then
tmpStr = Mid(tmpStr, 1, tmpStr.Length - 1)
End If
If tmpAll1.Contains(tmpStr) = False Then
tmpAll1 = tmpAll1 + tmpStr + vbCrLf
End If
End If
End If
Next
Next
Next
Next
Next
retVal = tmpAll1
Return retVal
End Function
I reckon [2^(n-1) - 1] in total:
(n-1) positions to put a comma, 2 "states" (comma or not comma), -1 for the trivial case with no commas.
A simpler algorithm would be to iterate through the number of cases and use the binary representation to determine whether to put a comma in each position.
For example (simple form with TextBox, Button and ListBox):
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Dim s As String = TextBox1.Text
If s.Length < 2 Then
MessageBox.Show("Enter a longer string")
Return
End If
For i = 1 To Math.Pow(2, s.Length - 1) - 1
Dim result As String = s(0)
For j = 1 To s.Length - 1
result = result & CommaOrNot(i, j) & s(j)
Next
ListBox1.Items.Add(result)
Next
End Sub
Private Function CommaOrNot(i As Integer, j As Integer) As String
If (i And Math.Pow(2, j - 1)) = Math.Pow(2, j - 1) Then
Return ","
Else
Return ""
End If
End Function
I really liked Fruitbat's approach. Here's an alternate version using a slightly different mechanism for the representation of the binary number and how to determine if the comma should be included or not:
Public Class Form1
Private combinations As List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim s As String = TextBox1.Text
If s.Length < 2 Then
MessageBox.Show("Enter a longer string")
Exit Sub
End If
Button1.Enabled = False
ListBox1.DataSource = Nothing
ListBox1.Items.Clear()
ListBox1.Items.Add("Generating combinations...")
BackgroundWorker1.RunWorkerAsync(s)
End Sub
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim s As String = e.Argument
Dim combinations As New List(Of String)
Dim binary() As Char
Dim values() As Char = s.ToCharArray
Dim max As Integer = Convert.ToInt32(New String("1", s.Length - 1), 2)
Dim sb As New System.Text.StringBuilder
For i As Integer = 0 To max
sb.Clear()
binary = Convert.ToString(i, 2).PadLeft(values.Length, "0").ToCharArray
For j As Integer = 0 To values.Length - 1
sb.Append(If(binary(j) = "0", "", ","))
sb.Append(values(j))
Next
combinations.Add(sb.ToString)
Next
e.Result = combinations
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
combinations = e.Result
ListBox1.Items.Clear()
ListBox1.Items.Add("Generating combinations...Done!")
ListBox1.Items.Add("Adding Results...one moment please!")
Application.DoEvents()
ListBox1.DataSource = Nothing
ListBox1.DataSource = combinations
Button1.Enabled = True
MessageBox.Show("Done!")
End Sub
End Class
I am creating a seat booking program for coursework in which a user selects which row he/she wishes to book seats in, and then the number of seats they wish to book.
Public Class Form1
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rowNumber As Integer
Dim SeatsData As String(,) = {{"booked", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "A10"}, {"booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked", "booked"}, {"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"}, {"D1", "booked", "booked", "D4", "D5", "D6", "D7", "D8", "D9", "D10"}, {"E1", "E2", "booked", "booked", "booked", "E6", "E7", "E8", "E9", "E10"}}
If ListBox2.Text = "A" Then
rowNumber = 0
ElseIf ListBox2.Text = "B" Then
rowNumber = 1
ElseIf ListBox2.Text = "C" Then
rowNumber = 2
ElseIf ListBox2.Text = "D" Then
rowNumber = 3
ElseIf ListBox2.Text = "E" Then
rowNumber = 4
End If
FindSeats(rowNumber, ListBox1.Text, SeatsData)
End Sub
Function FindSeats(ByVal RowNumber As Integer, ByVal NumSeats As Integer, SeatsData As Array) As String
Dim i As Integer = 0
Dim arrayPos As Integer = 0
Dim largestStreak As Integer = 0
Dim FirstSeat As String = 0
Dim LastSeat As String = 0
Dim foundSeats As Boolean = False
Dim returnMsg As String = ""
Do While foundSeats = False
Dim seatChar As String = SeatsData(RowNumber, arrayPos)
arrayPos = arrayPos + 1
If seatChar = "booked" Then
i = 0
Else
If i = 0 Then
FirstSeat = seatChar
End If
i = i + 1
If i > largestStreak Then
largestStreak = i
End If
End If
If i = NumSeats Then
LastSeat = seatChar
If FirstSeat = LastSeat Then
returnMsg = "Found seat: " + FirstSeat
Else
returnMsg = "Found seats: " + FirstSeat + " - " + LastSeat
End If
MsgBox(returnMsg)
Label3.Text = returnMsg
foundSeats = True
Exit Do
End If
If arrayPos = 10 Then
returnMsg = "Not enough available seats, maximum available seats: " + CStr(largestStreak)
MsgBox(returnMsg)
Label3.Text = returnMsg
Exit Do
End If
Loop
End Function
End Class
The issue I am having is that when a user selects the number of seats they want but not which row they want them for, the program automatically books seats in row A. How do I fix this? Also when A user selects a row but not a number of seats (or vice versa) the form goes to code and outputs the following error message: "An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Integer' is not valid."
How do I fix these errors? I am very new to VB so if any of this is obvious then I apologise!
You get the exception because you call FindSeats like this:
FindSeats(rowNumber, ListBox1.Text, SeatsData)
Note that the signature of FindSeats is actually
Function FindSeats(RowNumber As Integer, NumSeats As Integer, SeatsData As Array) As String
So you pass a string ListBox1.Text as NumSeats parameter, which is an Integer. Since you use Option Strict Off, VB.Net tries to implicitly cast the string in ListBox1.Text into an Integer.
This fails if ListBox1.Text is empty, and that's what the exception message it telling you:
Conversion from string "" to type 'Integer' is not valid.
First, turn Option Strict to On, then you'll see all those nifty implicit conversions that will bite you at runtime. Go on and replace them with safe explicit conversions that make sense.
Think about how you want to handle the case when the user clicks Button1 without selecting something in ListBox1 before.
Maybe you want to use Int32.TryParse to handle such cases, or maybe preselect a valid value in ListBox1
Structure TownType
Dim Name As String
Dim County As String
Dim Population As Integer
Dim Area As Integer
End Structure
Sub Main()
Dim TownList As TownType
Dim FileName As String
Dim NumberOfRecords As Integer
FileName = "N:\2_7_towns(2).csv"
FileOpen(1, FileName, OpenMode.Random, , , Len(TownList))
NumberOfRecords = LOF(1) / Len(TownList)
Console.WriteLine(NumberOfRecords)
Console.ReadLine()
There are only 12 records in the file but this returns a value of 24 for number of records. How do I fix this?
Contents of csv file:
Town, County,Pop, Area
Berwick-upon-tweed, Nothumberland,12870,468
Bideford, devon,16262,430
Bognor Regis, West Sussex,62141,1635
Bridlington, East Yorkshire,33589,791
Bridport, Dorset,12977,425
Cleethorpes, Lincolnshire,31853,558
Colwyn bay, Conway,30269,953
Dover, Kent,34087,861
Falmouth, Cornwall,21635,543
Great Yarmouth, Norfolk,58032,1467
Hastings, East Sussex,85828,1998
This will read the contents into a collection and you can get the number of records from the collection.
Sub Main()
Dim FileName As String
Dim NumberOfRecords As Integer
FileName = "N:\2_7_towns(2).csv"
'read the lines into an array
Dim lines As String() = System.IO.File.ReadAllLines(FileName)
'read the array into a collection of town types
'this could also be done i a loop if you need better
'parsing or error handling
Dim TownList = From line In lines _
Let data = line.Split(",") _
Select New With {.Name = data(0), _
.County = data(1), _
.Population = data(2), _
.Area = data(3)}
NumberOfRecords = TownList.Count
Console.WriteLine(NumberOfRecords)
Console.ReadLine()
End Sub
Writing to the console would be accomplished with something like:
For Each town In TownList
Console.WriteLine(town.Name + "," + town.County)
Next
Many ways to do that
Test this:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
dim FileName as string = "N:\2_7_towns(2).csv"
Dim Str() As String = System.IO.File.ReadAllLines(filename)
'Str(0) contains : "Town, County,Pop, Area"
'Str(1) contains : "Berwick-upon-tweed, Nothumberland,12870,468"
'Str(2) contains : "Bideford, devon,16262,430"
' etc...
'Sample code for string searching :
Dim Lst As New List(Of String)
Lst.Add(Str(0))
Dim LookingFor As String = "th"
For Each Line As String In Str
If Line.Contains(LookingFor) Then Lst.Add(Line)
Next
Dim Result As String = ""
For Each St As String In Lst
Result &= St & Environment.NewLine
Next
MessageBox.Show(Result)
'Sample code creating a grid :
Dim Grid = New DataGridView
Me.Controls.Add(Grid)
Grid.ColumnCount = Str(0).Split(","c).GetUpperBound(0) + 1
Grid.RowCount = Lst.Count - 1
Grid.RowHeadersVisible = False
For r As Integer = 0 To Lst.Count - 1
If r = 0 Then
For i As Integer = 0 To Lst(r).Split(","c).GetUpperBound(0)
Grid.Columns(i).HeaderCell.Value = Lst(0).Split(","c)(i)
Next
Else
For i As Integer = 0 To Lst(r).Split(","c).GetUpperBound(0)
Grid(i, r - 1).Value = Lst(r).Split(","c)(i)
Next
End If
Next
Grid.AutoResizeColumns()
Grid.AutoSize = True
End Sub
This is some code for my Computing coursework to create a seat booking system allowing a user to input a desired row and the number of adjacent seats required, and then the form outputs a message showing what seats are available or an alert if there is not enough seats in that row.
The main problem I am having is that I have no errors, but once the submit button is clicked nothing happens which would suggest the "seatsBlock" function is not calling, and I was wondering if anyone could give me a solution
Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rowNumber As Integer
If ListBox2.Text = "A" Then
rowNumber = 1
ElseIf ListBox2.Text = "B" Then
rowNumber = 2
ElseIf ListBox2.Text = "C" Then
rowNumber = 3
ElseIf ListBox2.Text = "D" Then
rowNumber = 4
ElseIf ListBox2.Text = "E" Then
rowNumber = 5
End If
SearchSeats(rowNumber, ListBox1.Text)
End Sub
Function SearchSeats(ByVal RowNumber As Integer, ByVal NumSeats As Integer) As String
Dim CountSeat As Integer = 0 '
Dim FirstSeat As String = 0
Dim lastSeat As String = 0
Dim i As Integer = 0
FirstSeat = SeatsBlock(RowNumber, i, FirstSeat, lastSeat)
Do While NumSeats > CountSeat
If i > 9 Then
Return ("There are not enough seats available in this block")
Exit Do
End If
If SeatsBlock(RowNumber, i, FirstSeat, lastSeat) = "booked" Then
CountSeat = 0
FirstSeat = SeatsBlock(RowNumber, (i + 1), FirstSeat, lastSeat)
Else
lastSeat = SeatsBlock(RowNumber, i, FirstSeat, lastSeat)
CountSeat = i + 1
End If
i = i + 1
Loop
Return ("There are currently " & NumSeats & "seats available: " & FirstSeat & " - " & lastSeat)
End Function
Function SeatsBlock(ByVal RowNumber As Integer, ByVal NumSeats As Integer, ByVal FirstSeat As String, ByVal LastSeat As String) As String
Return ("Currently available are " & NumSeats & " seats available: " & FirstSeat & " - " & LastSeat)
End Function
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim SeatsBlock As String(,) = {{"A1", "A2", "booked", "A4", "A5", "A6", "A7", "A8", "booked", "A10"}, {"booked", "booked", "booked", "B4", "B5", "booked", "booked", "B8", "booked", "B10"}, {"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "C10"}, {"D1", "booked", "booked", "D4", "D5", "D6", "D7", "D8", "D9", "D10"}, {"E1", "E2", "booked", "booked", "booked", "E6", "E7", "E8", "E9", "E10"}}
End Sub
Thanks
Change the following line:
SearchSeats(rowNumber, ListBox1.Text)
for this:
Msgbox(SearchSeats(rowNumber, ListBox1.Text))
That way you get a messagebox, else you will never see the error!