I find myself unable to implement critical sections of my code for a project. The assignment is to develop the backend a text-based gradebook. I have a method, addStudent, which is supposed to add a student to a list in a section, also contained in a list.
I have a method in the Gradebook class, addStudent, which is supposed to add a student object to a list contained within the current Section object. The sections are also contained in a list. I have tried all sorts of combinations, but I can't seem to find the magic words that actually let me accomplish this.
The file is defined in 5 files/classes; Assignment(can be ignored for now), Student, Section, Gradebook, Program (also can ignore, only used for front end (input to pass to mehtods, etc.)).
Here is the method I need to implement, contained in the Gradebook class.
public bool addStudent(string firstName, string lastName, string username, long phoneNumber)
{
return false; //FIXME
}
Here is the Section Class:
class Section
{
string sectionName;
//probably more properties need to be implemented, or at least would make life simpler
public Section(string sectionName)
{
this.sectionName = sectionName;
List<Student> students = new List<Student>();
List<Assignment> assignments = new List<Assignment>();
}
}
Here is the Student Class:
class Student
{
string firstName = null;
string lastName = null;
long studentID = 0;
long phoneNumber = 0;
int absentcount = 0;
int tardyCount = 0;
double gradePercent = 0;
//need to add more properties, read through Gradebook API for more
public Student(string firstName, string lastName, long studentID, long phoneNumber)
{
this.firstName = firstName;
this.lastName = lastName;
this.studentID = studentID;
this.phoneNumber = phoneNumber;
List<Assignment> studentAssignments = new List<Assignment>();
}
I am aware of the duplicate assignment definitions, I am keeping both in for now, likely for redundancy, though this might not be a great idea, in which case I will only keep the student version.
I expect to be able to just add some longer line referencing each object in the respective lists, but I always come up with an error, either that I need to make the fields read only, or that the object doesn't exist. The only solution I can think of would be to keep an index for the last modified element in the list, but I'd rather not do that.
I would logically think to do something like this (I know this is wrong): currentSection.Student.Add(Student(firstName, lastName, username, phoneNumber))
. I know this can't be though, because I need to refernce the list element, not the class itself.
I also may just be overthinking this quite a bit but any help would be appreciated.
You should make your List<Student> students
accessible from outside your Session
constructor
Change your Section
declaration to:
class Section
{
string sectionName;
public List<Student> students;
//probably more properties need to be implemented, or at least would make life simpler
public Section(string sectionName)
{
this.sectionName = sectionName;
students = new List<Student>();
List<Assignment> assignments = new List<Assignment>();
}
}
Add the student to your list with
currentSection.students.Add(new Student(firstName, lastName, username, phoneNumber));
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.