Gestion des stagiaires (avec fichier)

Contenu du snippet

Bon c'est un prog en mode console qui permet de réaliser quelques tâches de gestion des stagiaires (Ajout, Suppression, Modification, Recherche, Liste) on utilisons un fichier pour sauvegarder les infos
ce bout de Code Peut lancer une idée chez les débutants (que je suis parmit eux :) ) concernant la manipulation des fichiers sous VB.NET

Bonne Chance

Le Savoir ne sert que s'il est partagé par tout

Source / Exemple :


Imports System.IO

Structure stagiaire
    Dim num As Integer
    Dim nom, mention As String
    Dim moyenne, note() As Double
    Dim nbnot As Integer
End Structure
Module Module1
    Dim stag As stagiaire
    Const chemin As String = "stagiaire.bat"
    Dim nbstg As Integer
    Dim tabfich() As String
    Sub afficher_info(ByVal t)
        Console.WriteLine("le Stagiare Numéro: " & t(0))
        Console.WriteLine("      son nom est : " & t(1))
        Console.WriteLine("   sa moyenne est : " & t(2) & " " & t(3).ToString.ToUpper)
    End Sub
    Sub ecriture(ByVal fiche)
        fiche.WriteLine(stag.num & ";" & stag.nom & ";" & stag.moyenne & ";" & stag.mention)
        Console.Clear()
    End Sub
    Sub suppresion()
        Console.Clear()
        Dim t() As String
        Dim ligne As String
        Dim x As Integer
        Dim num As Integer
        Console.WriteLine("entrez le numéro du stagiaire à supprimé")
        num = Console.ReadLine
        Dim fich As New StreamReader(chemin)
        Do Until fich.EndOfStream
            ligne = fich.ReadLine.Trim
            t = ligne.Split(";")
            If t(0) <> num Then
                ReDim Preserve tabfich(x)
                tabfich(x) = ligne
                x += 1
            End If
        Loop
        fich.Close()
        Dim fiche As New StreamWriter(chemin)
        If x <> Nothing Then
            If MsgBox("voulez vous vraiment supprimé le stagiaire numéro " & num & "?", MsgBoxStyle.Information + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                For i = 0 To UBound(tabfich)
                    fiche.WriteLine(tabfich(i))
                Next
            Else
                Console.WriteLine("aucun stagiaire n'est supprimé")
            End If
        End If
        fiche.Close()
    End Sub
    Sub MAJ()
        Dim x As Integer
        Dim num, ligne, t() As String
        Dim fich As New StreamReader(chemin)

        Do Until fich.EndOfStream
            ligne = fich.ReadLine.Trim
            t = ligne.Split(";")
            For i = 0 To UBound(t)
                ReDim Preserve tabfich(x)
                tabfich(x) = t(i)
                x = UBound(tabfich) + 1
            Next
        Loop
        fich.Close()

        Console.WriteLine("entrez le numéro du stagiaire à modifier")
        num = Console.ReadLine
        Console.WriteLine("qu'est ce que vs voulez modifier??")
        Console.WriteLine("1- Nom")
        Console.WriteLine("2- Sa moyenne")
        x = Console.ReadLine

        For i = x To UBound(tabfich) Step +4

            If tabfich(i - x) = num Then
                Console.WriteLine("entrez la nouvelle valeur")
                tabfich(i) = Console.ReadLine
                If x = 2 Then
                    Call mention(tabfich(i))
                    tabfich(i + 1) = stag.mention
                End If
            End If
        Next
        Dim fiche As New StreamWriter(chemin)
        For i = 0 To UBound(tabfich) Step +4
            fiche.WriteLine(tabfich(i) & ";" & tabfich(i + 1) & ";" & tabfich(i + 2) & ";" & tabfich(i + 3))
        Next
        fiche.Close()
    End Sub
    Sub liste_stagiaire()
        Console.Clear()
        Dim fich As New StreamReader(chemin)
        Dim t(), ligne As String
        Do Until fich.EndOfStream
            ligne = fich.ReadLine
            t = ligne.Split(";")
            afficher_info(t)
        Loop
        fich.Close()
        Console.ReadLine()
        Console.Clear()
    End Sub
    Sub find_stagiaire()
        Dim fiche As New StreamReader(chemin)
        Dim num As Integer
        Dim ligne, t() As String
        Console.WriteLine("entre le num du stagiaire à chercher: ")
        num = Console.ReadLine
        Do Until fiche.Peek = -1
            ligne = fiche.ReadLine
            t = ligne.Split(";")
            If CInt(t(0)) = num Then
                Console.Clear()
                Call afficher_info(t)
                Console.ReadLine()
                Exit Do
            End If
        Loop
        fiche.Close()
        Console.Clear()
    End Sub
    Sub note()
        Dim notee As Double
        Dim s As Double
        Do Until notee = -1
            Console.WriteLine("entrez la " & stag.nbnot + 1 & "note")
            notee = Console.ReadLine
            If notee <> -1 Then
                ReDim Preserve stag.note(stag.nbnot)
                stag.note(stag.nbnot) = notee
                s += notee
                stag.nbnot += 1
            End If
        Loop
        If s <> 0 Then
            stag.moyenne = FormatNumber(s / stag.nbnot)
            Call mention(stag.moyenne)
        Else
            stag.moyenne = Nothing
        End If
    End Sub
    Sub mention(ByVal x)
        If x >= 10 Then
            stag.mention = "Admis"
        ElseIf x <= 10 Then
            stag.mention = "Non Admis"
        End If
    End Sub
    Sub ajouter_stagiaire()
        Dim fiche As StreamWriter
        Dim choix As Char
        Console.WriteLine("1-depuis le début 2 - ajouter")
        choix = Console.ReadLine
        If choix = "1" Then
            nbstg = 0
            fiche = New StreamWriter(chemin, False)
        Else
            fiche = New StreamWriter(chemin, True)
        End If
        Do Until choix = "n"
            Console.Clear()
            stag.nbnot = 0
            ReDim stag.note(stag.nbnot)
            stag.num = nbstg + 1
            Console.WriteLine("entrez le nom du stagiaire numéro: " & nbstg + 1)
            stag.nom = Console.ReadLine
            Console.WriteLine("il as des notes? o/n")
            choix = Console.ReadLine

            If choix = "o" Then
                Console.WriteLine("Entrez -1 pour terminé !!!")
                Call note()
            Else
                stag.moyenne = -1
                stag.mention = "Pas encore de Note"
            End If
            Call ecriture(fiche)
            nbstg += 1
            Console.WriteLine("vous voulez ajouter un autre ? o/n ")
            choix = Console.ReadLine
        Loop
        fiche.Close()
    End Sub
    Sub verif_nbstg()
        Dim fiche As New StreamReader(chemin)
        Do Until fiche.ReadLine = Nothing
            nbstg += 1
        Loop
        fiche.Close()
    End Sub
    Sub Main()
        Console.Title = "Ahmed04@Hotmail.fr"
        If File.Exists(chemin) = False Then
            Dim fich As New StreamWriter(chemin)
            fich.Close()
        End If
        Call verif_nbstg()
        Dim a As Integer = 1
        Do Until a = 0

            Console.WriteLine("1- ajouter")
            Console.WriteLine("2- liste")
            Console.WriteLine("3- rechercher")
            Console.WriteLine("4- supprimer")
            Console.WriteLine("5- MAJ")
            Console.WriteLine("0- Quitter")
            a = Console.ReadLine
            If a = 1 Then
                Call ajouter_stagiaire()
            ElseIf a = 2 Then
                Call liste_stagiaire()
            ElseIf a = 3 Then
                Call find_stagiaire()
            ElseIf a = 4 Then
                Call suppresion()
            ElseIf a = 5 Then
                Call MAJ()
            End If
            Console.Clear()
        Loop
    End Sub

    'Réaliser Par Ahmed
    '@: Ahmed04@hotmail.fr

End Module

Conclusion :


Attention !!!! Il Doit être éxécuté en mode console

Je n'accepterais que les critiques constructuives

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.