using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

namespace golestan
{
    [Serializable]
    class daneshjo
    {
        const int darse1 = 16 + 2 * 40;
        const int term1 = 4 + (2 * 20);
        const int ostad1 = 4 + 4 + (4 * 40);
        const int x = 4 + 4 + (40 * 5)+8;
        int a;
        string name;
        string famili;
        string fathername;
        string field;
        string maghta;
        int meli;
        int user;
        int pas;
        public daneshjo(int a1, string a2, string a3, string a4, string a5,string a9 ,int a6,int a7,int a8)
        {
            a = a1;
            user = a7;
            pas = a8;
            if (a9.Length > 20)
            {
                maghta = a2.Remove(20);
            }
            else if (a9.Length < 20)
            {
                maghta = a9 + new string(' ', 20 - a9.Length);
            }
            else
                maghta = a9;
            if (a2.Length > 20)
            {
                name = a2.Remove(20);
            }
            else if (a2.Length < 20)
            {
                name = a2 + new string(' ', 20 - a2.Length);
            }
            else
                name = a2;
            if (a3.Length > 20)
            {
                famili = a3.Remove(20);
            }
            else if (a3.Length < 20)
            {
                famili = a3 + new string(' ', 20 - a3.Length);
            }
            else
                famili = a3;
            if (a4.Length > 20)
            {
                fathername = a4.Remove(20);
            }
            else if (a4.Length < 20)
            {
                fathername = a4 + new string(' ', 20 - a4.Length);
            }
            else
                fathername = a4;
            if (a5.Length > 20)
            {
                field = a5.Remove(20);
            }
            else if (a5.Length < 20)
            {
                field = a5 + new string(' ', 20 - a5.Length);
            }
            else
                field = a5;
            meli = a6;
        }
        public string MAGHTA
        {
            get
            {
                return maghta;
            }
            set
            {
                if (value.Length > 20)
                {
                    maghta = value.Remove(20);
                }
                else if (value.Length < 20)
                {
                    maghta = value + new string(' ', 20 - value.Length);
                }
                else
                    maghta = value;
            }
        }

