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 modir
    {
        int user = 18097;
        int pas = 8617083;
        const int term1 = 4 + (2 * 20);
        const int ostad1 = 4 + 4 + (4 * 40);
        const int danesh = 4 + 4 + (40 * 5)+8;
        const int darse1 = 16 + 2 * 40;
        public void tdanesh(int num)
        {
            FileStream fs=new FileStream("f:\\golestan\\daneshjo.bin",FileMode.Open,FileAccess.Write);
            BinaryWriter bw=new BinaryWriter(fs);
            for (int i = 0; i < num; i++)
            {
                Console.WriteLine("please enter id:");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter name:");
                string x1 = Console.ReadLine();
                Console.WriteLine("please enter family:");
                string x2 = Console.ReadLine();
                Console.WriteLine("please enter fathername:");
                string x3 = Console.ReadLine();
                Console.WriteLine("please enter feild:");
                string x4 = Console.ReadLine();
                Console.WriteLine("please enter maghta:");
                string x8 = Console.ReadLine();
                Console.WriteLine("please enter melinumber:");
                int x5 = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter username:");
                int x6 = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter pasword:");
                int x7 = int.Parse(Console.ReadLine());
                daneshjo s = new daneshjo(x, x1, x2, x3, x4,x8, x5,x6,x7);
                fs.Position = (x - 1) * danesh;
                bw.Write(s.A);
                bw.Write(s.NAME);
                bw.Write(s.FAMILI);
                bw.Write(s.FATHERNAME);
                bw.Write(s.FIELD);
                bw.Write(s.MAGHTA);
                bw.Write(s.MELI);
                bw.Write(s.USER);
                bw.Write(s.PAS);
            }

            fs.Close();
            bw.Close();

        }
        public int USER
        {
            get
            {
                return user;
            }
        }
        public int PAS
        {
            get
            {
                return pas;
            }
        }
        public void tostad(int num)
        {
            FileStream fs=new FileStream("f:\\golestan\\ostad.bin",FileMode.Open,FileAccess.Write);
            BinaryWriter bw=new BinaryWriter(fs);
            for (int i = 0; i < num; i++)
            {
                Console.WriteLine("please enter id:");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter name:");
                string x1 = Console.ReadLine();
                Console.WriteLine("please enter family:");
                string x2 = Console.ReadLine();
                Console.WriteLine("please enter madrak:");
                string x3 = Console.ReadLine();
                Console.WriteLine("please enter mahaleakhz:");
                string x4 = Console.ReadLine();
                Console.WriteLine("please enter vahedmojaz:");
                int x5 = int.Parse(Console.ReadLine());
                ostad s = new ostad(x, x1, x2, x3, x4, x5);
                fs.Position = (x - 1) * ostad1;
                fs.Position = i * ostad1;
                bw.Write(s.A);
                bw.Write(s.NAME);
                bw.Write(s.FAMILI);
                bw.Write(s.MADRAK);
                bw.Write(s.MAHALAKHZ);
                bw.Write(s.VAHED);
            }
            fs.Close();
            bw.Close();

        }
        public void tterm(int num)
        {
            FileStream fs = new FileStream("f:\\golestan\\term.bin", FileMode.Open, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            for (int i = 0; i < num; i++)
            {
                Console.WriteLine("please enter id term:");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter name term:");
                string x1 = Console.ReadLine();
                term s = new term(x,x1);
                fs.Position = (x - 1) * term1;
                bw.Write(s.A);
                bw.Write(s.NAME);
             }
            fs.Close();
            bw.Close();

        }
        public void tkarshenas(int num)
        {
            FileStream fs = new FileStream("f:\\golestan\\karshenas.bin", FileMode.OpenOrCreate, FileAccess.Write);
            BinaryFormatter bw = new BinaryFormatter();
            for (int i = 0; i < num; i++)
            {
                Console.WriteLine("please enter username:");
                int x = int.Parse(Console.ReadLine());
                Console.WriteLine("please enter pasword:");
                int x1 = int.Parse(Console.ReadLine());
                karshenas r = new karshenas(x, x1);
                bw.Serialize(fs, r);
            }
        }


        public void khaliterm(int num)
        {
            term empty = new term(0,"");
            FileStream fs = new FileStream("f:\\golestan\\term.bin", FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            for (int i = 0; i < num; i++)
            {
                fs.Position = i * danesh;
                bw.Write(empty.A);
                bw.Write(empty.NAME);
            }
            fs.Close();
            bw.Close();

        }

        public void khalidanesh(int num)
        {
            daneshjo empty=new daneshjo(0,"","","","","",0,0,0);
            FileStream fs=new FileStream("f:\\golestan\\daneshjo.bin",FileMode.Create,FileAccess.Write);
            BinaryWriter bw=new BinaryWriter(fs);
            for (int i = 0; i < num; i++)
            {
                fs.Position = i * danesh;
                bw.Write(empty.A);
                bw.Write(empty.NAME);
                bw.Write(empty.FAMILI);
                bw.Write(empty.FATHERNAME);
                bw.Write(empty.FIELD);
                bw.Write(empty.MAGHTA);
                bw.Write(empty.MELI);
                bw.Write(empty.USER);
                bw.Write(empty.PAS);

            }
            fs.Close();
            bw.Close();
        }
        public void khaliostad(int num)
        {
            ostad empty = new ostad(0, "", "", "", "", 0);
            FileStream fs = new FileStream("f:\\golestan\\ostad.bin", FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            for (int i = 0; i < num; i++)
            {
                fs.Position = i * ostad1;
                bw.Write(empty.A);
                bw.Write(empty.NAME);
                bw.Write(empty.FAMILI);
                bw.Write(empty.MADRAK);
                bw.Write(empty.MAHALAKHZ);
                bw.Write(empty.VAHED);
            }
            fs.Close();
            bw.Close();
        }
        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}\tdaneshjoyane sabt shode:{2}", bw2.ReadString(), bw1.ReadString(),r.CAPACITY1-r.CAPACITY);
                    }
                }
                fs.Close();
                fs1.Close();
                bw1.Close();
                fs2.Close();
                bw2.Close();
            }
            else
            {
                Console.WriteLine("chenin termi vojod nadaarad");
            }
        }
        public void moshahedekarname(int t)
        {
            FileStream fs = new FileStream("f:\\golestan\\daneshjo.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw = new BinaryReader(fs);
            fs.Position = danesh * (t - 1);
            daneshjo r = new daneshjo(bw.ReadInt32(), bw.ReadString(), bw.ReadString(), bw.ReadString(), bw.ReadString(), bw.ReadString(), bw.ReadInt32(), bw.ReadInt32(), bw.ReadInt32());
            r.karnamekoli();
            fs.Close();
            bw.Close();
        }
        public void sabegheyeostad(int t)
        {
            FileStream fs = new FileStream("f:\\golestan\\araye.bin", FileMode.Create, FileAccess.Read);
            BinaryFormatter bw = new BinaryFormatter();
            FileStream fs1 = new FileStream("f:\\golestan\\ostad.bin", FileMode.OpenOrCreate, FileAccess.Read);
            BinaryReader bw1 = new BinaryReader(fs);
            fs1.Position = ostad1 * t;
            bw1.ReadInt32();
            string s=bw1.ReadString();
            string s1=bw1.ReadString();
            araye r;
            while (fs.Position < fs.Length)
            {
                r = (araye)bw.Deserialize(fs);
                if (r.OSTAD == t)
                    Console.WriteLine("aghaye:{0}{1}terme:{2}darse:{3}", s, s1, r.TERM, r.DARSE);
            }
            fs.Close();
            fs1.Close();
            bw1.Close();
        }
        public void list(int t)
        {
            ArrayList al =new ArrayList();
            akhz r;
            string e2;
            FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.OpenOrCreate, FileAccess.Read);
            BinaryFormatter bw = new BinaryFormatter();
            FileStream fs1=new FileStream("f:\\golestan\\daneshjo.bin",FileMode.Open,FileAccess.Read);
            BinaryReader bw1=new BinaryReader(fs);
            while (fs.Position < fs.Length)
            {
                r = (akhz)bw.Deserialize(fs);
                if (r.DARSE == t)
                {
                    fs1.Position = danesh * (r.DANESH - 1);
                    bw1.ReadUInt32();
                    string e = bw1.ReadString();
                    string e1 = bw1.ReadString();
                    bw1.ReadString();
                    string e3=bw1.ReadString();
                    e2 = e + ' ' + e1+' '+e3;
                    al.Add(e2);
                }
            }
            fs.Close();
            fs1.Close();
            bw1.Close();
            al.Sort();
            for (int i = 0; i < al.Count; i++)
            {
                Console.WriteLine(al[i]);
            }
        }
        public void listsortnomre(int t)
        {
            ArrayList al = new ArrayList();
            akhz r;
            string e2;
            FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.OpenOrCreate, FileAccess.Read);
            BinaryFormatter bw = new BinaryFormatter();
            FileStream fs1 = new FileStream("f:\\golestan\\daneshjo.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw1 = new BinaryReader(fs);
            while (fs.Position < fs.Length)
            {
                r =(akhz) bw.Deserialize(fs);
                if (r.DARSE == t)
                {
                    fs1.Position = danesh * (r.DANESH - 1);
                    bw1.ReadUInt32();
                    string e = bw1.ReadString();
                    string e1 = bw1.ReadString();
                    bw1.ReadString();
                    string e3 = bw1.ReadString();
                    e2 = r.NOMRE.ToString() + '/' + e + ' ' + e1 + ' ' + e3;
                    al.Add(e2);
                }
            }
            fs.Close();
            fs1.Close();
            bw1.Close();
            al.Sort();
            for (int i = 0; i < al.Count; i++)
            {
                Console.WriteLine(al[i].ToString().Substring(al[i].ToString().IndexOf('/') + 1));
            }
        }
        public void sortmoadelterm(int t)
        {
            daneshjo r;
            string y,y1;
            ArrayList al = new ArrayList();
            FileStream fs = new FileStream("f:\\golestan\\daneshjo.bin", FileMode.Create, FileAccess.Read);
            BinaryReader bw = new BinaryReader(fs);
            while (bw.PeekChar() != -1)
            {
                int x = bw.ReadInt32(); string x1 = bw.ReadString(); string x2 = bw.ReadString(); string x3 = bw.ReadString(); string x4 = bw.ReadString(); string x5 = bw.ReadString(); int x6 = bw.ReadInt32(); int x7 = bw.ReadInt32(); int x8 = bw.ReadInt32();
                r = new daneshjo(x, x1, x2, x3, x4, x5, x6, x7, x8);
                if (r.moadel(t) < 10)
                    y1='0'+r.moadel(t).ToString();
                else
                    y1 = r.moadel(t).ToString();

                y = y1 + " /" + x1 + " " + x2;
                al.Add(y);
            }
            fs.Close();
            bw.Close();
            al.Sort();
            for (int i = 0; i < al.Count; i++)
            {
                Console.WriteLine(al[i].ToString().Substring(al[i].ToString().IndexOf('/') + 1));
            }
        }
        public void studentsortmoadelkol()
        {
            daneshjo r;
            string y, y1;
            ArrayList al = new ArrayList();
            FileStream fs = new FileStream("f:\\golestan\\daneshjo.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw = new BinaryReader(fs);
            while (bw.PeekChar() != -1)
            {
                int x = bw.ReadInt32(); string x1 = bw.ReadString(); string x2 = bw.ReadString(); string x3 = bw.ReadString(); string x4 = bw.ReadString(); string x5 = bw.ReadString(); int x6 = bw.ReadInt32(); int x7 = bw.ReadInt32(); int x8 = bw.ReadInt32();
                r = new daneshjo(x, x1, x2, x3, x4, x5, x6, x7, x8);
                if (r.moadelkoli() < 10)
                    y1 = '0' + r.moadelkoli().ToString();
                else
                    y1 = r.moadelkoli().ToString();

                y = y1 + " /" + x1 + " " + x2;
                al.Add(y);
            }
            fs.Close();
            bw.Close();
            al.Sort();
            for (int i = 0; i < al.Count; i++)
            {
                Console.WriteLine(al[i].ToString().Substring(al[i].ToString().IndexOf('/') + 1));
            }
        }
        public void listdoroseostad(int a)
        {
            araye r;
            FileStream fs1 = new FileStream("f:\\golestan\\araye.bin", FileMode.Open, FileAccess.Read);
            BinaryFormatter bf = new BinaryFormatter();
            FileStream fs = new FileStream("f:\\golestan\\dars.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw = new BinaryReader(fs);
            while (fs1.Position < fs1.Length)
            {
                r =(araye) bf.Deserialize(fs1);
                if (r.OSTAD == a)
                {
                    fs.Position = darse1 * (r.DARSE - 1);
                    bw.ReadInt32();
                    Console.WriteLine("ostad:{0}\tdarse:{1}", a, bw.ReadString());
                }
            }
        }
        public void idkoftaade(int t,int k)
        {
            FileStream fs1 = new FileStream("f:\\golestan\\daneshjo.bin", FileMode.Open, FileAccess.Read);
            BinaryReader bw1 = new BinaryReader(fs1);
            FileStream fs = new FileStream("f:\\golestan\\akhz.bin", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            BinaryFormatter bw = new BinaryFormatter();
            
            int t1=0, j=0;
            bw.Deserialize(fs);
            object s=fs.Position;
            fs.Position=0;
            akhz r,r1;
            while (t < fs.Length)
            {
                j = 0;
                r = (akhz)bw.Deserialize(fs);
                if (r.NOMRE < 10)
                {
                    j++;
                    while (fs.Position < fs.Length)
                    {
                        r1 =(akhz) bw.Deserialize(fs);
                        if (r1.DANESH == r.DANESH && r1.NOMRE < 10)
                        {
                            j++;
                        }
                        if (j > k)
                        {
                            fs1.Position = r.DANESH * (danesh - 1);
                            bw1.ReadInt32();
                            string e = bw1.ReadString() + " " + bw1.ReadString() + " " + r1.DANESH.ToString();
                            Console.WriteLine("daneshjoye:{0}", e);
                            fs.Position = fs.Length;
                        }
                    }
                }
                fs.Position = t1 + (int)s;
            }
            fs.Close();
            fs1.Close();
            bw1.Close();
            }       
                    



        


    }
}
