Last active
August 29, 2015 13:56
-
-
Save adamcooper/8856731 to your computer and use it in GitHub Desktop.
Students and Courses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GradeStudents | |
def self.call(teacher_name, course_name, grade_list) | |
course = CourseRegistry.lookup(course_name) | |
teacher = TeacherRegistry.lookup(teacher_name) | |
course.for_each_student do |student| | |
if grade = grade_list[student.name] | |
student.complete_course(course, grade) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RegisterCourse | |
def self.call(student_name, course_name) | |
course = CourseRegistry.lookup(course_name) | |
student = StudentRegistry.lookup(student_name) | |
course.add_student(student) | |
student.add_course(course) | |
end | |
end | |
class CourseRegistry | |
def self.lookup(course_name) | |
Course.new(course_name) | |
end | |
end | |
class StudentRegistry | |
def self.lookup(student_name) | |
Student.new(student_name) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Course | |
attr_accessor :title, :location | |
private | |
attr_accessor :teachers, :students | |
def initialize(params = {}); end | |
def add_teacher(teacher); end | |
def add_student(student); end | |
def for_each_student; end | |
def initialize(params = {}) | |
self.title = params[:title] || "" | |
self.location = params[:location] || "" | |
self.teachers = params[:teachers] || [] | |
self.courses = params[:courses] || [] | |
end | |
def add_teacher(teacher) | |
self.teachers << teacher | |
end | |
def add_student(student) | |
self.students << student | |
end | |
def for_each_student | |
students.each { |student| yield student } | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person | |
attr_accessor :name, :age | |
def initialize(params = {}); end | |
def initialize(params = {}) | |
self.name = params[:name] || "" | |
self.age = params[:age] || 0 | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Student < Person | |
protected | |
attr_accessor :gpa | |
def initialize(params = {}); end | |
def add_course(course); end | |
def complete_course(course, gpa); end | |
private | |
attr_accessor :courses | |
attr_accessor :grades | |
def calculate_gpa; end | |
public | |
def initialize(params={}) | |
self.gpa = params[:gpa] || 0.0 | |
self.courses = params[:courses] || [] | |
self.grades = params[:grades] || {} | |
super | |
end | |
def add_course(course) | |
courses << course | |
end | |
def complete_course(course, grade) | |
grades[course] = grade | |
calculate_cpa | |
end | |
private | |
def calculate_gpa | |
self.gpa = grades.values.inject(0.0) {|gpa, grade| gpa += grade } / grades.size | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Teacher < Person | |
attr_accessor :description | |
private | |
attr_accessor :courses | |
def initialize(params={}); end | |
def add_course(course); end | |
def initialize(params={}) | |
self.description = params[:description] || "" | |
self.courses = params[:courses] || [] | |
super | |
end | |
def add_course(course) | |
courses << course | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment