You are on page 1of 3

Imports System.Collections.

Generic

Public Class Team


Public Property Name() As String
Public Property Players As New List(Of String)
Public Property Wins As Integer
Public Property Losses As Integer
Public Property Draws As Integer
Public ReadOnly Property Points As Integer
Get
Return Wins * 3 + Draws
End Get
End Property
End Class

Public Class Match


Public Property HomeTeam() As String
Public Property AwayTeam As String
Public Property HomeScore As Integer
Public Property AwayScore As Integer
End Class

Public Class Tournament


Public Property Teams As New Dictionary(Of String, Team)
Public Property Matches As New List(Of Match)

Public Sub AddTeam(name As String, players As List(Of String))


If Not Teams.ContainsKey(name) Then
Teams.Add(name, New Team With {.Name = name, .Players = players})
End If
End Sub

Public Sub AddMatch(ByVal homeTeam As String, ByVal awayTeam As String)


Matches.Add(New Match With {.HomeTeam = homeTeam, .AwayTeam =
awayTeam})
End Sub

Public Sub RecordResult(ByVal homeTeam As String, ByVal awayTeam As


String, ByVal homeScore As Integer, ByVal awayScore As Integer)
For Each match In Matches
If match.HomeTeam = homeTeam AndAlso match.AwayTeam = awayTeam
Then
match.HomeScore = homeScore
match.AwayScore = awayScore

If homeScore > awayScore Then


Teams(homeTeam).Wins += 1
Teams(awayTeam).Losses += 1
ElseIf homeScore < awayScore Then
Teams(awayTeam).Wins += 1
Teams(homeTeam).Losses += 1
Else
Teams(homeTeam).Draws += 1
Teams(awayTeam).Draws += 1
End If
End If
Next
End Sub

Public Function GetStandings() As List(Of Team)


Return Teams.Values.OrderByDescending(Function(team)
team.Points).ToList()
End Function
End Class

Public Class MainForm


Private ReadOnly tournament As New Tournament()

Private Sub btnAddTeam_Click(ByVal sender As Object, ByVal e As EventArgs)


Handles btnAddTeam.Click
Dim teamName As String = txtTeamName.Text
Dim players As New List(Of String)
(txtPlayers.Text.Split(","c).Select(Function(player) player.Trim()))

tournament.AddTeam(teamName, players)

UpdateTeamList()
End Sub

Private Sub btnAddMatch_Click(ByVal sender As Object, ByVal e As


EventArgs) Handles btnAddMatch.Click
Dim homeTeam As String = cmbHomeTeam.SelectedItem.ToString()
Dim awayTeam As String = cmbAwayTeam.SelectedItem.ToString()

tournament.AddMatch(homeTeam, awayTeam)

UpdateMatchList()
End Sub

Private Sub btnRecordResult_Click(ByVal sender As Object, ByVal e As


EventArgs) Handles btnRecordResult.Click
Dim homeTeam As String = cmbHomeTeam.SelectedItem.ToString()
Dim awayTeam As String = cmbAwayTeam.SelectedItem.ToString()
Dim homeScore As Integer = CInt(txtHomeScore.Text)
Dim awayScore As Integer = CInt(txtAwayScore.Text)

tournament.RecordResult(homeTeam, awayTeam, homeScore, awayScore)

UpdateMatchList()
UpdateStandings()
End Sub

Private Sub UpdateTeamList()


lstTeams.Items.Clear()
lstTeams.Items.AddRange(tournament.Teams.Keys.ToArray())
End Sub

Private Sub UpdateMatchList()


lstMatches.Items.Clear()
For Each match In tournament.Matches
lstMatches.Items.Add($"{match.HomeTeam} vs {match.AwayTeam}")
Next
End Sub
Private Sub UpdateStandings()
dgvStandings.Rows.Clear()
For Each team In tournament.GetStandings()
dgvStandings.Rows.Add(team.Name, team.Wins, team.Losses,
team.Draws, team.Points)
Next
End Sub

Private Sub MainForm_Load(ByVal sender As Object, ByVal e As EventArgs)


Handles MyBase.Load
' Initialize teams and players (for demo purpose)
tournament.AddTeam("Team A", New List(Of String) From {"Player 1",
"Player 2", "Player 3"})
tournament.AddTeam("Team B", New List(Of String) From {"Player 4",
"Player 5", "Player 6"})
tournament.AddTeam("Team C", New List(Of String) From {"Player 7",
"Player 8", "Player 9"})

UpdateTeamList()
End Sub
End Class

You might also like