Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

grandM

macrumors 68000
Original poster
Oct 14, 2013
1,565
329
Hi everyone

I'm coding a grade book. Doing so I wondered what you would code in your real life projects. I've coded several solutions. Each having advantages and disadvantages. Please elaborate your answer. Or even come up with a better solution :)

Code:
struct Name {

   var firstName: String

   var lastName: String

}


struct Course {

   var title = ""

   var lectureMoments: Int

   var gradeBook: GradeBook

}


struct Pupil {

   let name: Name

   var courses: [Course]

}


struct GradeBook {

   var grades: [Int] = []

}

In this first solution course contains the grades. This seems wrong though. If course were to be an object one would expect a dictionary linking the pupils of the course to their grades. As such one would get an n-to-n relation (a student has several courses, a course has several students). Adding a grade would require the developer to iterate through the entire gradeBook array.

Code:
struct Name {

   var firstName: String

   var lastName: String

}


struct Course {

   var title = ""

   var lectureMoments: Int

}


struct Pupil {

   let name: Name

   var gradesPerCourse: [Course: GradeBook]

}


struct GradeBook {

   var grades: [Int] = []

}
This solution wouldn't run yet because I would have to render Course equable and hashable. But once more I have my doubts. When I would have to add a grade it would be really messy to enter that Course instance as key. Further more I'm in doubt if a grade book isn't directly correlated to a course instance.

Code:
struct Name {

   var firstName: String

   var lastName: String

}



struct Course {

   var title = ""

   var lectureMoments: Int

}



struct Pupil {

   let name: Name

   var gradesPerCourse: [String: GradeBook]

}



struct GradeBook {

   let course: Course

   var grades: [Int] = []

}
The problem I have with it is that the title of a course object would be the same as the key in the gradeBooks property of pupil.

Code:
struct Name {

   var firstName: String

   var lastName: String

}



struct Course {

   var title: String

   var lectureMoments: Int

}



struct Pupil {

   let name: Name

}



struct GradeBook {

   let course: Course

   let pupil: Pupil

   var grades: [Int] = []

}
In this approach one would have to iterate over all grade books to get the right one. Changing it is easy.

Please share your vision! Tx
 
I could also do this of course. Thinking of it I actually prefer it:
Code:
struct Name {

    var firstName: String

    var lastName: String

}



struct Course {

    var title: String

    var lectureMoments: Int

}



struct Pupil {

    let name: Name

}



struct Grade {

    let course: Course

    let pupil: Pupil

    let grade: Int

}



struct GradeBook {

    var grades: [Grade] = []

}
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.