Maria Attard
Maria Attard

Reputation: 21

Adding items to a list

I have a list one for students and one for marks. I want to add student marks according to the id entered and then view the records. I'm not implementing it correctly. Can someone tell me how to do it correctly please? This is the output I am trying to achieve: 1) add student 2)add marks: user will be asked to enter id and according to the id if it exists then marks will be stored according to that id. 3)View Marks: view student id and his marks

class Student
{     
    public Student(string name, string surname, string dob, string address)
    {
        this.Name = name;
        this.Surname = surname;
        this.DOB = dob;
        this.Address = address;
    }

    public List<Marks> studentId = new 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 string Name { get; set; }
    public string Surname { get; set; }
    public string DOB { get; set; }
    public string Addr { get; set; }
    public string Id { get; set; }

    public Student(string s)
    {
        string[] splitted = s.Split(',');
        name = splitted[0];
        surname = splitted[1];
        dob = splitted[2];
        address = splitted[3];
        id = splitted[4];          
     }

    public void AddMarks(string id, int hyexam, int anexam)
    {
        if (Id.Equals(id))
        {
            studentId.Add(new Marks(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));
        }
        else
        {
            Console.WriteLine("id not found");
        }
    }

    public void StudMarks()
    {
        foreach (Marks mrk in studentId)
        {
            Console.WriteLine(mrk);
        }
        foreach (Marks mrk in Mathematics)
        {
            Console.WriteLine(mrk);
        }

        foreach (Marks mrk in English)
        {
            Console.WriteLine(mrk);
        }

        foreach (Marks mrk in Maltese)
        {
            Console.WriteLine(mrk);
        }

        foreach (Marks mrk in ReligiousStudies)
        {
            Console.WriteLine(mrk);
        }            
     }           
   }
}

public class ClassYear 
{               
    private const string filestud = "C:\\Users\\Maria\\Desktop\\Students\\stud.txt";    
    public string Year { get; set; }
    public List<Student> mystudent=new List <Student> ();
    public List<Student> MyStud
    {
        get { return mystudent; }
    }

    public void Addstudent(string name,string surname,string dob, string addr)
    {
        mystudent.Add(new Student(name,surname,dob,addr));                   
    }

    private void LoadFromFile()
    {
        try
        {
            string s;
            StreamReader myFile = File.OpenText(filestud);
            while ((s = myFile.ReadLine()) != null)
            {
                Student st = new Student(s);
                mystudent.Add(st);
            }
            myFile.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("There was a file handling problem");
        }
    }

    private void DumpToFile()
    {
        try
        {
            StreamWriter myFile = File.CreateText(filestud);
            foreach (Student s in mystudent)
            {
                myFile.WriteLine(s.ToString());
            }
            myFile.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("There was a file handling problem");
        }
    }

    private int SearchItem(int search)
    {
        int found = -1;
        if (mystudent != null)
        {
            foreach (Student st in mystudent)
            {
                found++;
                if (Student.Equals(search,st)) {
                    break;
                }
            }
        }
        return found;
    }

    private void Delete(int index)
    {
        SearchItem(index);
        mystudent.RemoveAt(index);
        foreach (Student st in mystudent) {
            Console.WriteLine(st);
        }            
    }

    private void EditItem(int index)        
    {
        Student stud = new Student();        
        SearchItem(index);
        mystudent[index] = stud;
        Console.WriteLine("current record is" + index);
        Console.WriteLine("enter new record:");
        stud.Name = Console.ReadLine();
        stud.Surname = Console.ReadLine();
        stud.Addr = Console.ReadLine();
        stud.Id = Console.ReadLine();
        foreach (Student st in mystudent)
        {
            Console.WriteLine(st);
        }
    }
    private void ViewStudents()
    {
        foreach (Student st in mystudent)
        {
            Console.WriteLine(st);
        }
    }                    
 }  
 }

public class Marks
{      
    public int HYEXAM { get; private set; }
    public int ANEXAM { get; private set; }
    public string Id { get; private set; }

    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 * HEYXAM) + (0.6 * ANEXAM);
    }
   }
 }  

Upvotes: 0

Views: 253

Answers (1)

MadHenchbot
MadHenchbot

Reputation: 1385

Ok, from what I can tell, you have a list of students. Each of these students has several lists of marks: Mathematics, English, Maltese, etc.

So let's say you want to add a Mathematics mark to a certain student. Assuming you have:

List<Student> students;
int userId; // the id entered by the user
Mark mathMark; // the mark you want to add

Search the list for the given id:

foreach (Student myStudent in students)
{
    if (myStudent.Id == userId)
    {
        myStudent.Mathematics.Add(mathMark);
    }
}

Then when you want to display all the math marks for your student:

foreach (Mark mark in myStudent.Mathematics)
{
    Console.WriteLine(mark.HYEXAM + "," + mark.ANEXAM);
}

Is that what you're after?

Upvotes: 1

Related Questions