public static void generate()
        {
            Random rnd = new Random();
	        contract tcontract;
	        cheese t1;
	        wheat t2;
	        //string tstr = "";
	        int t;
	        if (rnd.Next(2)==0)
	        {
		        tcontract = new cheese();
		        t1 = (cheese)tcontract;
		        t1.setType(rnd.Next(3)+1);
		        t1.setFat(10*(rnd.Next(3)+3));
		        t1.setPack(rnd.Next(2)+1);
	        }
	        else
	        {
		        tcontract = new wheat();
		        t2 = (wheat)tcontract;
		        t = rnd.Next(3);
		        if (t==0)
                    t2.setGrade(t);
		        else
                    t2.setGrade(t+2);
		        t2.setHardness(rnd.Next(2)+1);
	        }
	        tcontract.setNumber(rnd.Next(100));
	        t=rnd.Next(2);
	        if (t==0)
                tcontract.setRealNumber(tcontract.getNumber());
	        else
                tcontract.setRealNumber((int)(tcontract.getNumber() * (0.5 + rnd.NextDouble())));
	        if (db.Count != 0)
	        {
		        tcontract.setDay(db[db.Count-1].getDay());
		        if (db[db.Count-1].getMonth()<12)
		        {
			        tcontract.setMonth(db[db.Count-1].getMonth()+1);
			        tcontract.setYear(db[db.Count-1].getYear());
		        }
		        else
		        {
			        tcontract.setMonth(1);
			        tcontract.setYear(db[db.Count-1].getYear()+1);
		        }
	        }
	        else
	        {
		        tcontract.setYear(2008);
		        tcontract.setMonth(1);
		        tcontract.setDay(12);
	        }
	        tcontract.setMoney(rnd.Next(10000));
	        tcontract.setName(rndstr(rnd));
            tcontract.setProvider(rndstr(rnd));
            tcontract.setReceiver(rndstr(rnd));
	        db.Add(tcontract);
        }
 public static bool loadFromFile(string filename)
 {
     string type = "";
     string cmd, t;
     int code = 0, ti;
     contract tcontract = new cheese();
     StreamReader fin;
     try
     {
         fin = new StreamReader(filename);
     }
     catch
     {
         return false;
     }
     {
         t = readBeforeSpace(fin);
         while (fin.EndOfStream == false)
         {
             cmd = readBeforeSpace(fin);
             if (fin.EndOfStream == true || cmd[0] == '[')
             {
                 if (type == "cheese")
                 {
                     cheese tcheese = (cheese)tcontract;
                     db.Add(tcheese);
                 }
                 else if (type == "wheat")
                 {
                     wheat twheat = (wheat)tcontract;
                     db.Add(twheat);
                 }
                 type = "";
             }
             else
             {
                 t = readBeforeSpace(fin);
                 if (t == "=")
                 {
                     if (cmd == "contract") code = 1;
                     else if (cmd == "name") code = 2;
                     else if (cmd == "provider") code = 3;
                     else if (cmd == "receiver") code = 4;
                     else if (cmd == "year") code = 5;
                     else if (cmd == "month") code = 6;
                     else if (cmd == "day") code = 7;
                     else if (cmd == "number") code = 8;
                     else if (cmd == "realnumber") code = 9;
                     else if (cmd == "money") code = 10;
                     else if (cmd == "type") code = 11;
                     else if (cmd == "fat") code = 12;
                     else if (cmd == "pack") code = 13;
                     else if (cmd == "grade") code = 14;
                     else if (cmd == "hardness") code = 15;
                 }
                 else
                     t = readBeforeSpace(fin);
                 switch (code)
                 {
                     case 1:
                         type = readBeforeSpace(fin);
                         if (type == "cheese")
                         {
                             tcontract = new cheese();
                         }
                         else if (type == "wheat")
                         {
                             tcontract = new wheat();
                         }
                         break;
                     case 2:
                         t = readBeforeSpace(fin);
                         tcontract.setName(reSpace(t, false));
                         break;
                     case 3:
                         t = readBeforeSpace(fin);
                         tcontract.setProvider(reSpace(t, false));
                         break;
                     case 4:
                         t = readBeforeSpace(fin);
                         tcontract.setReceiver(reSpace(t, false));
                         break;
                     case 5:
                         ti = Convert.ToInt32(readBeforeSpace(fin));
                         tcontract.setYear(ti);
                         break;
                     case 6:
                         ti = Convert.ToInt32(readBeforeSpace(fin));
                         tcontract.setMonth(ti);
                         break;
                     case 7:
                         ti = Convert.ToInt32(readBeforeSpace(fin));
                         tcontract.setDay(ti);
                         break;
                     case 8:
                         ti = Convert.ToInt32(readBeforeSpace(fin));
                         tcontract.setNumber(ti);
                         break;
                     case 9:
                         ti = Convert.ToInt32(readBeforeSpace(fin));
                         tcontract.setRealNumber(ti);
                         break;
                     case 10:
                         ti = Convert.ToInt32(readBeforeSpace(fin));
                         tcontract.setMoney(ti);
                         break;
                     case 11:
                         if (type == "cheese")
                         {
                             cheese tcheese = (cheese)tcontract;
                             ti = Convert.ToInt32(readBeforeSpace(fin));
                             tcheese.setType(ti);
                         }
                         break;
                     case 12:
                         if (type == "cheese")
                         {
                             cheese tcheese = (cheese)tcontract;
                             ti = Convert.ToInt32(readBeforeSpace(fin));
                             tcheese.setFat(ti);
                         }
                         break;
                     case 13:
                         if (type == "cheese")
                         {
                             cheese tcheese = (cheese)tcontract;
                             ti = Convert.ToInt32(readBeforeSpace(fin));
                             tcheese.setPack(ti);
                         }
                         break;
                     case 14:
                         if (type == "wheat")
                         {
                             wheat twheat = (wheat)tcontract;
                             ti = Convert.ToInt32(readBeforeSpace(fin));
                             twheat.setGrade(ti);
                         }
                         break;
                     case 15:
                         if (type == "wheat")
                         {
                             wheat twheat = (wheat)tcontract;
                             ti = Convert.ToInt32(readBeforeSpace(fin));
                             twheat.setHardness(ti);
                         }
                         break;
                 }
             }
         }
         fin.Close();
         return true;
     }
 }
        public static void updateElem(int index, System.Windows.Forms.DataGridView tdataGridView)
        {
	        contract tcontract = null;
	        if (string.Compare(tdataGridView.Rows[index].Cells["contract"].Value.ToString(), "Сыр") == 0)
	        {
		        tcontract = new cheese();
		        cheese tcheese = (cheese)tcontract;
		        if (tdataGridView.Rows[index].Cells["type"].Value.ToString() == "Алтайский")
			        tcheese.setType(1);
		        else if (tdataGridView.Rows[index].Cells["type"].Value.ToString() == "Радонежский")
			        tcheese.setType(2);
		        else if (tdataGridView.Rows[index].Cells["type"].Value.ToString() == "Голландский")
			        tcheese.setType(3);
		        if (tdataGridView.Rows[index].Cells["fat"].Value.ToString() == "30%")
			        tcheese.setFat(30);
		        else if (tdataGridView.Rows[index].Cells["fat"].Value.ToString() == "40%")
			        tcheese.setFat(40);
		        else if (tdataGridView.Rows[index].Cells["fat"].Value.ToString() == "50%")
			        tcheese.setFat(50);
		        if (tdataGridView.Rows[index].Cells["pack"].Value.ToString() == "Плёнка")
			        tcheese.setPack(1);
		        else if (tdataGridView.Rows[index].Cells["pack"].Value.ToString() == "Парафин")
			        tcheese.setPack(2);
	        }
	        else if (string.Compare(tdataGridView.Rows[index].Cells["contract"].Value.ToString(), "Пшеница") == 0)
	        {
		        tcontract = new wheat();
		        wheat twheat = (wheat)tcontract;
		        if (tdataGridView.Rows[index].Cells["grade"].Value.ToString() == "Нестд")
			        twheat.setGrade(0);
		        else if (tdataGridView.Rows[index].Cells["grade"].Value.ToString() == "3")
			        twheat.setGrade(3);
                else if (tdataGridView.Rows[index].Cells["grade"].Value.ToString() == "4")
			        twheat.setGrade(4);
		        if (tdataGridView.Rows[index].Cells["hardness"].Value.ToString() == "Твёрдая")
			        twheat.setHardness(1);
                else if (tdataGridView.Rows[index].Cells["hardness"].Value.ToString() == "Мягкая")
			        twheat.setHardness(2);
	        }
	        if (tdataGridView.Rows[index].Cells["name"].Value != null)
		        tcontract.setName(tdataGridView.Rows[index].Cells["name"].Value.ToString());
	        if (tdataGridView.Rows[index].Cells["provider"].Value != null)
		        tcontract.setProvider(tdataGridView.Rows[index].Cells["provider"].Value.ToString());
	        if (tdataGridView.Rows[index].Cells["receiver"].Value != null)
		        tcontract.setReceiver(tdataGridView.Rows[index].Cells["receiver"].Value.ToString());
	        char []tmp = {'-', '.', '/', '\\'};
	        string []date = tdataGridView.Rows[index].Cells["date"].Value.ToString().Split(tmp,3);
	        if (date.Length == 3)
	        {
		        if (checkStr(date[0])) tcontract.setYear(Convert.ToInt32(date[0]));
		        if (checkStr(date[1])) tcontract.setMonth(Convert.ToInt32(date[1]));
		        if (checkStr(date[2])) tcontract.setDay(Convert.ToInt32(date[2]));
	        }
	        if (checkStr(tdataGridView.Rows[index].Cells["number"].Value.ToString()))
		        tcontract.setNumber(Convert.ToInt32(tdataGridView.Rows[index].Cells["number"].Value.ToString()));
	        if (checkStr(tdataGridView.Rows[index].Cells["realnumber"].Value.ToString()))
		        tcontract.setRealNumber(Convert.ToInt32(tdataGridView.Rows[index].Cells["realnumber"].Value.ToString()));
	        if (checkStr(tdataGridView.Rows[index].Cells["money"].Value.ToString()))
		        tcontract.setMoney(Convert.ToInt32(tdataGridView.Rows[index].Cells["money"].Value.ToString()));
	        db[index] = tcontract;
        }