        public int USER
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
            }
        }
        public int PAS
        {
            get
            {
                return pas;
            }
            set
            {
                pas = value;
            }
        }
        public int A
        {
            get
            {
                return a;
            }
            set
            {
                a = value;
            }
        }
        public string NAME
        {
            get
            {
                return name;
            }
            set
            {
                if (value.Length > 20)
                {
                    name = value.Remove(20);
                }
                else if (value.Length < 20)
                {
                    name = value + new string(' ', 20 - value.Length);
                }
                else
                    name = value;
            }
        }
        public string FAMILI
        {
            get
            {
                return famili;
            }
            set
            {
                if (value.Length > 20)
                {
                    famili = value.Remove(20);
                }
                else if (value.Length < 20)
                {
                    famili = value + new string(' ', 20 - value.Length);
                }
                else
                    famili = value;
            }
        }
        public string FATHERNAME
        {
            get
            {
                return fathername;
            }
            set
            {
                if (value.Length > 20)
                {
                    fathername = value.Remove(20);
                }
                else if (value.Length < 20)
                {
                    fathername = value + new string(' ', 20 - value.Length);
                }
                else
                    fathername = value;
            }
        }
        public string FIELD
        {
            get
            {
                return field;
            }
            set
            {
                if (value.Length > 20)
                {
                    field = value.Remove(20);
                }
                else if (value.Length < 20)
                {
                    field = value + new string(' ', 20 - value.Length);
                }
                else
                    field = value;
            }
        }
        public int MELI
        {
            get
            {
                return meli;
            }
            set
            {
                meli = value;
            }
        }
        public void printlist(int num)
        {
            FileStream fs3 = new FileStream("f:\\golestan\\term.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw3 = new BinaryReader(fs3);
            fs3.Position = term1 * (num-1);
            int s= bw3.ReadInt32();
            fs3.Close();
            bw3.Close();
            if (num == s)
            {
                int x, x1;
                FileStream fs = new FileStream("f:\\golestan\\araye.bin", FileMode.Open, FileAccess.Read);
                BinaryFormatter bw = new BinaryFormatter();
                FileStream fs1 = new FileStream("f:\\golestan\\ostad.bin", FileMode.Open, FileAccess.Read);
                BinaryReader bw1 = new BinaryReader(fs);
                FileStream fs2 = new FileStream("f:\\golestan\\dars.bin", FileMode.Open, FileAccess.Read);
                BinaryReader bw2 = new BinaryReader(fs);
                araye r;
                while (fs.Position < fs.Length)
                {
                    r = (araye)bw.Deserialize(fs);
                    if (r.TERM == num)
                    {

                        x = r.DARSE;
                        fs2.Position = darse1 * x + 4;
                        x1 = r.OSTAD;
                        fs1.Position = ostad1 * x1 + 4;
                        Console.WriteLine("dars:{0}\tostad:{1}\tcapacity:{2}", bw2.ReadString(), bw1.ReadString(), r.CAPACITY);
                    }
                }
                fs.Close();
                fs1.Close();
                bw1.Close();
                fs2.Close();
                bw2.Close();
            }
            else
            {
                Console.WriteLine("chenin termi vojod nadaarad");
            }

        }
        public double moadel(int tt)
        {
            FileStream fs2 = new FileStream("f:\\golestan\\term.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw2 = new BinaryReader(fs2);
            fs2.Position = term1 * (tt-1);
            int p= bw2.ReadInt32();
            fs2.Close();
            bw2.Close();
            if (tt == p)
            {

                FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.Open, FileAccess.Read);
                BinaryFormatter bw = new BinaryFormatter();
                FileStream fs1 = new FileStream("f:\\golestan\\darse.bin", FileMode.Open, FileAccess.Read);
                BinaryReader bw1 = new BinaryReader(fs1);
                akhz r;
                int t = 1, i = 0;
                double s = 0;
                while (fs.Position < fs.Length)
                {
                    r = (akhz)bw.Deserialize(fs);
                    if (r.DANESH == a && r.TERM == tt)
                    {
                        fs.Position = (16 + 2 * 40) * (r.DARSE - 1);
                        bw1.ReadUInt32();
                        bw1.ReadString();
                        bw1.ReadString();
                        t = bw1.ReadInt32();
                        s += r.NOMRE * t;
                        i++;
                    }
                }
                fs.Close();
                fs1.Close();
                bw1.Close();
                return s / i;
            }
            else
                return -1;
            
        }
        public void karnametermi(int tt)
        {
            FileStream fs2 = new FileStream("f:\\golestan\\term.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw2 = new BinaryReader(fs2);
            fs2.Position = term1 * (tt-1);
            int f= bw2.ReadInt32();

            if (tt == f)
            {
                FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.Open, FileAccess.Read);
                BinaryFormatter bw = new BinaryFormatter();
                FileStream fs1 = new FileStream("f:\\golestan\\darse.bin", FileMode.Open, FileAccess.Read);
                BinaryReader bw1 = new BinaryReader(fs1);
                akhz r;
                int t = 1, i = 0;
                double s = 0;
                string p;
                while (fs.Position < fs.Length)
                {
                    r = (akhz)bw.Deserialize(fs);
                    if (r.DANESH == a && r.TERM == tt)
                    {
                        fs.Position = (16 + 2 * 40) * (r.DARSE - 1);
                        bw1.ReadInt32();
                        p = bw1.ReadString();
                        bw1.ReadString();
                        t = bw1.ReadInt32();
                        s += r.NOMRE * t;
                        i++;
                        Console.WriteLine("darse:{}\tnomre{1}", p, r.NOMRE);
                    }
                }
                fs.Close();
                fs1.Close();
                bw1.Close();
                Console.WriteLine("moadel:{0}\t", s / i);
            }
            else
            {
                Console.WriteLine("chenin termi vojod nadaarad");
            }
        }
        public void karnamekoli()
        {
            FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.Open, FileAccess.Read);
            BinaryFormatter bw = new BinaryFormatter();
            FileStream fs1 = new FileStream("f:\\golestan\\darse.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw1 = new BinaryReader(fs1);
            akhz r;
            int t=1, i = 0;
            double s = 0;
            string p;
            while (fs.Position < fs.Length)
            {
                r = (akhz)bw.Deserialize(fs);
                if (r.DANESH == a)
                {
                    fs.Position = (16 + 2 * 40) * (r.DARSE - 1);
                    bw1.ReadInt32();
                    p = bw1.ReadString();
                    bw1.ReadString();
                    t = bw1.ReadInt32();
                    s += r.NOMRE * t;
                    i++;
                    Console.WriteLine("darse:{}\tnomre{1}", p, r.NOMRE);
                }
            }
            fs.Close();
            fs1.Close();
            bw1.Close();
            Console.WriteLine("moadel:{0}\t", s / i);
        }
        public double moadelkoli()
        {
            FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.Open, FileAccess.Read);
            BinaryFormatter bw = new BinaryFormatter();
            FileStream fs1 = new FileStream("f:\\golestan\\darse.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw1 = new BinaryReader(fs1);
            akhz r;
            int t = 1, i = 0;
            double s = 0;
            string p;
            while (fs.Position < fs.Length)
            {
                r = (akhz)bw.Deserialize(fs);
                if (r.DANESH == a)
                {
                    fs.Position = (16 + 2 * 40) * (r.DARSE - 1);
                    bw1.ReadInt32();
                    p = bw1.ReadString();
                    bw1.ReadString();
                    t = bw1.ReadInt32();
                    s += r.NOMRE * t;
                    i++;
                   
                }
            }
            fs.Close();
            fs1.Close();
            bw1.Close();
            return (s / i);
        }


    }
}
