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.
C#
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 ListStudent> studentList = new ListStudent>();
public ListMarks> markslist = new ListMarks>();
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);
Hemant SrivastavaRank #581 · 4.2K reputationPosted Nov 23, 2012, 12:44 PM EST
public Hashtable stuHashTable= new Hashtable(); Then you can add student records into hashtable like (where you are adding student record into your list): if(!stuHashTable.ContainsKey((string)id)) { //Insert new student not duplicate coz hash table won't allow you //to have duplicate keys stuHashTable.Add(id,st); }But for your Marks class, I want to suggest you that instead of creating marks class
create a "Subject_Marks" class
in which you could have following fields :
private string subject_Id;
private int hyexam;
private int anexam;
(For example: Subject_Id = 1 for Maths, 2 for English, 3 for SocialStudies etc.)
Then create hashtable for "Subject_Marks"
public Hashtable marksHashTable= new Hashtable();
You can iterate any hashtable with "DictionaryEntry", refer to following link for hashtable iteration:
http://www.mindfiresolutions.com/How-To-Iterate-the-Hashtable-in-C-311.php
Hope this could help you..
rasul moh64 reputationPosted Nov 23, 2012, 9:38 PM EST
Posted on: 23 Nov 2012
I have
2 listsin my program one that store objects of typestudentand another one which stores objects of typemarks.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
Idof 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 ListStudent> studentList = new ListStudent>(); public ListMarks> markslist = new ListMarks>(); 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);Posted on: 23 Nov 2012
For student hashtable, you could create as (the place where you are creating list):
public Hashtable stuHashTable= new Hashtable(); Then you can add student records into hashtable like (where you are adding student record into your list): if(!stuHashTable.ContainsKey((string)id)) { //Insert new student not duplicate coz hash table won't allow you //to have duplicate keys stuHashTable.Add(id,st); }But for your Marks class, I want to suggest you that instead of creating marks class
create a "Subject_Marks" class
in which you could have following fields :
private string subject_Id;
private int hyexam;
private int anexam;
(For example: Subject_Id = 1 for Maths, 2 for English, 3 for SocialStudies etc.)
Then create hashtable for "Subject_Marks"
public Hashtable marksHashTable= new Hashtable();
You can iterate any hashtable with "DictionaryEntry", refer to following link for hashtable iteration:
http://www.mindfiresolutions.com/How-To-Iterate-the-Hashtable-in-C-311.php
inna grech3 reputationPosted Nov 24, 2012, 9:59 AM EST
Hemant SrivastavaRank #581 · 4.2K reputationPosted Nov 28, 2012, 10:20 AM EST