9.04 - Insects + persistence

Objetive

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

Solution

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.");
        }
    }
}

Popular Posts