Insectos + persistencia Ejercicio VB.Net - Curso de Programación Visual Basic

 Ejercicio

Insectos + persistencia

Objetivo

Crear una nueva versión del ejercicio "insectos" (17 de abril), que debe guardar sus datos utilizando la persistencia.

Código

Imports System
Namespace Insects
    Class Ant
        Inherits NonFlyingInsect

        Public Overrides Sub ShowData()
            Console.WriteLine("I am an ant.")
        End Sub
    End Class
End Namespace

Namespace Insects
    Class Bee
        Inherits FlyingInsect

        Public Overrides Sub ShowData()
            Console.WriteLine("I am a bee.")
        End Sub
    End Class
End Namespace

Namespace Insects
    Class Fly
        Inherits FlyingInsect

        Public Overrides Sub ShowData()
            Console.WriteLine("I am a fly.")
        End Sub
    End Class
End Namespace

Namespace Insects
    Class FlyingInsect
        Inherits Insect

        Public Overrides Sub ShowData()
            Console.WriteLine("I am a flying insect.")
        End Sub
    End Class
End Namespace

Namespace Insects
    [Serializable]
    Class Insect
        Public Overridable Sub ShowData()
            Console.WriteLine("I am an insect.")
        End Sub

        Public Shared Sub Guardar(ByVal nombre As String, ByVal insect As Insect)
            Dim formatter As IFormatter = New BinaryFormatter()
            Dim stream As Stream = New FileStream(nombre, FileMode.Create, FileAccess.Write, FileShare.None)
            formatter.Serialize(stream, insect)
            stream.Close()
        End Sub

        Public Shared Function Cargar(ByVal nombre As String) As Insect
            Dim objeto As Insect
            Dim formatter As IFormatter = New BinaryFormatter()
            Dim stream As Stream = New FileStream(nombre, FileMode.Open, FileAccess.Read, FileShare.Read)
            objeto = CType(formatter.Deserialize(stream), Insect)
            stream.Close()
            Return objeto
        End Function
    End Class
End Namespace

Namespace Insects
    Class Mosquito
        Inherits FlyingInsect

        Public Overrides Sub ShowData()
            Console.WriteLine("I am a mosquito.")
        End Sub
    End Class
End Namespace

Namespace Insects
    Class NonFlyingInsect
        Inherits Insect

        Public Overrides Sub ShowData()
            Console.WriteLine("I am a non flying insect.")
        End Sub
    End Class
End Namespace

Namespace Insects
    Class Program
        Private Shared Sub Main(ByVal args As String())
            Dim rand As Random = New Random()
            Dim amount As Integer = 10
            Dim data As Insect() = New Insect(amount - 1) {}

            For i As Integer = 0 To amount - 1 - 1

                Select Case rand.[Next](1, 5)
                    Case 1
                        data(i) = New Fly()
                    Case 2
                        data(i) = New Mosquito()
                    Case 3
                        data(i) = New Bee()
                    Case 4
                        data(i) = New Ant()
                    Case 5
                        data(i) = New Spider()
                End Select
            Next

            For i As Integer = 0 To amount - 1 - 1
                data(i).ShowData()
            Next
        End Sub
    End Class
End Namespace

Namespace Insects
    Class Spider
        Inherits NonFlyingInsect

        Public Overrides Sub ShowData()
            Console.WriteLine("I am a spider.")
        End Sub
    End Class
End Namespace

Juan A. Ripoll - Tutoriales de Sistemas y Cursos de Programación© 2024 Todos los derechos reservados.  Condiciones legales.