开发者

Doing a sort of a database where the user enters id names etc and then when users enter id he can add courses to that user

开发者 https://www.devze.com 2023-02-22 18:43 出处:网络
I am having some trouble as follows: When i add a new user,it works fine but when I add the second user, it forgets the first one. It keeps forgetting the preceding user added before it. Here is the c

I am having some trouble as follows: When i add a new user, it works fine but when I add the second user, it forgets the first one. It keeps forgetting the preceding user added before it. Here is the code:

    static List<Students> stud = new List<Students>();
    static Courses regcor = new Courses(0,"");
    static void Main(string[] args)
    {
        Students regstud = new Students("", "", "", 0);
        string idselec = "";
        string selec = "";
        do
        {


            Console.WriteLine("1.Register new student.");
            Console.WriteLine("2.Add course.");
            Console.WriteLine("3.All information.");
            Console.WriteLine("4.Exit.");
            selec = Console.ReadLine();
            if (selec == "1")
            {
                do
                {
                    Console.Clear();
                    Console.WriteLine("Enter ID");
                    idselec = Console.ReadLine();
                    if (checkid(idselec))
                    {
                        Console.WriteLine("Id already excsists");
                        Console.ReadLine();
                    }

                }
                while (checkid(idselec));
                regstud.ID = idselec;
                Console.WriteLine("Enter Name");
                regstud.name = Console.ReadLine();
                Console.WriteLine("Enter Surname");
                regstud.surname = Console.ReadLine();
                Console.WriteLine("Enter Age");
                regstud.age = Convert.ToInt32(Console.ReadLine());
                stud.Add(regstud);
            }
            else if (selec == "2")
            {
                Console.WriteLine("Enter ID");
                idselec = Console.ReadLine();
                check(idselec);

            }
            else if (selec == "3") 
            {
                Console.Clear();
                writeall();
            }

        }
        while (selec != "4");


    }
    static bool checkid(string id)
    {

        return stud.Any(u => u.ID == id);
    }

    static void check(string ID)
    {
        int i = 0;
        bool found = false;
        do
        {
            if (stud[i].ID == ID )
            {

                Console.WriteLine("Hello " + stud[i].name+" "+ stud[i].surname) ;
                Console.WriteLine("Enter code");
                int code =Convert.ToInt32( Console.ReadLine());
                Console.WriteLine("Enter Name");
                string name = Console.ReadLine();

                regcor.CourID = code;
                regcor.courname = name;
                stud[i].cour(regcor);

                found = true;


            }

            i++;
        }
        while ((i < stud.Count) && !(found));




    }
    static void writeall()
    {
        int i = 0, y=0, sub=0,sub3=0;
        string sub2="";
        do
        {
            Console.WriteLine(stud[i].ID);
            Console.WriteLine(stud[i].name);
            Console.WriteLine(stud[i].surname);
            Console.WriteLine(stud[i].age);
            sub3 = stud[i].cour3();
            do
            {
                sub = stud[i].cour1(y);
                sub2 = stud[i].cour2(y);
                Console.WriteLine(sub);
                Console.WriteLine(sub2);

                y++;


            }
            while (y < sub3);
            i++;
        }
        while (i < stud.Count);
    }
}

}

This is the class Courses:

    public int CourID = 0;
    public string courname = "";
    public Courses(int corID, string corname)
    {
        this.CourID = corID;
        this.courname = corname;

    }
}

}

This is the class Students;

    public int age = 0;
    public string name = "", surname = "", ID = "";
    List<Courses> cours = new List<Courses>();
    public Students(string name, string surname,string ID, int age)
    {
        this.ID = ID;
        this.surname = surname;
        this.age = age;
        this.name = name;
    }
    public void cour(Courses c)
    {
        cours.Add(c);
    }
    public int cour1(int i)
    {
       return cours[i].CourID;

    }
    public string cour2(i开发者_如何学Pythonnt i)
    {
      return  cours[i].courname;
    }
    public int cour3()
    {
        return cours.Count;
    }

}

}


You are currently only creating one student, then overwrite his properties

Move this:

Students regstud = new Students("", "", "", 0);

into your do loop where you actually create a new student:

if (selec == "1")
{
   Students regstud = new Students("", "", "", 0);
   ...
}

Even better would be only creating the Student object once you have all of his properties ready:

if (selec == "1")
{
   ...
   Console.WriteLine("Enter Name");
   string name = Console.ReadLine();
   Console.WriteLine("Enter Surname");
   string surname = Console.ReadLine();
   Console.WriteLine("Enter Age");
   int age = Convert.ToInt32(Console.ReadLine());

   Students regstud = new Students(name, surename, id, age);
   ...
}

Besides this your code looks like of a combination of Stone Age and Futurama to me: You use archaic loop constructs with separate index variables on the one hand, and then generics and LINQ on the other. Try to stick with higher abstractions as possible - most loop variables you use can be avoided, if you use ForEach() etc - this will make your code much cleaner and easier to read.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号