简体   繁体   中英

C# making sure that a subject assigned to a student can have one or many results

I am currently creating a simple student administration. within that I added a function to add a student to a class. Furthermore I also added a function that gives an individual students some lessons/classes they can follow.

Now what I want is to add a function that allows a student to get a result per subject they take. Problem is that I don't know how I can pull that off. I have tried several things, but they don't seem to work

Here I will show you the code I am currently working with in case it might help provide some insight. although its not perfect, i am quite proud of what i have managed to pull of here. having said that: here is the program class in which most of the initializing and lists are present

    class Program
{

    //lijst met klassen-------------------------------------------------------------------------------------
    static List<Klas> Klassenlijst = new List<Klas>()
    {
        //klassen worden aangemaakt
        new Klas("KL0001", "AO1-A", new List<Student>()),                           //klas [0]
        new Klas("KL0002", "AO1-B", new List<Student>()),                           //klas [1]
        new Klas("KL0003", "AO2-A", new List<Student>()),                           //klas [2]
        new Klas("KL0004", "AO3-B", new List<Student>()),                           //klas [3]
        new Klas("KL0005", "GD1-A", new List<Student>()),                           //klas [4]
        new Klas("KL0069", "STM-F", new List<Student>()),                           //klas [5]
    };

    //lijst met studenten
    static List<Student> StudentenLijst = new List<Student>()
    {
        new Student("S101010", "Test123", "69", "420"),                             //student [0]
        new Student("S202020", "Dio Brando", "1234AT", "+31 06 261552"),            //student [1]
        new Student("S303030", "Mark Rutte", "2345MK", "+31 06 289985"),            //student [2]
        new Student("S404040", "Jarl Balgruuf", "5897WH", "+31 06 123456"),         //student [3]
        new Student("S505050", "Darth Revan", "3131MK", "+31 06 280985"),           //student [4]
        new Student("S606060", "Geralt Of Rivia", "3459RV", "+31 06 909090"),       //student [5]
        new Student("S707070", "Obama II", "0262BR", "+31 06 158775"),              //student [6]
    };

    //Lijst met vakken die een student kan volgen
    static List<Vakken> VakkenLijst = new List<Vakken>()
    {
        new Vakken("0", "Nederlands"),                                              //Vak [0]
        new Vakken("1", "Engels"),                                                  //Vak [1]
        new Vakken("2", "Rekenen"),                                                 //Vak [2]
        new Vakken("3", "Software Programming"),                                    //Vak [3]
        new Vakken("4", "Web Development"),                                         //Vak [4]
        new Vakken("5", "Tools en Devices"),                                        //Vak [5]
        new Vakken("6", "Maatschappijleer"),                                        //Vak [6]
        new Vakken("7", "Game Development"),                                        //Vak [7]
        new Vakken("8", "project"),                                                 //Vak [8]
    };

    //this  is where the results of the lessons are created.
    static List<Cijfer> Cijferlijst = new List<Cijfer>()
    {
        new Cijfer(1),                                                            //Cijfer [0]
        new Cijfer(1.5),                                                          //Cijfer [1]
        new Cijfer(2),                                                            //Cijfer [2]
        new Cijfer(2.5),                                                          //Cijfer [3]
        new Cijfer(3),                                                            //Cijfer [4]
        new Cijfer(3.5),                                                          //Cijfer [5]
        new Cijfer(4),                                                            //Cijfer [6]
        new Cijfer(4.5),                                                          //Cijfer [7]
        new Cijfer(5),                                                            //Cijfer [8]
        new Cijfer(5.5),                                                          //Cijfer [9]
        new Cijfer(6),                                                            //Cijfer [10]
        new Cijfer(6.5),                                                          //Cijfer [11]
        new Cijfer(7),                                                            //Cijfer [12]
        new Cijfer(7.5),                                                          //Cijfer [13]
        new Cijfer(8),                                                            //Cijfer [14]
        new Cijfer(8.5),                                                          //Cijfer [15]
        new Cijfer(9),                                                            //Cijfer [16]
        new Cijfer(9.5),                                                          //Cijfer [17]
        new Cijfer(10),                                                           //Cijfer [18]
    };

