-2

In my program I have a list of marks and a dictionary which stores the studentId. I want that the user enters the studentId and according to that id it will point to the list of marks. I think I am implementing it incorrectly. Can someone help me in implementing it. Thanks

 public class Student() {
        private string name;
        private string surname;
        private string dob;
        private string address;
        private int id;

        public Student()
        {
        }
        public Student(string year,string name, string surname, string dob, string address)
        {
            this.name = name;
            this.surname = surname;
            this.dob = dob;
            this.address = address;
            this.year = year;
        }


      public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Surname
        {
            get { return surname; }
            set { surname = value; }

        }

        public string DOB
        {
            get { return dob; }
            set { dob = value; }
        }

        public string Addr
        {
            get { return address; }
            set { address = value; }

        }
        public int Id
        {
            get { return id; }
            set { id = value; }
        }


        private string year;

        public string Year
        {
            get { return year; }
            set { year = value; }

        }
         public Student(string s)
        {
            string[] splitted = s.Split(',');
            name = splitted[0];
            surname = splitted[1];
            dob = splitted[2];
            address = splitted[3];
           // id = splitted[4];          
         }
        public Dictionary<int,List<Marks>> dictionary= new Dictionary<int,List<Marks>>();
        public List<Marks> Mathematics = new List<Marks>();
        public List<Marks> English = new List<Marks>();
        public List<Marks> Maltese = new List<Marks>();
        public List<Marks> ReligiousStudies = new List<Marks>();
        public List<Marks> SocialStudies = new List<Marks>();
        public Dictionary<int, List<Marks>> dictionar = new Dictionary<int, List<Marks>>();


        public void AddMarks(int hyexam, int anexam)
        {
            {
                Console.WriteLine("enter id of student to input marks to:");
                string id = Console.ReadLine();
                if (dictionar.ContainsKey(Id).Equals(id))
                {
                    Mathematics.Add(new Marks(hyexam, anexam));
                    English.Add(new Marks(hyexam, anexam));
                    Maltese.Add(new Marks(hyexam, anexam));
                    ReligiousStudies.Add(new Marks(hyexam, anexam));
                    SocialStudies.Add(new Marks(hyexam, anexam));
                    dictionar.Add(id, (Mathematics)); //dont know how to implement it
                }
                else
                {
                    Console.WriteLine("id not found");
                }

            }
        }

   public class Marks
    {
        private int hyexam;
        private int anexam;
        private string id;
        public int HYEXAM
        {
            get { return hyexam; }
            set { hyexam = value; }
        }

        public int ANEXAM
        {
            get { return anexam; }
            set { anexam = value; }
        }

        public string Id
        {
            get { return id; }
            set { id = value; }
        }

        public Marks(int hyexam, int anexam)
        {          
            this.hyexam = hyexam;
            this.anexam = anexam;
        }
        public Marks(string id)
        {
            this.id = id;
        }

        public double OverallExam()
        {
            return (0.4 * hyexam) + (0.6 * anexam);
        }

    }
}
7
  • 2
    First thing to note: you should look into automatically implemented properties - they'd vastly reduce the amount of boiler-plate in the first half of your code.
    – Jon Skeet
    Dec 4, 2012 at 17:41
  • What excactly is the problem? Where exactly are you stuck? Dec 4, 2012 at 17:42
  • 1
    you have to add all the marks or just one list of marks? You choice of data structure makes me scratch my head. It should be Marks, wit h subject Id, instead of List<Marks> for each subject. (Unless there are multiple set of marks per subject)
    – Tilak
    Dec 4, 2012 at 17:43
  • what is the problem you are having and trying to solve? just looking at this there are some syntax errors, is that your issue with it? compiling?
    – tam
    Dec 4, 2012 at 17:43
  • It looks like you have a single list (per subject) for the grades of all students. Is that what you intended? Because that doesn't seem like a good way to do it if you then what the grades to be stored in a dictionary keyed by student id. Dec 4, 2012 at 17:44

3 Answers 3

4

I'd say the main problem is your modelling. You've included the Dictionary<int, List<Marks>> as a field within Student. That means that each Student object has a dictionary. That makes no sense - because the key for the dictionary is meant to be the student ID, right?

It probably makes sense for each Student object to the lists of marks as you've currently got (although not as public fields, IMO). Given that information, do you really need a Dictionary going to the marks at all? Wouldn't it be cleaner to have a Dictionary<int, Student> somewhere (not in the Student class - maybe in a School class?) mapping each student ID to a Student, and you can get the marks from the Student?

Think hard about what you're trying to achieve, and where the data really belongs. In my experience, when you've got the data modelling right, the code usually follows in a clean way.

(I'd also question your Marks class, both in terms of name and design. What are those properties meant to represent? Isn't it really a single mark in an exam? Perhaps ExamResult would be clearer? Does it really need to be mutable?)

3
  • There should be 2 types of exams one halfhearly exam and another annual exam Dec 4, 2012 at 17:57
  • @MariaAttard: That's not at all clear from the property names :) Does it definitely make sense for them to be in a single class? Or perhaps there should be a collection of half-yearly marks, and another of annual marks.
    – Jon Skeet
    Dec 4, 2012 at 18:02
  • 2
    +1 to rethinking the model. If you find yourself fighting to implement the model, it could well be because the model is bad. Dec 4, 2012 at 19:04
1

I'd agree with Jon that this is definitely a modeling issue (based on the OP's other posts). If you're new to object oriented programming, the first thing you need to do is determine what objects you'll need to create to answer the problem.

What is a Student? A student has a name, id, dob, class year, etc. For every attribute a student has, you need to set up a field or property.

public class Student
{
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DoB { get; set; }
    ... etc ...
}

What is a Mark? From your descriptions, a Mark has a subject, a mid-exam score, and an annual-exam score.

public class Mark
{
    public string Subject { get; set; }
    public int MidScore { get; set; }
    public int FinalScore { get; set; }
}

What is the relationship between a Student and a Mark? A student has a list of marks associated with them. This is best described with a List<> object.

public class Student() // continued
{
    public List<Mark> Marks { get; set; }
}

Once you have the basics set up for your objects, it will be easier to filter out what you DON'T need, and make it much easier to answer your question. :)

0

My proposal:

Your "Mark" class must have something that identifies the subject (could be an enum called Subject, or an string, or an int with the ID of the subject if you have it stored in database)

Then you could have:

Console.WriteLine("enter id of student to input marks to:");
string id = Console.ReadLine();

var marks = new Dictionary<int, List<Mark>>();

if (UserExists(id))
{
    Console.WriteLine("mark for subject1:");        
    string s1 = Console.ReadLine();
    Console.WriteLine("mark for subject2:");        
    string s2 = Console.ReadLine();

    var list = new List<Mark>();
    list.Add(new Mark { Subject = SubjectEnum.Subject1, Value = Convert.ToDecimal(s1), });
    list.Add(new Mark { Subject = SubjectEnum.Subject2, Value = Convert.ToDecimal(s2), });

    marks.Add(Convert.ToInt32(id), list)
}
else
{
    Console.WriteLine("id not found");
}

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.