Practice Exercises C# Sharp - Learn to program with performing exercises C# Sharp

Practice Exercises C# Sharp

Learn to program with performing exercises C# Sharp

Insects + persistence - Practice Exercises C# Sharp
Practice Exercises C# Sharp4,85320940

Insects + persistence - Practice Exercises C# Sharp


Lesson 9:

Object persistence


Exercise 9.4:

Insects + persistence


Objetive:

Create a new version of the "insects" exercise (april 17th), which must save its data using persistence.


Source Code:


using System;
namespace Insects
{
    class Ant : NonFlyingInsect
    {
        public override void ShowData()
        {
            Console.WriteLine("I am an ant.");
        }
    }
}

using System;
namespace Insects
{
    class Bee : FlyingInsect
    {
        public override void ShowData()
        {
            Console.WriteLine("I am a bee.");
        }
    }
}

using System;
namespace Insects
{
    class Fly : FlyingInsect
    {
        public override void ShowData()
        {
            Console.WriteLine("I am a fly.");
        }
    }
}

using System;
namespace Insects
{
    class FlyingInsect : Insect
    {
        public override void ShowData()
        {
            Console.WriteLine("I am a flying insect.");
        }
    }
}

using System;
using System.Runtime.Serialization;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Insects
{
    [Serializable]
    class Insect
    {
        public virtual void ShowData()
        {
            Console.WriteLine("I am an insect.");
        }

        public static void Guardar(string nombre, Insect insect)
        {
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(nombre,
            FileMode.Create, FileAccess.Write, FileShare.None);
            formatter.Serialize(stream, insect);
            stream.Close();
        }

        public static Insect Cargar(string nombre)
        {
            Insect objeto;
            IFormatter formatter = new BinaryFormatter();
            Stream stream = new FileStream(nombre,
            FileMode.Open, FileAccess.Read, FileShare.Read);
            objeto = (Insect)formatter.Deserialize(stream);
            stream.Close();
            return objeto;
        }
    }
}

using System;
namespace Insects
{
    class Mosquito : FlyingInsect
    {
        public override void ShowData()
        {
            Console.WriteLine("I am a mosquito.");
        }
    }
}

using System;
namespace Insects
{
    class NonFlyingInsect : Insect
    {
        public override void ShowData()
        {
            Console.WriteLine("I am a non flying insect.");
        }
    }
}

using System;
namespace Insects
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rand = new Random();
            int amount = 10;
            Insect[] data = new Insect[amount];
            for (int i = 0; i < amount - 1; i++)
            {
                switch (rand.Next(1, 5))
                {
                    case 1:
                        data[i] = new Fly();
                        break;
                    case 2:
                        data[i] = new Mosquito();
                        break;
                    case 3:
                        data[i] = new Bee();
                        break;
                    case 4:
                        data[i] = new Ant();
                        break;
                    case 5:
                        data[i] = new Spider();
                        break;
                }
            }

            for (int i = 0; i < amount - 1; i++)
                data[i].ShowData();

        }
    }
}

using System;
namespace Insects
{
    class Spider : NonFlyingInsect
    {
        public override void ShowData()
        {
            Console.WriteLine("I am a spider.");
        }
    }
}
Exercisey 9.4

Share Now:







Privacy Policy:



Google uses associated advertising companies to serve ads when it visits our website. These companies may use the information they obtain from your visits to this and other websites (not including your name, address, email address, or phone number) to provide you with announcements about products and services that interest you. If you would like to learn more about this practice and know your options to prevent these companies from using this information. Click in... Privacy and Terms of Google.

Cookies

This site uses Google cookies to provide its services, to personalize advertisements and to analyze traffic. Google receives information about your use of this website. More information in... Privacy and Terms of Google.