    static void Main(string[] args)
    {
        //voegt een student toe aan een klas---------------------------------------------------------------------
        //maakt een nieuwe variabele aan een bind het aan een klas in de klassenlijst
        Klas klas01 = Klassenlijst[0];
        Klas klas02 = Klassenlijst[1];
        Klas klas03 = Klassenlijst[2];
        Klas klas04 = Klassenlijst[3];
        Klas klas05 = Klassenlijst[4];
        Klas klas69 = Klassenlijst[5];

        //maakt een nieuwe student aan en bindt het aan een student in de studentenlijst.
        Student student01 = StudentenLijst[0];
        Student student02 = StudentenLijst[1];
        Student student03 = StudentenLijst[2];
        Student student04 = StudentenLijst[3];
        Student student05 = StudentenLijst[4];
        Student student06 = StudentenLijst[5];
        Student student07 = StudentenLijst[6];

        **//this one of the parts you would want to focus on**
        Cijfer cijfer01 = Cijferlijst[0];
        Cijfer cijfer015 = Cijferlijst[1];
        Cijfer cijfer02 = Cijferlijst[2];
        Cijfer cijfer025 = Cijferlijst[3];
        Cijfer cijfer03 = Cijferlijst[4];
        Cijfer cijfer035 = Cijferlijst[5];
        Cijfer cijfer04 = Cijferlijst[6];
        Cijfer cijfer045 = Cijferlijst[7];
        Cijfer cijfer05 = Cijferlijst[8];
        Cijfer cijfer055 = Cijferlijst[9];
        Cijfer cijfer06 = Cijferlijst[10];
        Cijfer cijfer065 = Cijferlijst[11];
        Cijfer cijfer07 = Cijferlijst[12];
        Cijfer cijfer075 = Cijferlijst[13];
        Cijfer cijfer08 = Cijferlijst[14];
        Cijfer cijfer085 = Cijferlijst[15];
        Cijfer cijfer09 = Cijferlijst[16];
        Cijfer cijfer095 = Cijferlijst[17];
        Cijfer cijfer10 = Cijferlijst[18];

        //voegt de eerste student toe aan de eerste klas met gebruik van de voegstudenttoe() functie.
        klas01.Voegstudenttoe(student01);
        klas01.Voegstudenttoe(student02);
        klas01.Voegstudenttoe(student03);
        klas02.Voegstudenttoe(student04);
        klas02.Voegstudenttoe(student01);
        klas03.Voegstudenttoe(student05);
        klas04.Voegstudenttoe(student06);
        klas69.Voegstudenttoe(student04);
        klas69.Voegstudenttoe(student07);

        //Wijst een vak toe aan een student-----------------------------------------------------------------------
        //maakt variabelen aan en bindt ze aan een vak waarna de variabele aangewezen word aan een student.
        Vakken VakNED = VakkenLijst[0];
        Vakken VakENG = VakkenLijst[1];
        Vakken VakREK = VakkenLijst[2];
        Vakken VakSWP = VakkenLijst[3];
        Vakken VakWDV = VakkenLijst[4];
        Vakken VakTND = VakkenLijst[5];
        Vakken VakMSL = VakkenLijst[6];
        Vakken VakGDV = VakkenLijst[7];
        Vakken VakPRJ = VakkenLijst[8];

        //vakken aanwijzen aan de studenten
        //student 1 krijgt vakken aangewezen
        student01.VoegVakToe(VakNED);
        student01.VoegVakToe(VakENG);
        student01.VoegVakToe(VakPRJ);
        student01.VoegVakToe(VakMSL);
        student01.VoegVakToe(VakWDV);
        student01.VoegVakToe(VakSWP); 

        //student 2 krijgt vakken aangewezen
        student02.VoegVakToe(VakWDV);
        student02.VoegVakToe(VakSWP);

        //student 3 krijgt vakken aangewezen
        student03.VoegVakToe(VakWDV);
        student03.VoegVakToe(VakSWP);

        //student 4 krijgt vakken aangewezen
        student04.VoegVakToe(VakWDV);
        student04.VoegVakToe(VakSWP);
        student04.VoegVakToe(VakNED);

        //student 5 krijgt vakken aangewezen
        student05.VoegVakToe(VakWDV);
        student05.VoegVakToe(VakSWP);

        //student 6 krijgt vakken aangewezen
        student06.VoegVakToe(VakWDV);
        student06.VoegVakToe(VakGDV);

        student06.VoegVakToe(VakSWP);
        student06.VoegVakToe(VakREK);
        student06.VoegVakToe(VakNED);
        student06.VoegVakToe(VakPRJ);     

        //student 7 krijgt vakken aangewezen
        student07.VoegVakToe(VakSWP);
        student07.VoegVakToe(VakREK);
        student07.VoegVakToe(VakNED);
        student07.VoegVakToe(VakPRJ);
    }
}

the premise is simple:

  1. A student can be put in a class (although they dont have to).

  2. A student can also be assigned several lessons.

  3. Those lessons can have results differing per student (from tests and the likes, but for simplicitys sake i'll keep it to one main result)

for some more context, here are the other classes used

{
    class Vakken
    {
        //de properties van de vakken zoals het ID van een vak en de bijbehorende naam.
        public string VakID { get; set; }
        public string VakNaam { get; set; }
        public List<Cijfer> CijferLijst { get; } = new List<Cijfer>();

        //constructor met variabelen
        public Vakken (string vakID, string vaknaam)
        {
            VakID = vakID;
            VakNaam = vaknaam;
        }

        public void VoegCijferToe(Cijfer nieuwcijfer)
        {
            if (nieuwcijfer != null)
            {
                CijferLijst.Add(nieuwcijfer);
            }
        }
    }
}

this contains the lessons and a funciton that can add results to such a lesson (or at least, thats what its supposed to) As for the next class:

    class Cijfer
{
    public double Cijfers { get; set; }

    public Cijfer(double cijfers)
    {
        Cijfers = cijfers;
    }
}

This class describes the result.

class Klas
{
    public string KlasCode { get; set; }
    public string KlasnNaam { get; set; }
    public List<Student> Studentenlijst { get; } = new List<Student>(); //voorkomt dat de lijst niet herplaatst kan worden door een andere gebruiker.

    public Klas(string klasNummer, string klasNaam, List<Student> Studenten)
    {
        KlasCode = klasNummer;
        KlasnNaam = klasNaam;
        Studentenlijst = Studenten;
    }

    public void Voegstudenttoe(Student NieuweStudent)
    {

        if(NieuweStudent != null)
        {
            Studentenlijst.Add(NieuweStudent);
        }
    }
}

this describes a class and a function that allows one to add students to a class.

class Student
{
    //alle properties van de student
    public string StudentNummer { get; set; }
    public string Naam { get; set; }
    public string Postcode { get; set; }
    public string TelefoonNummer { get; set; }
    public List<Vakken> VakkenLijst { get; } = new List<Vakken>();

    //een no-argument constructor
    public Student(string studentnummer1, string naam1, string postcode1, string telefoonnummer1)
    {
        StudentNummer = studentnummer1;
        Naam = naam1;
        Postcode = postcode1;
        TelefoonNummer = telefoonnummer1;
    }

    /*override van de ToString methode? (ik weet niet of dit ook de goede manier is)
    public override string ToString()
    {
        return $"Studentnummer: {StudentNummer}, naam: {Naam}, postcode: {Postcode}, telefoonnummer :{TelefoonNummer}";
    }*/

    //voegt vakken toe aan een lijst bij uitvoeren
    public void VoegVakToe(Vakken Vak)
    {
        if (Vak != null)
        {
            VakkenLijst.Add(Vak);
        }
    }

}

and finally the class that describes a student and a function that can grant a student lessons (Vakken).

I want to add to this that I am a beginner, so forgive me if i make any beginner mistakes.

Having said all that, is anybody willing to help me?

just at a logical level...

You have to manage the following relationships:

1 class => 0-n students

1 student => 0-n Lessons

1 student => 0-n scoresPerLesson

So at least I would develop 4 c# classes (plus a manager), each of them containing the proper relationship and the manipulating methods:

Each class knows about partecipants

public class Class {
    internal List<Student> students;

    public bool AddStudent(Student aStudent) {
        if (students.Contains(aStudent))  // you have to manage an ID property
            return false;

        students.Add(aStudent);
        return true;
    }

    public List<Student> GetStudents() {
        return students;
    }

Each students knows about lessons attended, and scores assigned

public class Student {
    internal List<Lesson> lessons;
    private List<ScorePerLesson> myScores;

    private class ScorePerLesson {
        private int lessonID;
        private Score score;

        public Score Score { get => score; set => score = value; }
        public int LessonID { get => lessonID; set => lessonID = value; }
    }

    public bool addLesson (Lesson aLesson) {
        if (lessons.Contains(aLesson)) // you have to manage an ID property
            return false;

        lessons.Add(aLesson);
        return true;
    }

    public List<Lesson> GetLessonsAttended() {
        return lessons;
    }

    public List<Score> getScoresPerLesson(Lesson reqLesson) {
        List<Score> retScores = new List<Score>();
        foreach (var sc in myScores) {
            if (sc.LessonID.Equals(reqLesson.ID))
                retScores.Add(sc.Score);
        }
        return retScores;
    }

Each lesson knows about students attending

public class Lesson {
    internal List<Student> attendants;
    internal string subject;
    internal List<DateTime> schedule;
    internal string ID;

    public List<Student> GetPartecipant() {
        return attendants;
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM