4
Answers

using a hasehtable

inna grech

inna grech

11y
1.7k
1

I have 2 lists in my program one that store objects of type student and another one which stores objects of type marks.

I have 3 methods one that inputs the details of the students, one that inputs the marks and one to view student details and their marks. My question is how I can use a hash table to get the Id of a student and then use it to input the marks and then how to retrieve both the student details and their marks altogether.

Please help me if you can.

public class Student
    {
        private string name;
        private string surname;
        private string dob;
        private string address;
        private string id;
        private string Name_2;
        private string Surname_2;


        public Student()
        {
            //empty contructor
        }

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

        public Student(string Name_2, string Surname_2)
        {
            // TODO: Complete member initialization
            this.Name_2 = Name_2;
            this.Surname_2 = Surname_2;
        }

        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 string Id
        {
            get { return id; }
            set { id = value; }
        }

    }
}

public struct Marks
{
    private int hyexam;
    private int anexam;

    public int HYEXAM
    {
        get { return hyexam; }
        set { hyexam = value; }
    }

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

    }

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

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


}

 public class JuniorStudents : Student
    {
        public int numofStudents;
        public Marks Mathematics;
        public Marks English;
        public Marks Maltese;
        public Marks ReligiousStudies;
        public Marks SocialStudies;


        const string filestud = "C:\\Users\\Maria\\Desktop\\Students.txt";
        const string filemarks= "C:\\Users\\Maria\\Desktop\\Marks.txt";
        public List<Student> studentList = new List<Student>();
        public List<Marks> markslist = new List<Marks>();

        public void ReadStudentsRecords()
        {

           try
            {
                string name;
                string surname;
                string dob;
                string addr;
                string id;
                StreamReader myFile = File.OpenText(filestud);
                while (myFile.ReadLine() != null)
                {
                    name = myFile.ReadLine();
                    surname=myFile.ReadLine();
                    dob=myFile.ReadLine();
                    addr=myFile.ReadLine();
                    id=myFile.ReadLine();
                    Student st = new Student(name, surname, dob, addr, id);
                    studentList.Add(st);

                }
                myFile.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }

            }

        public void ReadMarksList()
        {
            try {                     
                StreamReader myFile = File.OpenText(filemarks);
                while (myFile.ReadLine() != null)
                {
                        Mathematics.HYEXAM = myFile.Read();
                        Mathematics.ANEXAM = myFile.Read();
                        English.HYEXAM = myFile.Read();
                        English.ANEXAM = myFile.Read();
                        Maltese.HYEXAM = myFile.Read();
                        Maltese.ANEXAM = myFile.Read();
                        ReligiousStudies.HYEXAM = myFile.Read();
                        ReligiousStudies.ANEXAM = myFile.Read();
                        SocialStudies.HYEXAM = myFile.Read();
                        SocialStudies.ANEXAM = myFile.Read();

                        markslist.Add(Mathematics);
                        markslist.Add(English);
                        markslist.Add(Maltese);
                        markslist.Add(ReligiousStudies);
                        markslist.Add(SocialStudies);
                }
                myFile.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }



        public void WriteStudentRecords()
        {   
            try
            {
                StreamWriter myFile = File.CreateText(filestud);
                foreach (Student s in studentList)
                {
                    myFile.WriteLine(s.ToString());
                }
                myFile.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }

        public void WriteMarksRecords()
        {
             try
            {
                StreamWriter myFile = File.CreateText(filemarks);
                foreach (Marks m in markslist)
                {
                    myFile.WriteLine(m.ToString());
                }
                myFile.Close();
            }
            catch (Exception e)
            {
                e.ToString();
            }
        }


        public void AddStudentRecord()
        {

            Student stud = new Student(Name,Surname,DOB,Addr,Id);
            studentList.Add(stud);

            //bool error = false;
            //if (error == false)
            //{
            //    try
            //    {
            //        stud.Name = name;
            //        stud.Surname = surname;
            //        stud.DOB = dob;
            //        stud.Addr = address;
            //        stud.Id = id;

            //    }
            //    catch (FormatException ex)
            //    {
            //        error = true;
            //        Console.WriteLine(ex.ToString());
            //    }

                studentList.Add(stud);
                //myFile.WriteLine(studentList);
               // numofStudents = studentList.Count;
            }
          //  return 1;

        public void AddMarksRecord(int mthyexam,int mtanexam, int enhyexam, int enanexam, int mlhyexam, int mlanexam, int rshyexam,int rsanexam, int sshyexam, int ssanexam)
        {
             bool error=false;
            if (error==false)
                try{
            Mathematics.HYEXAM= mthyexam;
            Mathematics.ANEXAM= mtanexam;
            English.HYEXAM = enhyexam;
            English.ANEXAM = enanexam;
            Maltese.HYEXAM = mlhyexam;
            Maltese.ANEXAM = mlanexam;
            ReligiousStudies.HYEXAM = rshyexam;
            ReligiousStudies.ANEXAM = rsanexam;
            SocialStudies.HYEXAM = sshyexam;
            SocialStudies.ANEXAM = ssanexam;


               }
                catch  (FormatException ex){
                error=true;
                Console.WriteLine (ex.ToString());
                }

            markslist.Add(Mathematics);
            markslist.Add(English);
            markslist.Add(Maltese);
            markslist.Add(ReligiousStudies);
            markslist.Add(SocialStudies);  


Answers (4)