Ceva de genu:
Public Class Student
Public firstName As String = String.Empty
Public surname As String = String.Empty
Public tel As String = String.Empty
Public course As String = String.Empty
End Class
Public Class Form1
Dim cursuri As New List(Of String)(New String() {"Computing Technology", "Grafician in caramida"})
Dim dataBase As New Dictionary(Of String, List(Of Student))
Dim dbDir = "cursuri"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Not System.IO.Directory.Exists(dbDir) Then
System.IO.Directory.CreateDirectory(dbDir)
End If
For Each curs As String In cursuri
cbCourse.Items.Add(curs)
createField(curs)
If (System.IO.File.Exists(dbDir & "\" & curs & ".txt")) Then
For Each line As String In System.IO.File.ReadAllLines(dbDir & "\" & curs & ".txt")
If Not String.IsNullOrEmpty(line) AndAlso line.Contains(":") Then
Dim info() As String = line.Split(":")
Dim student As Student = New Student()
student.firstName = info(0).Trim
student.surname = info(1).Trim
student.tel = info(2).Trim
student.course = curs
addStudent(student)
End If
Next
End If
Next
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
If Not String.IsNullOrEmpty(tbTel.Text) Then
If searchForStudent(tbTel.Text) = True Then
Dim student As Student = getStudent(tbTel.Text)
tbFirstName.Text = student.firstName
tbSurname.Text = student.surname
tbTel.Text = student.tel
cbCourse.SelectedText = student.course
Else
MessageBox.Show(Me, "Studentul nu exista in baza de date", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show(Me, "Studentii se cauta dupa numarul de telefon", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
If Not String.IsNullOrEmpty(tbTel.Text) Then
If searchForStudent(tbTel.Text) = True Then
removeStudent(getStudent(tbTel.Text))
MessageBox.Show(Me, "Studentul a fost sters din baza de date", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(Me, "Studentul nu exista in baza de date", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show(Me, "Studentii se sterg dupa numarul de telefon", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub btnRegister_Click(sender As Object, e As EventArgs) Handles btnRegister.Click
If Not String.IsNullOrEmpty(tbFirstName.Text) AndAlso Not String.IsNullOrEmpty(tbFirstName.Text) AndAlso Not String.IsNullOrEmpty(tbTel.Text) AndAlso cbCourse.SelectedIndex > -1 Then
If searchForStudent(tbTel.Text) = False Then
Dim student As Student = New Student()
student.firstName = tbFirstName.Text
student.surname = tbSurname.Text
student.tel = tbTel.Text
student.course = cbCourse.SelectedItem.ToString
addStudent(student)
MessageBox.Show(Me, tbFirstName.Text & " " & tbSurname.Text & " a fost adaugat in baza de date", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
MessageBox.Show(Me, "Acest student exista in baza de date", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Else
MessageBox.Show(Me, "Te rugam sa completezi toate campurile", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
End Sub
Private Sub btnList_Click(sender As Object, e As EventArgs) Handles btnList.Click
couseList.Items.Clear()
listStudents(cbCourse.SelectedItem.ToString)
End Sub
Private Sub listStudents(ByVal course As String)
MsgBox(course)
If dataBase.ContainsKey(course) Then
If dataBase(course).Count > 0 Then
For Each student As Student In dataBase(course)
couseList.Items.Add(student.firstName & " " & student.surname)
Next
Else
MessageBox.Show(Me, "Nu exista studenti pentru cursul selectat", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error) ' ??
End If
Else
MessageBox.Show(Me, "Nu exista cursul selectat", "Eroare", MessageBoxButtons.OK, MessageBoxIcon.Error) ' ??
End If
End Sub
Private Function searchForStudent(ByVal tel As String) As Boolean
For Each pair In dataBase
For Each student As Student In pair.Value
If student.tel = tel Then
Return True
End If
Next
Next
Return False
End Function
Private Function getStudent(ByVal tel As String) As Student
For Each pair In dataBase
For Each student As Student In pair.Value
If student.tel = tel Then
Return student
End If
Next
Next
Return Nothing
End Function
Private Function getStudentByIndex(ByVal course As String, ByVal index As Integer) As Student
If dataBase.ContainsKey(course) Then
If dataBase(course).Count >= index Then
Return dataBase(course).Item(index)
End If
End If
End Function
Private Sub removeStudent(ByVal student As Student)
For index As Integer = 0 To dataBase(student.course).Count - 1
If dataBase(student.course)(index).tel = student.tel Then
dataBase(student.course).RemoveAt(index)
End If
Next
End Sub
Private Sub addStudent(ByVal student As Student)
If dataBase.ContainsKey(student.course) Then
dataBase.Item(student.course).Add(student)
Else
createField(student.course)
dataBase.Item(student.course).Add(student)
End If
End Sub
Private Sub createField(ByVal fieldName As String)
dataBase.Add(fieldName, New List(Of Student))
End Sub
Private Sub couseList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles couseList.SelectedIndexChanged
If couseList.SelectedIndex > -1 Then
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
For Each pair In dataBase
Dim O As New System.IO.StreamWriter(dbDir & "\" & pair.Key & ".txt", False)
For Each student As Student In pair.Value
O.WriteLine(student.firstName & ":" & student.surname & ":" & student.tel)
Next
O.Close()
Next
End Sub
End Class
A mai ramas putin de lucrat la el; si evident ca poate fi simplificat